"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "useListState", {
    enumerable: true,
    get: function() {
        return useListState;
    }
});
const _react = require("react");
const _reducers = require("../reducers");
const useListState = ({ disabled, readOnly, onToggle, multiselect, rootRef })=>{
    const [path, dispatchPath] = (0, _react.useReducer)(_reducers.pathReducer, []);
    const [treePath, dispatchTreePath] = (0, _react.useReducer)(_reducers.treePathReducer, {});
    const [focusedPath, dispatchFocusedPath] = (0, _react.useReducer)(_reducers.focusedPathReducer, []);
    const resetListState = ()=>{
        dispatchFocusedPath({
            type: 'reset'
        });
        dispatchPath({
            type: 'reset'
        });
        dispatchTreePath({
            type: 'reset'
        });
    };
    const handleListToggle = (opened)=>{
        if (disabled || readOnly) {
            return;
        }
        if (opened) {
            dispatchPath({
                type: 'opened_first_level'
            });
        } else {
            // При закрытии ресетим все состояния.
            resetListState();
            const chipsWrapperNode = rootRef?.current?.querySelector('.input-scrollable-wrapper');
            // И скроллим чипы влево (при multiselect).
            if (multiselect && chipsWrapperNode) {
                chipsWrapperNode.scrollLeft = 0;
            }
        }
        if (onToggle) {
            onToggle(opened);
        }
    };
    return {
        path,
        treePath,
        focusedPath,
        dispatchPath,
        dispatchTreePath,
        dispatchFocusedPath,
        handleListToggle,
        resetListState,
        isListOpened: Boolean(path[0])
    };
};
