"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useKeyboardNavigation = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const useGlobalEvent_1 = require("../../components/utils/useGlobalEvent");
const useKeyboardNavigation = (setCurrentSection, sections) => {
    const handleKeyPress = React.useCallback((event) => {
        if ((event.ctrlKey || event.metaKey) &&
            (event.code === 'ArrowUp' || event.code === 'ArrowDown')) {
            const isKeyUp = event.code === 'ArrowUp';
            const validIndexes = sections.reduce((acc, item, index) => {
                if (item !== '-') {
                    acc.push(index);
                }
                return acc;
            }, []);
            setCurrentSection((currentActiveSection) => {
                let nextActiveSection = null;
                const currentActiveInList = validIndexes.indexOf(currentActiveSection);
                if (isKeyUp) {
                    const candidate = validIndexes[currentActiveInList - 1];
                    nextActiveSection =
                        typeof candidate === 'number' ? candidate : validIndexes[validIndexes.length - 1];
                }
                else {
                    const candidate = validIndexes[currentActiveInList + 1];
                    nextActiveSection = typeof candidate === 'number' ? candidate : validIndexes[0];
                }
                return nextActiveSection;
            });
        }
    }, [sections, setCurrentSection]);
    (0, useGlobalEvent_1.useGlobalEvent)('keydown', handleKeyPress);
};
exports.useKeyboardNavigation = useKeyboardNavigation;
