"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 _classnames = /*#__PURE__*/ _interop_require_default(require("classnames"));
const _Dropdowncontext = require("../../../../Dropdown.context");
const _utils = require("../../../../utils");
const _Dropdowntokens = require("../../../../Dropdown.tokens");
const _Itemstyles = require("./Item.styles");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
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, path, currentLevel, index, ariaControls, ariaExpanded, ariaLevel, ariaLabel })=>{
    const { value, label, disabled, contentLeft, contentRight, dividerBefore, dividerAfter, className, ...rest } = item;
    const ref = (0, _react.useRef)(null);
    const { focusedPath, size, variant, itemRole, handleGlobalToggle, closeOnSelect, onHover, onItemSelect, treeId, renderItem } = (0, _react.useContext)(_Dropdowncontext.Context);
    const hasDescendants = Boolean(item.items?.length);
    const disclosureIconSize = size === 'xs' ? 'xs' : 's';
    const isDisabledClassName = disabled ? _Dropdowntokens.classes.dropdownItemIsDisabled : undefined;
    const focusedClass = currentLevel === focusedPath.length - 1 && index === focusedPath?.[currentLevel] ? _Dropdowntokens.classes.dropdownItemIsFocused : undefined;
    const activeClass = value === path?.[currentLevel + 1] ? _Dropdowntokens.classes.dropdownItemIsActive : undefined;
    (0, _react.useEffect)(()=>{
        if (focusedClass && ref?.current) {
            ref.current.scrollIntoView({
                behavior: 'smooth',
                block: 'center',
                inline: 'center'
            });
        }
    }, [
        focusedClass
    ]);
    const handleClick = (event)=>{
        if (disabled) {
            event.stopPropagation();
            return;
        }
        if (onItemSelect) {
            onItemSelect(item, event);
        }
        // Закрываем весь дропдаун целиком при клике на айтем без потомков. Только при closeOnSelect === true.
        if (closeOnSelect && !hasDescendants) {
            handleGlobalToggle(false, event);
        }
    };
    const handleHover = ()=>{
        if (onHover) {
            onHover(index, item);
        }
    };
    return /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, dividerBefore && /*#__PURE__*/ _react.default.createElement(_Itemstyles.Divider, {
        variant: variant
    }), /*#__PURE__*/ _react.default.createElement(_Itemstyles.Wrapper, {
        ...rest,
        ref: ref,
        className: (0, _classnames.default)(isDisabledClassName, focusedClass, activeClass, className),
        id: (0, _utils.getItemId)(treeId, value.toString()),
        role: itemRole,
        onClick: handleClick,
        onMouseEnter: handleHover,
        variant: variant,
        "aria-disabled": disabled,
        "aria-controls": ariaControls,
        "aria-expanded": ariaExpanded,
        "aria-level": ariaLevel,
        "aria-label": ariaLabel
    }, renderItem ? /*#__PURE__*/ _react.default.createElement(_Itemstyles.RenderItemWrapper, null, renderItem(item)) : /*#__PURE__*/ _react.default.createElement(_Itemstyles.CellWrapper, null, /*#__PURE__*/ _react.default.createElement(_Itemstyles.StyledCell, {
        contentLeft: contentLeft,
        contentRight: contentRight,
        alignContentLeft: "center",
        alignContentRight: "center",
        title: label
    })), hasDescendants && /*#__PURE__*/ _react.default.createElement(_Itemstyles.DisclosureIconWrapper, null, /*#__PURE__*/ _react.default.createElement(_Itemstyles.StyledIconDisclosureRight, {
        size: disclosureIconSize,
        color: "inherit"
    }))), dividerAfter && /*#__PURE__*/ _react.default.createElement(_Itemstyles.Divider, {
        variant: variant
    }));
};
