import { useEffect } from "react";
import { useDispatch } from "react-redux";
import { actions } from "../../../platform/services/window/modules/actions";
import { FEAGlobals } from "../../../common/Globals";
/**
 * This hook adds support for navigating tabs through keyboard commands. This is done
 * through a global keyboard event handler on the window (so that, much like Chrome, a
 * user can switch tabs regardless of whether the tab region has focus).
 */
export const useKeyboardNavigation = (enableKeyboardNavigation) => {
    const dispatch = useDispatch();
    useEffect(() => {
        if (!enableKeyboardNavigation)
            return;
        window.addEventListener("keydown", (e) => {
            if (e.ctrlKey) {
                // On Mac or Windows, Ctrl+Tab changes tabs to the right
                // On Windows, Ctrl+PageDown changes tabs to the right
                if ((e.key === "Tab" && !e.shiftKey) || e.key === "PageDown") {
                    dispatch(actions.shiftPromotedTab(1));
                    e.preventDefault();
                    // On Mac or Windows, Ctrl+Shift+Tab changes tabs to the left
                    // On Windows, Ctrl+PageUp changes tabs to the left
                }
                else if (e.key === "PageUp" || (e.key === "Tab" && e.shiftKey)) {
                    dispatch(actions.shiftPromotedTab(-1));
                    e.preventDefault();
                }
            }
            else if (e.metaKey && e.altKey) {
                // On Mac, Cmd+Opt+Right changes tabs to the right
                if (e.key === "ArrowRight") {
                    dispatch(actions.shiftPromotedTab(1));
                    e.preventDefault();
                    // On Mac, Cmd+Opt+Left changes tabs to the left
                }
                else if (e.key === "ArrowLeft") {
                    dispatch(actions.shiftPromotedTab(-1));
                    e.preventDefault();
                }
            }
            else {
                /**
                 * Goal: If a user gets focus into the title bar (if they Alt+Tab or Cmd+Tab into the window),
                 * we want to let get into app's contents (browser view) part of the window using the keyboard alone.
                 * The UX for this is to tab from the title bar into the window. In other words, if the user presses
                 * the "Tab" key, move their focus to the browser view
                 */
                if (FEAGlobals.fin.isTitlebarWindow() && e.key === "Tab") {
                    // This magic moves focus to a browserView, if one exists
                    FEAGlobals.finsembleWindow.focus();
                }
            }
        });
    }, []);
};
//# sourceMappingURL=useKeyboardNavigation.js.map