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;
};
import React, { forwardRef, useCallback, useEffect, useRef } from "react";
import { composeEventHandlers } from "../../../helpers/index.js";
import { rovingFocus } from "../../../helpers/roving-focus.js";
import { useEventCallback, useMergeRefs } from "../../../hooks/index.js";
import { Slot } from "../../slot/Slot.js";
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 = forwardRef((_a, ref) => {
    var { children, asChild, onKeyDown, onEntryFocus, onMouseDown, onFocus } = _a, rest = __rest(_a, ["children", "asChild", "onKeyDown", "onEntryFocus", "onMouseDown", "onFocus"]);
    const _ref = React.useRef(null);
    const composedRefs = useMergeRefs(ref, _ref);
    const handleEntryFocus = useEventCallback(onEntryFocus);
    const isMouseFocusRef = useRef(false);
    useEffect(() => {
        const node = _ref.current;
        if (node) {
            node.addEventListener(ENTRY_FOCUS, handleEntryFocus);
            return () => node.removeEventListener(ENTRY_FOCUS, handleEntryFocus);
        }
    }, [handleEntryFocus]);
    const handleKeyDown = useCallback((event) => {
        const container = _ref.current;
        if (!container) {
            return;
        }
        const current = event.target.closest(MENU_ITEM_SELECTOR);
        const keyMap = {
            ArrowUp: () => rovingFocus(MENU_ITEM_SELECTOR, container, "prev", current, false),
            ArrowDown: () => rovingFocus(MENU_ITEM_SELECTOR, container, "next", current, false),
            Home: () => rovingFocus(MENU_ITEM_SELECTOR, container, "first"),
            End: () => rovingFocus(MENU_ITEM_SELECTOR, container, "last"),
        };
        const action = keyMap[event.key];
        if (action) {
            event.preventDefault();
            action();
        }
    }, []);
    const Comp = asChild ? Slot : "div";
    return (React.createElement(Comp, Object.assign({ ref: composedRefs }, rest, { tabIndex: 0, style: Object.assign({ outline: "none" }, rest.style), onKeyDown: composeEventHandlers(onKeyDown, handleKeyDown), onMouseDown: composeEventHandlers(onMouseDown, () => {
            isMouseFocusRef.current = true;
        }), onFocus: 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 && rovingFocus(MENU_ITEM_SELECTOR, container, "first");
                }
            }
            isMouseFocusRef.current = false;
        }) }), children));
});
export { RovingFocus };
//# sourceMappingURL=RovingFocus.js.map