"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "useList", {
    enumerable: true,
    get: function() {
        return useList;
    }
});
const _react = require("react");
const _hooks = require("../../../hooks");
const _pathReducer = require("../reducers/pathReducer");
const _focusedPathReducer = require("../reducers/focusedPathReducer");
const useList = ({ alwaysOpened, disabled, onToggle, closeOnOverlayClick, floatingPopoverRef, listWrapperRef })=>{
    const initialPath = alwaysOpened ? [
        'root'
    ] : [];
    const [path, dispatchPath] = (0, _react.useReducer)(_pathReducer.pathReducer, initialPath);
    const [focusedPath, dispatchFocusedPath] = (0, _react.useReducer)(_focusedPathReducer.focusedPathReducer, []);
    /* Callback для глобального открытия/закрытия списка */ const handleGlobalToggle = (opened, event)=>{
        if (disabled) {
            return;
        }
        if (alwaysOpened || opened) {
            dispatchPath({
                type: 'opened_first_level'
            });
        } else {
            dispatchFocusedPath({
                type: 'reset'
            });
            dispatchPath({
                type: 'reset'
            });
        }
        if (onToggle) {
            onToggle(opened, event);
        }
    };
    /* Логика работы при клике за пределами выпадающего списка */ (0, _hooks.useOutsideClick)((event)=>{
        if (!path[0] || !closeOnOverlayClick || alwaysOpened) {
            return;
        }
        dispatchPath({
            type: 'reset'
        });
        dispatchFocusedPath({
            type: 'reset'
        });
        if (onToggle) {
            onToggle(false, event);
        }
    }, [
        floatingPopoverRef,
        listWrapperRef
    ]);
    return {
        path,
        dispatchPath,
        focusedPath,
        dispatchFocusedPath,
        handleGlobalToggle
    };
};
