#include "core/input/key_remap.hpp"

#include <SDL3/SDL.h>

#include "core/jail/jinput.hpp"
#include "game/options.hpp"

namespace KeyRemap {

    static void mirror(SDL_Scancode custom, SDL_Scancode standard, const bool* ks) {
        if (custom == standard || custom == SDL_SCANCODE_UNKNOWN) {
            Ji::setVirtualKey(standard, Ji::VirtualSource::REMAP, false);
            return;
        }
        Ji::setVirtualKey(standard, Ji::VirtualSource::REMAP, ks[custom]);
    }

    void update() {
        const bool* ks = SDL_GetKeyboardState(nullptr);
        if (ks == nullptr) {
            return;
        }
        mirror(Options::keys_game.up, SDL_SCANCODE_UP, ks);
        mirror(Options::keys_game.down, SDL_SCANCODE_DOWN, ks);
        mirror(Options::keys_game.left, SDL_SCANCODE_LEFT, ks);
        mirror(Options::keys_game.right, SDL_SCANCODE_RIGHT, ks);
    }

}  // namespace KeyRemap
