"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useTabList = useTabList;
const react_1 = require("react");
const roving_focus_1 = require("../../../utils/helpers/roving-focus");
const Tabs_context_1 = require("../../Tabs.context");
const TAB_SELECTOR = "[data-aksel-tab]:not([data-disabled])";
/**
 * TabList hook to manage multiple tab buttons,
 * and ensures only one tab is selected at a time.
 */
function useTabList() {
    const { loop, selectedValue, setFocusedValue } = (0, Tabs_context_1.useTabsContext)();
    /**
     * Implements roving-tabindex for horizontal tabs.
     */
    const onKeyDown = (0, react_1.useCallback)((event) => {
        const container = event.currentTarget;
        const current = event.target.closest(TAB_SELECTOR);
        const keyMap = {
            ArrowLeft: () => (0, roving_focus_1.rovingFocus)(TAB_SELECTOR, container, "prev", current, loop),
            ArrowRight: () => (0, roving_focus_1.rovingFocus)(TAB_SELECTOR, container, "next", current, loop),
            Home: () => (0, roving_focus_1.rovingFocus)(TAB_SELECTOR, container, "first"),
            End: () => (0, roving_focus_1.rovingFocus)(TAB_SELECTOR, container, "last"),
        };
        const hasModifiers = event.shiftKey || event.ctrlKey || event.altKey || event.metaKey;
        const action = keyMap[event.key];
        if (action && !hasModifiers) {
            event.preventDefault();
            action();
        }
        else if (event.key === "Tab") {
            /**
             * Imperative focus during keydown is risky so we prevent React's batching updates
             * to avoid potential bugs. See: https://github.com/facebook/react/issues/20332
             */
            selectedValue && setTimeout(() => setFocusedValue(selectedValue));
        }
    }, [loop, selectedValue, setFocusedValue]);
    return { onKeyDown };
}
//# sourceMappingURL=useTabList.js.map