"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RovingFocus = void 0;
const react_1 = __importStar(require("react"));
const helpers_1 = require("../../../helpers");
const roving_focus_1 = require("../../../helpers/roving-focus");
const hooks_1 = require("../../../hooks");
const Slot_1 = require("../../slot/Slot");
const MENU_ITEM_SELECTOR = "[data-aksel-menu-item]:not([data-disabled])";
const ENTRY_FOCUS = "rovingFocusGroup.onEntryFocus";
const EVENT_OPTIONS = { bubbles: false, cancelable: true };
const RovingFocus = (0, react_1.forwardRef)((_a, ref) => {
    var { children, asChild, onKeyDown, onEntryFocus, onMouseDown, onFocus } = _a, rest = __rest(_a, ["children", "asChild", "onKeyDown", "onEntryFocus", "onMouseDown", "onFocus"]);
    const _ref = react_1.default.useRef(null);
    const composedRefs = (0, hooks_1.useMergeRefs)(ref, _ref);
    const handleEntryFocus = (0, hooks_1.useEventCallback)(onEntryFocus);
    const isMouseFocusRef = (0, react_1.useRef)(false);
    (0, react_1.useEffect)(() => {
        const node = _ref.current;
        if (node) {
            node.addEventListener(ENTRY_FOCUS, handleEntryFocus);
            return () => node.removeEventListener(ENTRY_FOCUS, handleEntryFocus);
        }
    }, [handleEntryFocus]);
    const handleKeyDown = (0, react_1.useCallback)((event) => {
        const container = _ref.current;
        if (!container) {
            return;
        }
        const current = event.target.closest(MENU_ITEM_SELECTOR);
        const keyMap = {
            ArrowUp: () => (0, roving_focus_1.rovingFocus)(MENU_ITEM_SELECTOR, container, "prev", current, false),
            ArrowDown: () => (0, roving_focus_1.rovingFocus)(MENU_ITEM_SELECTOR, container, "next", current, false),
            Home: () => (0, roving_focus_1.rovingFocus)(MENU_ITEM_SELECTOR, container, "first"),
            End: () => (0, roving_focus_1.rovingFocus)(MENU_ITEM_SELECTOR, container, "last"),
        };
        const action = keyMap[event.key];
        if (action) {
            event.preventDefault();
            action();
        }
    }, []);
    const Comp = asChild ? Slot_1.Slot : "div";
    return (react_1.default.createElement(Comp, Object.assign({ ref: composedRefs }, rest, { tabIndex: 0, style: Object.assign({ outline: "none" }, rest.style), onKeyDown: (0, helpers_1.composeEventHandlers)(onKeyDown, handleKeyDown), onMouseDown: (0, helpers_1.composeEventHandlers)(onMouseDown, () => {
            isMouseFocusRef.current = true;
        }), onFocus: (0, helpers_1.composeEventHandlers)(onFocus, (event) => {
            if (event.target === event.currentTarget) {
                const entryFocusEvent = new CustomEvent(ENTRY_FOCUS, EVENT_OPTIONS);
                event.currentTarget.dispatchEvent(entryFocusEvent);
                if (!entryFocusEvent.defaultPrevented) {
                    const container = _ref.current;
                    container && (0, roving_focus_1.rovingFocus)(MENU_ITEM_SELECTOR, container, "first");
                }
            }
            isMouseFocusRef.current = false;
        }) }), children));
});
exports.RovingFocus = RovingFocus;
//# sourceMappingURL=RovingFocus.js.map