import { BaseInput } from './base-input';
export class Keyboard extends BaseInput {
    constructor(element) {
        super(element);
        this.keysDown = new Map();
        this.keysJustPressed = new Map();
        this.keysJustReleased = new Map();
        this.handleKeyDown = this.handleKeyDown.bind(this);
        this.handleKeyUp = this.handleKeyUp.bind(this);
    }
    capture(preventDefault) {
        this.active = true;
        this.preventDefault = preventDefault;
        this.element.addEventListener('keydown', this.handleKeyDown);
        this.element.addEventListener('keyup', this.handleKeyUp);
    }
    release() {
        this.active = false;
        this.element.removeEventListener('keydown', this.handleKeyDown);
        this.element.removeEventListener('keyup', this.handleKeyUp);
    }
    getState() {
        const state = {
            keysDown: new Map(this.keysDown),
            keysJustPressed: new Map(this.keysJustPressed),
            keysJustReleased: new Map(this.keysJustReleased)
        };
        this.keysJustPressed.clear();
        this.keysJustReleased.clear();
        return state;
    }
    handleKeyDown(event) {
        if (this.active && this.preventDefault)
            event.preventDefault();
        if (this.keysDown.get(event.keyCode))
            return;
        this.keysDown.set(event.keyCode, true);
        this.keysJustPressed.set(event.keyCode, true);
        this.keysJustReleased.set(event.keyCode, false);
    }
    handleKeyUp(event) {
        if (this.active && this.preventDefault)
            event.preventDefault();
        if (!this.keysDown.get(event.keyCode))
            return;
        this.keysDown.set(event.keyCode, false);
        this.keysJustPressed.set(event.keyCode, false);
        this.keysJustReleased.set(event.keyCode, true);
    }
}
