"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "useCheckedState", {
    enumerable: true,
    get: function() {
        return useCheckedState;
    }
});
const _react = require("react");
const _utils = require("../../../utils");
const _utils1 = require("../utils");
const useCheckedState = ({ multiselect, mode, value, valueToCheckedMap, valueToItemMap, closeAfterSelect, handleChange, resetListState })=>{
    const [checked, setChecked] = (0, _react.useState)(valueToCheckedMap);
    const handleCheckboxChange = (item)=>{
        if (!multiselect) {
            return;
        }
        const checkedCopy = new Map(checked);
        switch(checkedCopy.get(item.value)){
            case 'indeterminate':
                {
                    (0, _utils1.updateDescendants)(item, checkedCopy, true, valueToItemMap);
                    break;
                }
            case true:
                {
                    (0, _utils1.updateDescendants)(item, checkedCopy, false, valueToItemMap);
                    checkedCopy.set(item.value, false);
                    break;
                }
            case false:
                {
                    (0, _utils1.updateDescendants)(item, checkedCopy, true, valueToItemMap);
                    checkedCopy.set(item.value, true);
                    break;
                }
            default:
                {
                    break;
                }
        }
        const newValues = [];
        valueToItemMap.forEach((currentItem, currentValue)=>{
            if (checkedCopy.get(currentValue)) {
                newValues.push(currentItem.value);
            }
        });
        if (Array.isArray(value)) {
            value.forEach((currentValue)=>{
                if (!valueToItemMap.has(currentValue)) {
                    newValues.push(currentValue);
                }
            });
        }
        if (closeAfterSelect) {
            resetListState();
        }
        handleChange(newValues, item);
    };
    const handleItemClick = (item, event)=>{
        if (!(0, _utils.isEmpty)(item.items)) {
            return;
        }
        if (multiselect) {
            handleCheckboxChange(item);
            return;
        }
        event?.stopPropagation();
        const isCurrentChecked = checked.get(item.value);
        if (closeAfterSelect) {
            resetListState();
        }
        if (mode === 'radio' && isCurrentChecked) {
            return;
        }
        handleChange(isCurrentChecked ? '' : item.value, item);
    };
    const handlePressDown = (item, event)=>{
        if ((0, _utils.isEmpty)(item.items)) {
            handleItemClick(item, event);
            return;
        }
        if (multiselect) {
            handleCheckboxChange(item);
        }
    };
    (0, _react.useLayoutEffect)(()=>{
        setChecked((0, _utils1.syncCheckedState)(value, valueToCheckedMap, valueToItemMap));
    }, [
        value,
        valueToCheckedMap,
        valueToItemMap
    ]);
    return {
        checked,
        handleCheckboxChange,
        handleItemClick,
        handlePressDown
    };
};
