"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useAutoFocusOnSelect = void 0;
const react_1 = require("react");
const Debounce_1 = require("../util/Debounce");
const duration = 300;
const useAutoFocusOnSelect = ({ ref, onlyAutoOpenWhenInputEmpty = false, focusOnSelected = true, disableOpenOnFocus = false }) => {
    const [isFocused, setIsFocused] = (0, react_1.useState)(false);
    const [isDropdownOpen, setIsDropdownOpen] = (0, react_1.useState)(false);
    const lockOpen = (0, react_1.useRef)(false);
    const timeout = (0, react_1.useRef)();
    (0, react_1.useEffect)(() => clearTimeout(timeout.current), []);
    const debounceSetFocus = (0, react_1.useCallback)((0, Debounce_1.debounce)((value) => {
        setIsFocused(value);
    }, duration), []);
    const onTriggerClicked = () => {
        setIsDropdownOpen(true);
        debounceSetFocus(true);
    };
    const onDropdownClosed = (e) => {
        const composedPath = e.composedPath();
        if (composedPath.some((el) => { var _a; return (_a = el.isSameNode) === null || _a === void 0 ? void 0 : _a.call(el, ref); })) {
            return;
        }
        if (!lockOpen.current) {
            setIsDropdownOpen(false);
        }
        debounceSetFocus(false);
    };
    const onItemSelected = () => {
        if (!lockOpen.current)
            setIsDropdownOpen(false);
        if (focusOnSelected)
            ref === null || ref === void 0 ? void 0 : ref.focus();
        else
            ref === null || ref === void 0 ? void 0 : ref.blur();
    };
    const _onFocus = (0, react_1.useCallback)(() => {
        if (!isFocused) {
            if (!onlyAutoOpenWhenInputEmpty || !(ref === null || ref === void 0 ? void 0 : ref.value)) {
                setIsDropdownOpen(!disableOpenOnFocus);
                lockOpen.current = true;
                timeout.current = setTimeout(() => {
                    lockOpen.current = false;
                }, duration);
            }
        }
        debounceSetFocus(true);
    }, [setIsFocused, isFocused, isDropdownOpen, setIsDropdownOpen, disableOpenOnFocus]);
    const _onBlur = (0, react_1.useCallback)(() => {
        debounceSetFocus(false);
    }, [setIsFocused]);
    (0, react_1.useEffect)(() => {
        if (!ref)
            return;
        ref.addEventListener('focus', _onFocus);
        ref.addEventListener('blur', _onBlur);
        ref.addEventListener('click', _onFocus);
        return () => {
            if (!ref)
                return;
            ref.removeEventListener('focus', _onFocus);
            ref.removeEventListener('blur', _onBlur);
            ref.removeEventListener('click', _onFocus);
        };
    }, [ref, _onFocus, _onBlur]);
    return {
        isFocused,
        isDropdownOpen,
        onDropdownClosed,
        onTriggerClicked,
        onItemSelected,
        setIsDropdownOpen,
    };
};
exports.useAutoFocusOnSelect = useAutoFocusOnSelect;
//# sourceMappingURL=AutoFocusOnSelect.js.map