"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.clonePointerEvent = void 0;
exports.webInput = webInput;
const clonePointerEvent = (event, partial) => {
    const { pointerId, pointerType, clientX, clientY, screenX, screenY } = event;
    return new PointerEvent(event.type, {
        pointerId,
        clientX,
        clientY,
        screenX,
        screenY,
        pointerType,
        ...partial,
    });
};
exports.clonePointerEvent = clonePointerEvent;
function webInput(window, root, bootOpt) {
    const pointerCapture = {};
    const pointers = {};
    const pointerInListener = (event) => {
        const { pointerId, clientX, clientY } = event;
        pointers[pointerId] = { screenX: clientX, screenY: clientY, down: false };
    };
    const pointerOutListener = (event) => {
        // console.log('pointerOutListener')
        const { pointerId } = event;
        delete pointers[pointerId];
    };
    const pointerMoveListener = (event) => {
        const { pointerId, clientX, clientY } = event;
        if (!pointers[pointerId]) {
            pointerInListener(event);
        }
        pointers[pointerId].screenX = clientX;
        pointers[pointerId].screenY = clientY;
    };
    const pointerDownListener = (event) => {
        const { pointerId } = event;
        pointers[pointerId].down = true;
    };
    const pointerUpListener = (event) => { };
    const pointerCancelListener = (event) => {
        //
    };
    const opt = { capture: true };
    root.addEventListener('pointerover', pointerInListener, opt);
    root.addEventListener('pointerout', pointerOutListener, opt);
    root.addEventListener('pointermove', pointerMoveListener, opt);
    root.addEventListener('pointerdown', pointerDownListener, opt);
    root.addEventListener('pointerup', pointerUpListener, opt);
    root.addEventListener('pointercancel', pointerCancelListener, opt);
    const input = {
        keyboard: {},
        gamepad: {
            getGamepad: (i) => {
                const gamepads = navigator.getGamepads();
                const gamepad = gamepads[i];
                return gamepad;
            },
            addEventListener: (type, listener, options) => {
                window.addEventListener(type, listener, options);
                return () => {
                    window.removeEventListener(type, listener, options);
                };
            },
        },
        pointer: {
            getPointerPosition: (pointerId) => {
                const pointer = pointers[pointerId];
                if (pointer) {
                    return pointer;
                }
                else {
                    throw new Error('pointer cannot be found');
                }
            },
            setPointerCapture: (element, pointerId) => {
                pointerCapture[pointerId] = element;
                element.setPointerCapture(pointerId);
                return () => {
                    element.releasePointerCapture(pointerId);
                    delete pointerCapture[pointerId];
                };
            },
        },
    };
    return input;
}
//# sourceMappingURL=input.js.map