"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "Item", {
    enumerable: true,
    get: function() {
        return Item;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _utils = require("../../../../../../utils");
const _hooks = require("../../../../../../hooks");
const _Selecttokens = require("../../../../Select.tokens");
const _utils1 = require("../../../../utils");
const _treePathReducer = require("../../../../reducers/treePathReducer");
const _Selectcontext = require("../../../../Select.context");
const _Itemstyles = require("./Item.styles");
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const Item = ({ item, pathToItem })=>{
    const { label, value, disabled, contentLeft, contentRight } = item;
    const isLeaf = !item?.items;
    const { checked, multiselect, size, variant, renderItem, _checkboxAppearance, focusedPath, treePath, dispatchTreePath, arrowPlacement, handleItemClick, handleCheckboxChange, valueToPathMap, singleLine } = (0, _Selectcontext.useSelectContext)();
    const disabledClassName = disabled ? _Selecttokens.classes.dropdownItemIsDisabled : undefined;
    const ref = (0, _react.useRef)(null);
    const currentItemDepth = (valueToPathMap.get(item.value.toString())?.length || 0) - 1;
    const isCurrentLevelOpened = (0, _treePathReducer.keyExists)(treePath, valueToPathMap.get(value.toString()) || []);
    const withArrowInverse = isCurrentLevelOpened ? _Selecttokens.classes.arrowInverse : undefined;
    const focusedClass = (0, _utils.isArraysEqual)(pathToItem, focusedPath) ? _Selecttokens.classes.dropdownItemIsFocused : undefined;
    const handleClick = (e)=>{
        if (disabled) {
            return;
        }
        if (!isLeaf) {
            dispatchTreePath({
                type: 'toggled_level',
                value: valueToPathMap.get(value.toString()) || []
            });
        }
        if (handleItemClick) {
            handleItemClick(item, e);
        }
    };
    const handleChange = (e)=>{
        if (disabled) {
            return;
        }
        e.stopPropagation();
        handleCheckboxChange(item);
    };
    (0, _hooks.useDidMountEffect)(()=>{
        if (focusedClass && ref?.current) {
            ref.current.scrollIntoView({
                behavior: 'smooth',
                block: 'center',
                inline: 'center'
            });
        }
    }, [
        focusedClass
    ]);
    return /*#__PURE__*/ _react.default.createElement(_Itemstyles.ItemWrapper, null, /*#__PURE__*/ _react.default.createElement(_Itemstyles.Wrapper, {
        ref: ref,
        className: (0, _utils.cx)(focusedClass, disabledClassName),
        onClick: handleClick,
        variant: variant,
        role: "treeitem"
    }, /*#__PURE__*/ _react.default.createElement(_Itemstyles.Offset, {
        depth: currentItemDepth
    }), arrowPlacement === 'left' && /*#__PURE__*/ _react.default.createElement(_Itemstyles.DisclosureIconWrapper, {
        visibility: isLeaf ? 'hidden' : 'visible'
    }, /*#__PURE__*/ _react.default.createElement(_Itemstyles.StyledArrowLeft, {
        size: (0, _utils1.sizeToIconSize)(size, variant),
        color: "inherit",
        className: withArrowInverse
    })), multiselect && /*#__PURE__*/ _react.default.createElement(_Itemstyles.IconWrapper, {
        variant: variant
    }, /*#__PURE__*/ _react.default.createElement(_Itemstyles.StyledCheckboxWrapper, {
        onClick: (e)=>e.stopPropagation()
    }, /*#__PURE__*/ _react.default.createElement(_Itemstyles.StyledCheckbox, {
        disabled: disabled,
        checked: Boolean(checked.get(item.value)),
        indeterminate: checked.get(item.value) === 'indeterminate',
        onChange: handleChange,
        appearance: _checkboxAppearance ?? 'default'
    }))), !multiselect && arrowPlacement === 'right' && /*#__PURE__*/ _react.default.createElement(_Itemstyles.IconWrapper, {
        variant: variant
    }, checked.get(item.value) === 'dot' && /*#__PURE__*/ _react.default.createElement(_Itemstyles.StyledIndicator, {
        size: "s",
        view: "default"
    }), checked.get(item.value) === 'done' && /*#__PURE__*/ _react.default.createElement(_Itemstyles.StyledIconDone, {
        size: (0, _utils1.sizeToIconSize)(size, variant),
        color: "inherit"
    })), renderItem ? /*#__PURE__*/ _react.default.createElement(_Itemstyles.StyledText, null, renderItem(item)) : /*#__PURE__*/ _react.default.createElement(_Itemstyles.StyledCell, {
        contentLeft: contentLeft,
        contentRight: contentRight,
        title: label,
        className: singleLine ? _Selecttokens.classes.singleLineMode : ''
    }), !multiselect && arrowPlacement === 'left' && /*#__PURE__*/ _react.default.createElement(_Itemstyles.IconWrapper, {
        variant: variant
    }, checked.get(item.value) === 'dot' && /*#__PURE__*/ _react.default.createElement(_Itemstyles.StyledIndicator, {
        size: "s",
        view: "default"
    }), checked.get(item.value) === 'done' && /*#__PURE__*/ _react.default.createElement(_Itemstyles.StyledIconDone, {
        size: (0, _utils1.sizeToIconSize)(size, variant),
        color: "inherit"
    })), arrowPlacement === 'right' && /*#__PURE__*/ _react.default.createElement(_Itemstyles.DisclosureIconWrapper, {
        visibility: isLeaf ? 'hidden' : 'visible'
    }, /*#__PURE__*/ _react.default.createElement(_Itemstyles.StyledArrowRight, {
        size: (0, _utils1.sizeToIconSize)(size, variant),
        color: "inherit",
        className: withArrowInverse
    }))), !isLeaf && isCurrentLevelOpened && /*#__PURE__*/ _react.default.createElement(_Itemstyles.ChildItems, null, item.items?.map((item, index)=>{
        return /*#__PURE__*/ _react.default.createElement(Item, {
            item: item,
            key: item.value,
            pathToItem: [
                ...pathToItem,
                index
            ]
        });
    })));
};
