"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 _utils = require("../utils");
const _Comboboxtokens = require("../Combobox.tokens");
const useListState = ({ disabled, readOnly, onToggle, multiple, alwaysOpened, renderValue, rootRef, value, valueToItemMap, setTextValue })=>{
    const [path, dispatchPath] = (0, _react.useReducer)(_reducers.pathReducer, alwaysOpened ? [
        'root'
    ] : []);
    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();
            setTextValue((0, _utils.getTextValue)(multiple, value, valueToItemMap, renderValue));
            const chipsWrapperNode = rootRef?.current?.querySelector(`.${_Comboboxtokens.classes.inputScrollableWrapper}`);
            if (multiple && chipsWrapperNode) {
                chipsWrapperNode.scrollLeft = 0;
            }
        }
        if (onToggle) {
            onToggle(opened);
        }
    };
    return {
        path,
        treePath,
        focusedPath,
        dispatchPath,
        dispatchTreePath,
        dispatchFocusedPath,
        handleListToggle,
        resetListState,
        isListOpened: Boolean(path[0])
    };
};
