"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.webInit = webInit;
const keyboard_1 = require("../../event/keyboard");
const showNotification_1 = require("../../showNotification");
const theme_1 = require("../../theme");
function webInit(system, window, root) {
    const { document } = window;
    const blurListener = () => {
        // when window is blurred by a popup/alert as a result
        // of a "keydown", "keyup" will not be fired
        system.input.keyboard.pressed = [];
    };
    // double click is redefined in the system, so "whatever"
    // browser default behavior should be ignored
    const dbClickListener = (event) => {
        event.preventDefault();
    };
    const keyDownListener = (event) => {
        if (!(0, keyboard_1.isSupportedKeyboardEvent)(event)) {
            return;
        }
        const { metaKey, key, repeat } = event;
        if (metaKey) {
            return;
        }
        const index = system.input.keyboard.pressed.indexOf(key);
        system.input.keyboard.repeat = repeat;
        if (index === -1) {
            system.input.keyboard.pressed.push(key);
        }
    };
    const keyUpListener = (event) => {
        if (!(0, keyboard_1.isSupportedKeyboardEvent)(event)) {
            return;
        }
        const { metaKey, key } = event;
        if (metaKey) {
            return;
        }
        const index = system.input.keyboard.pressed.indexOf(key);
        system.input.keyboard.pressed.splice(index, 1);
    };
    // prevent app from reacting to 'wheel' event when
    // viewport is "pinch zoomed"
    const wheelListener = (event) => {
        if (window.visualViewport) {
            const scale = window.visualViewport.scale || 1;
            if (scale > 1) {
                event.stopPropagation();
            }
        }
    };
    // prevent document from scrolling at all (iPhone)
    const scrollListener = (event) => {
        if (document.documentElement.scrollTop > 0) {
            document.documentElement.scrollTop = 0;
            event.preventDefault();
        }
    };
    const errorListener = (event) => {
        const { error } = event;
        if (error) {
            // const message = `${error.message}\n\n${JSON.stringify(error.stack, null, 2)}`
            const message = `${error.message}`;
            (0, showNotification_1.showNotification)(system, message, {
                color: theme_1.COLOR_RED,
                borderColor: theme_1.COLOR_RED,
            });
        }
    };
    const dragOverListener = (event) => {
        event.preventDefault();
    };
    const dropListener = (event) => {
        event.preventDefault();
    };
    root.addEventListener('blur', blurListener, true);
    root.addEventListener('keydown', keyDownListener, true);
    root.addEventListener('keyup', keyUpListener, true);
    root.addEventListener('wheel', wheelListener, {
        capture: true,
        passive: true,
    });
    root.addEventListener('dblclick', dbClickListener);
    root.addEventListener('dragover', dragOverListener);
    root.addEventListener('drop', dropListener);
    window.addEventListener('error', errorListener);
    return () => {
        root.removeEventListener('blur', blurListener, true);
        root.removeEventListener('keydown', keyDownListener, true);
        root.removeEventListener('keyup', keyUpListener, true);
        root.removeEventListener('wheel', wheelListener, {
            capture: true,
        });
        root.removeEventListener('dblclick', dbClickListener);
        root.removeEventListener('dragover', dragOverListener);
        root.removeEventListener('drop', dropListener);
        window.removeEventListener('error', errorListener);
        document.removeEventListener('scroll', scrollListener, { capture: true });
    };
}
//# sourceMappingURL=init.js.map