"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InputManager = void 0;
const keyboard_js_1 = require("./keyboard.js");
const MOD_SHIFT = 8;
class InputManager {
    constructor(page) {
        this.page = page;
    }
    getModifiers(mods) {
        const { altKey, ctrlKey, metaKey, shiftKey } = mods;
        return 0 | (altKey ? 1 : 0) | (ctrlKey ? 2 : 0) | (metaKey ? 4 : 0) | (shiftKey ? 8 : 0);
    }
    async click(point, options = {}) {
        const { delay = 0 } = options;
        await this.mouseDown(point, options);
        if (delay) {
            await new Promise(r => setTimeout(r, delay));
        }
        await this.mouseUp(point, options);
    }
    async mouseDown(point, options = {}) {
        const { modifiers = 0, button = 'left', clickCount = 1 } = options;
        const { x, y, zoom = 1 } = point;
        await this.page.send('Input.dispatchMouseEvent', {
            type: 'mousePressed',
            x: Math.round(x * zoom),
            y: Math.round(y * zoom),
            modifiers,
            button,
            clickCount,
        });
    }
    async mouseUp(point, options = {}) {
        const { modifiers = 0, button = 'left', clickCount = 1 } = options;
        const { x, y, zoom = 1 } = point;
        await this.page.send('Input.dispatchMouseEvent', {
            type: 'mouseReleased',
            x: Math.round(x * zoom),
            y: Math.round(y * zoom),
            modifiers,
            button,
            clickCount,
        });
    }
    async mouseMove(point, options = {}) {
        const { modifiers = 0, button } = options;
        const { x, y, zoom = 1 } = point;
        await this.page.send('Input.dispatchMouseEvent', {
            type: 'mouseMoved',
            x: Math.round(x * zoom),
            y: Math.round(y * zoom),
            modifiers,
            button,
        });
    }
    async print(text, options = {}) {
        const { parallel = true } = options;
        if (parallel) {
            await this.printParallel(text);
        }
        else {
            for (const char of text) {
                await this.printSingleChar(char, options);
            }
        }
    }
    async printParallel(text) {
        const promises = [];
        for (const char of text) {
            const [key, modifiers] = this.charToKey(char);
            promises.push(this.keyDown(key, modifiers));
            promises.push(this.keyUp(key, modifiers));
        }
        await Promise.all(promises);
    }
    async printSingleChar(char, options = {}) {
        const { delay = 0 } = options;
        const [key, modifiers] = this.charToKey(char);
        await this.keyDown(key, modifiers);
        if (delay) {
            await new Promise(r => setTimeout(r, delay));
        }
        await this.keyUp(key, modifiers);
    }
    async keyDown(key, modifiers = 0) {
        await this.page.send('Input.dispatchKeyEvent', {
            type: 'keyDown',
            modifiers,
            text: key.text,
            key: key.key,
            code: key.code,
            windowsVirtualKeyCode: key.keyCode,
        });
    }
    async keyUp(key, modifiers = 0) {
        await this.page.send('Input.dispatchKeyEvent', {
            type: 'keyUp',
            modifiers,
            key: key.key,
            code: key.code,
            windowsVirtualKeyCode: key.keyCode,
        });
    }
    charToKey(char) {
        const isLetter = char.toLowerCase() !== char.toUpperCase();
        const isUpperCase = isLetter && char === char.toUpperCase();
        const modifiers = isUpperCase ? MOD_SHIFT : 0;
        const key = (0, keyboard_js_1.lookupKey)(char);
        return [key, modifiers];
    }
}
exports.InputManager = InputManager;
//# sourceMappingURL=input-manager.js.map