'use strict';

var React = require('react');

const PORTAL_FOCUS_ATTRIBUTE_NAME = "data-atl-maintain-portal-focus";
const formFieldFocusAttribute = {
    [PORTAL_FOCUS_ATTRIBUTE_NAME]: "true",
};
function useFormFieldFocus(props) {
    const [focused, setFocused] = React.useState(false);
    function handleFocusIn() {
        setFocused(true);
    }
    function handleFocusOut() {
        setTimeout(() => {
            var _a, _b, _c;
            const focusedElementWithinWrapper = (_b = (_a = props.wrapperRef) === null || _a === void 0 ? void 0 : _a.current) === null || _b === void 0 ? void 0 : _b.contains(document === null || document === void 0 ? void 0 : document.activeElement);
            const focusException = (_c = document === null || document === void 0 ? void 0 : document.activeElement) === null || _c === void 0 ? void 0 : _c.closest(`[${PORTAL_FOCUS_ATTRIBUTE_NAME}='true']`);
            if (!focusedElementWithinWrapper && !focusException) {
                setFocused(false);
            }
        }, 1);
    }
    React.useEffect(() => {
        var _a, _b, _c, _d;
        (_b = (_a = props.wrapperRef) === null || _a === void 0 ? void 0 : _a.current) === null || _b === void 0 ? void 0 : _b.addEventListener("focusin", handleFocusIn);
        (_d = (_c = props.wrapperRef) === null || _c === void 0 ? void 0 : _c.current) === null || _d === void 0 ? void 0 : _d.addEventListener("focusout", handleFocusOut);
        return () => {
            var _a, _b, _c, _d;
            (_b = (_a = props.wrapperRef) === null || _a === void 0 ? void 0 : _a.current) === null || _b === void 0 ? void 0 : _b.removeEventListener("focusin", handleFocusIn);
            (_d = (_c = props.wrapperRef) === null || _c === void 0 ? void 0 : _c.current) === null || _d === void 0 ? void 0 : _d.removeEventListener("focusout", handleFocusOut);
        };
    }, []);
    return {
        focused,
    };
}

exports.formFieldFocusAttribute = formFieldFocusAttribute;
exports.useFormFieldFocus = useFormFieldFocus;
