"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useLogic = void 0;
const useLogic = ({ value, level, item, isInitialExpanded, expandedLevel, chainToSelectedItem = [], disabledItems, onChange, isObjectMode, }) => {
    const { id, options } = item;
    const isItemSelected = () => {
        if (isObjectMode && typeof value === 'object') {
            return value?.id === id;
        }
        return value === id;
    };
    const isSelected = isItemSelected();
    const checkIsDefaultExpanded = () => {
        if (chainToSelectedItem.includes(id)) {
            return true;
        }
        if (options?.hasOwnProperty('isDefaultExpanded')) {
            return options.isDefaultExpanded;
        }
        return isInitialExpanded && level <= expandedLevel - 1;
    };
    const isDefaultExpanded = checkIsDefaultExpanded();
    const disabledItem = disabledItems?.find((formatDisabledItem) => formatDisabledItem.id === id);
    const isDisabled = Boolean(disabledItem);
    const disabledReason = disabledItem?.disabledReason;
    const nextLevel = level + 1;
    const handleChange = () => {
        if (isObjectMode) {
            return onChange?.(item);
        }
        return onChange?.(id);
    };
    return {
        isSelected,
        isDefaultExpanded,
        isDisabled,
        disabledReason,
        nextLevel,
        handleChange,
    };
};
exports.useLogic = useLogic;
