"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "useFocusTrap", {
    enumerable: true,
    get: function() {
        return useFocusTrap;
    }
});
const _react = require("react");
const _focusManager = require("../utils/focusManager");
const _tabbable = require("../utils/tabbable");
const _scopeTab = require("../utils/scopeTab");
// Находим элемент для фокуса
const getFocusElement = (node, firstFocusSelector)=>{
    let focusElement = null;
    if (firstFocusSelector) {
        if (typeof firstFocusSelector === 'string') {
            focusElement = node.querySelector(firstFocusSelector);
        } else if (firstFocusSelector.current) {
            focusElement = firstFocusSelector.current;
        }
    }
    if (!focusElement) {
        const children = Array.from(node.querySelectorAll(_tabbable.focusSelector));
        focusElement = children.find((el)=>(0, _tabbable.isTabble)(el)) || null;
    }
    // Если ничего не нашлось, то может ли сама нода быть под фокусом
    if (!focusElement && (0, _tabbable.isFocusable)(node)) {
        focusElement = node;
    }
    return focusElement;
};
const processNode = (node, firstFocusSelector, ref)=>{
    if (ref?.current) {
        ref.current.removeEventListener('animationend', ()=>{
            processNode(node, firstFocusSelector, ref);
        });
    }
    const focusElement = getFocusElement(node, firstFocusSelector);
    if (focusElement) {
        focusElement.focus();
    }
};
const focusManager = new _focusManager.FocusManager();
const useFocusTrap = (active = true, firstFocusSelector, focusAfterNode, focusAfterAnimation, enableOnFirstMount)=>{
    const ref = (0, _react.useRef)();
    const setRef = (0, _react.useCallback)((node)=>{
        if (!active) {
            return;
        }
        if (ref.current) {
            focusManager.teardownScopedFocus();
            focusManager.returnFocus();
        }
        if ((active || enableOnFirstMount) && node) {
            focusManager.setupScopedFocus(node);
            focusManager.markForFocusAfter(focusAfterNode);
            // Delay processing the HTML node by a frame. This ensures focus is assigned correctly.
            setTimeout(()=>{
                if (ref?.current && node.ownerDocument && focusAfterAnimation) {
                    ref.current.addEventListener('animationend', ()=>{
                        processNode(node, firstFocusSelector, ref);
                    });
                } else if (node.ownerDocument) {
                    processNode(node, firstFocusSelector);
                }
            });
            ref.current = node;
            return;
        }
        ref.current = null;
    }, [
        active,
        enableOnFirstMount,
        firstFocusSelector
    ]);
    (0, _react.useEffect)(()=>{
        if (!active && !enableOnFirstMount) {
            return;
        }
        const handleKeyDown = (event)=>{
            if (event.key === 'Tab' && ref.current) {
                (0, _scopeTab.scopeTab)(ref.current, event, active);
                if (enableOnFirstMount) {
                    setTimeout(()=>{
                        focusManager.teardownScopedFocus();
                    });
                }
            }
        };
        document.addEventListener('keydown', handleKeyDown);
        return ()=>{
            document.removeEventListener('keydown', handleKeyDown);
        };
    }, [
        active,
        enableOnFirstMount
    ]);
    return setRef;
};
