/** @internal NOTE: Internal APIs. Subject to change. Use of these APIs in production applications is not supported. */
import { __decorate } from "tslib";
import { 
// eslint-disable-next-line @typescript-eslint/no-unused-vars
serialize, Vector3, KeyboardEventTypes } from '../../loader/babylonjs-import';
/**
 * Manage the keyboard inputs to control the movement of a TargetCamera.
 * @see http://doc.babylonjs.com/how_to/customizing_camera_inputs
 */
export class TwinfinityTargetCameraKeyboardInput {
    _cameraOptions;
    constructor(_cameraOptions) {
        this._cameraOptions = _cameraOptions;
    }
    /**
     * Defines the camera the input is attached to.
     */
    camera;
    /**
     * Gets or Set the list of keyboard keys used to control the forward move of the camera.
     */
    keysUp = [38, 87];
    /**
     * Gets or Set the list of keyboard keys used to control the backward move of the camera.
     */
    keysDown = [40, 83];
    /**
     * Gets or Set the list of keyboard keys used to control the left strafe move of the camera.
     */
    keysLeft = [37, 65];
    /**
     * Gets or Set the list of keyboard keys used to control the right strafe move of the camera.
     */
    keysRight = [39, 68];
    _keys = new Array();
    _onCanvasBlurObserver;
    _onKeyboardObserver;
    _transformedDirection = Vector3.Zero();
    _localDirection = Vector3.Zero();
    /**
     * Attach the input controls.
     * @param noPreventDefault Defines whether event caught by the controls should call preventdefault() (https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
     */
    attachControl(noPreventDefault) {
        if (this._onCanvasBlurObserver) {
            return;
        }
        const scene = this.camera.getScene();
        const engine = this.camera.getEngine();
        this._onCanvasBlurObserver = engine.onCanvasBlurObservable.add(() => {
            this._keys = [];
        });
        this._onKeyboardObserver = scene.onKeyboardObservable.add((info) => {
            const evt = info.event;
            if (!evt.metaKey) {
                if (info.type === KeyboardEventTypes.KEYDOWN) {
                    if (this.keysUp.indexOf(evt.keyCode) !== -1 ||
                        this.keysDown.indexOf(evt.keyCode) !== -1 ||
                        this.keysLeft.indexOf(evt.keyCode) !== -1 ||
                        this.keysRight.indexOf(evt.keyCode) !== -1) {
                        const index = this._keys.indexOf(evt.keyCode);
                        if (index === -1) {
                            this._keys.push(evt.keyCode);
                        }
                        if (!noPreventDefault) {
                            evt.preventDefault();
                        }
                    }
                }
                else {
                    if (this.keysUp.indexOf(evt.keyCode) !== -1 ||
                        this.keysDown.indexOf(evt.keyCode) !== -1 ||
                        this.keysLeft.indexOf(evt.keyCode) !== -1 ||
                        this.keysRight.indexOf(evt.keyCode) !== -1) {
                        const index = this._keys.indexOf(evt.keyCode);
                        if (index >= 0) {
                            this._keys.splice(index, 1);
                        }
                        if (!noPreventDefault) {
                            evt.preventDefault();
                        }
                    }
                }
            }
        });
    }
    /**
     * Detach the current controls from the specified dom element.
     */
    detachControl() {
        if (!this.camera) {
            return;
        }
        const scene = this.camera.getScene();
        const engine = this.camera.getEngine();
        if (this._onKeyboardObserver) {
            scene.onKeyboardObservable.remove(this._onKeyboardObserver);
        }
        if (this._onCanvasBlurObserver) {
            engine.onCanvasBlurObservable.remove(this._onCanvasBlurObserver);
        }
        this._onKeyboardObserver = null;
        this._onCanvasBlurObserver = null;
        this._keys = [];
    }
    /**
     * Update the current camera state depending on the inputs that have been used this frame.
     * This is a dynamically created lambda to avoid the performance penalty of looping for inputs in the render loop.
     */
    checkInputs() {
        if (this._onKeyboardObserver && this._cameraOptions.isKeyboardMoveEnabled) {
            const camera = this.camera;
            // Keyboard
            for (let index = 0; index < this._keys.length; index++) {
                const keyCode = this._keys[index];
                const speed = camera._computeLocalCameraSpeed();
                if (this.keysLeft.indexOf(keyCode) !== -1) {
                    this._localDirection.copyFromFloats(-speed, 0, 0);
                }
                else if (this.keysUp.indexOf(keyCode) !== -1) {
                    this._localDirection.copyFromFloats(0, 0, speed);
                }
                else if (this.keysRight.indexOf(keyCode) !== -1) {
                    this._localDirection.copyFromFloats(speed, 0, 0);
                }
                else if (this.keysDown.indexOf(keyCode) !== -1) {
                    this._localDirection.copyFromFloats(0, 0, -speed);
                }
                if (camera.getScene().useRightHandedSystem) {
                    this._localDirection.z *= -1;
                }
                camera.getViewMatrix().invertToRef(camera._cameraTransformMatrix);
                Vector3.TransformNormalToRef(this._localDirection, camera._cameraTransformMatrix, this._transformedDirection);
                camera.position.addInPlace(this._transformedDirection);
            }
        }
    }
    /**
     * Gets the class name of the current intput.
     * @returns the class name
     */
    getClassName() {
        return 'TargetCameraKeyboardInput';
    }
    /** @hidden */
    _onLostFocus() {
        this._keys = [];
    }
    /**
     * Get the friendly name associated with the input class.
     * @returns the input friendly name
     */
    getSimpleName() {
        return 'keyboard';
    }
}
__decorate([
    serialize()
], TwinfinityTargetCameraKeyboardInput.prototype, "keysUp", void 0);
__decorate([
    serialize()
], TwinfinityTargetCameraKeyboardInput.prototype, "keysDown", void 0);
__decorate([
    serialize()
], TwinfinityTargetCameraKeyboardInput.prototype, "keysLeft", void 0);
__decorate([
    serialize()
], TwinfinityTargetCameraKeyboardInput.prototype, "keysRight", void 0);
// (CameraInputTypes as any)['TargetCameraMousePanInput'] = TargetCameraMousePanInput;
//# sourceMappingURL=TwinfinityCameraKeyboardInput.js.map