import { useDeepEffect } from "./useDeepEffect";
import { FEAGlobals } from "../../common/Globals";
/**
 * This hook can be used to register a global hotkey. It makes use of Finsemble's HotkeyClient API.
 *
 * @param keyCombination [Array<string>] - A hotkey Combination. If null or undefined then no operation will occur.
 * @param action Function - The function to call when the hotkey combination is pressed.
 */
export const useHotkey = (keyCombination, action) => {
    // We use a deep effect comparison because we expect arrays to be passed in.
    // React's useEffect() hook only performs shallow comparisons. We use the literal ["escape"] which would trigger an effect with every render.
    // useDeepEffect() is a custom hook that will compare the *contents* of the arrays. This way, two literal instances will still be perceived by React as identical.
    useDeepEffect(() => {
        let actionIsAFunction = typeof action === "function";
        if (!actionIsAFunction)
            console.error("useHotkey() invalid parameters: action is not a function");
        keyCombination && actionIsAFunction && FEAGlobals.FSBL.Clients.HotkeyClient.addGlobalHotkey(keyCombination, action);
        return function cleanup() {
            keyCombination &&
                actionIsAFunction &&
                FEAGlobals.FSBL.Clients.HotkeyClient.removeGlobalHotkey(keyCombination, action);
        };
    }, [keyCombination, action]);
};
//# sourceMappingURL=useHotkey.js.map