import { jsx as _jsx } from "@emotion/react/jsx-runtime";
import { useEffect, useMemo, useState, useRef, useCallback, createContext, } from 'react';
import { ROOT_SCOPE } from '../../../consts/scopes';
export const HotkeysContext = createContext({
    activeScope: ROOT_SCOPE,
    setActiveScope: () => { },
    addHotkeyListener: () => { },
    removeHotkeyListener: () => { },
});
export const HotkeysProvider = ({ children, }) => {
    const listenersRef = useRef({});
    const [activeScope, setActiveScope] = useState(ROOT_SCOPE);
    const handleAddHotkeyListener = useCallback((scope, hotkey, callback) => {
        var _a;
        const hotkeyListeners = listenersRef.current;
        (_a = hotkeyListeners[scope]) !== null && _a !== void 0 ? _a : (hotkeyListeners[scope] = {
            cut: [],
            copy: [],
            paste: [],
            delete: [],
        });
        hotkeyListeners[scope][hotkey].push(callback);
    }, []);
    const handleRemoveHotkeyListener = useCallback((scope, hotkey, callback) => {
        const hotkeyListeners = listenersRef.current;
        if (!hotkeyListeners[scope]) {
            return;
        }
        hotkeyListeners[scope][hotkey] = hotkeyListeners[scope][hotkey].filter((fn) => fn !== callback);
    }, []);
    useEffect(() => {
        const handleHotkeyEvent = (hotkey) => (value) => {
            const { activeElement } = document;
            if ((activeElement === null || activeElement === void 0 ? void 0 : activeElement.tagName) === 'INPUT' ||
                (activeElement === null || activeElement === void 0 ? void 0 : activeElement.tagName) === 'TEXTAREA') {
                if (hotkey === 'paste') {
                    document.execCommand('insertText', undefined, value);
                }
                else {
                    document.execCommand(hotkey);
                }
                return;
            }
            const hotkeyListeners = listenersRef.current;
            if (!hotkeyListeners[activeScope]) {
                return;
            }
            hotkeyListeners[activeScope][hotkey].forEach((callback) => callback());
        };
        const cutUnsubscribe = window.electron.onCut(handleHotkeyEvent('cut'));
        const copyUnsubscribe = window.electron.onCopy(handleHotkeyEvent('copy'));
        const pasteUnsubscribe = window.electron.onPaste(handleHotkeyEvent('paste'));
        const deleteUnsubscribe = window.electron.onDelete(handleHotkeyEvent('delete'));
        return () => {
            cutUnsubscribe();
            copyUnsubscribe();
            pasteUnsubscribe();
            deleteUnsubscribe();
        };
    }, [activeScope]);
    const currentContext = useMemo(() => ({
        activeScope,
        setActiveScope,
        addHotkeyListener: handleAddHotkeyListener,
        removeHotkeyListener: handleRemoveHotkeyListener,
    }), [
        activeScope,
        setActiveScope,
        handleAddHotkeyListener,
        handleRemoveHotkeyListener,
    ]);
    return (_jsx(HotkeysContext.Provider, { value: currentContext, children: children }));
};
