function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
function _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function ownKeys(object, enumerableOnly) {
    var keys = Object.keys(object);
    if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);
        if (enumerableOnly) {
            symbols = symbols.filter(function(sym) {
                return Object.getOwnPropertyDescriptor(object, sym).enumerable;
            });
        }
        keys.push.apply(keys, symbols);
    }
    return keys;
}
function _object_spread_props(target, source) {
    source = source != null ? source : {};
    if (Object.getOwnPropertyDescriptors) {
        Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
    } else {
        ownKeys(Object(source)).forEach(function(key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
        });
    }
    return target;
}
function _object_without_properties(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys, key, i;
    if (typeof Reflect !== "undefined" && Reflect.ownKeys) {
        sourceKeys = Reflect.ownKeys(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
        return target;
    }
    target = _object_without_properties_loose(source, excluded);
    if (Object.getOwnPropertySymbols) {
        sourceKeys = Object.getOwnPropertySymbols(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
    }
    return target;
}
function _object_without_properties_loose(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys = Object.getOwnPropertyNames(source), key, i;
    for(i = 0; i < sourceKeys.length; i++){
        key = sourceKeys[i];
        if (excluded.indexOf(key) >= 0) continue;
        if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
        target[key] = source[key];
    }
    return target;
}
import React, { useRef, useContext } from "react";
import { cx, isEmpty } from "../../../../../../utils";
import { useDidMountEffect } from "../../../../../../hooks";
import { mapCheckedVariants } from "../../../../../Combobox/ComboboxNew/utils";
import { sizeToIconSize, getItemId } from "../../../../utils";
import { classes } from "../../../../Select.tokens";
import { Context } from "../../../../Select.context";
import { StyledCell, StyledCheckbox, StyledIndicator, DisclosureIconWrapper, IconWrapper, StyledText, Wrapper, StyledCheckboxWrapper, StyledArrow, StyledIconDone } from "./Item.styles";
export var Item = function Item(param) {
    var item = param.item, path = param.path, currentLevel = param.currentLevel, index = param.index, ariaControls = param.ariaControls, ariaExpanded = param.ariaExpanded, ariaLevel = param.ariaLevel, ariaLabel = param.ariaLabel;
    var value = item.value, label = item.label, disabled = item.disabled, isDisabled = item.isDisabled, contentLeft = item.contentLeft, contentRight = item.contentRight, className = item.className, rest = _object_without_properties(item, [
        "value",
        "label",
        "disabled",
        "isDisabled",
        "contentLeft",
        "contentRight",
        "className"
    ]);
    var ref = useRef(null);
    var _useContext = useContext(Context), focusedPath = _useContext.focusedPath, checked = _useContext.checked, multiselect = _useContext.multiselect, size = _useContext.size, handleCheckboxChange = _useContext.handleCheckboxChange, handleItemClick = _useContext.handleItemClick, variant = _useContext.variant, renderItem = _useContext.renderItem, renderSelectionIcon = _useContext.renderSelectionIcon, treeId = _useContext.treeId, singleLine = _useContext.singleLine, // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore
    _checkboxAppearance = _useContext._checkboxAppearance;
    var checkedValue = checked.get(item.value) || false;
    var itemDisabled = Boolean(disabled || isDisabled);
    var disabledClassName = itemDisabled ? classes.dropdownItemIsDisabled : undefined;
    var focusedClass = currentLevel === focusedPath.length - 1 && index === (focusedPath === null || focusedPath === void 0 ? void 0 : focusedPath[currentLevel]) ? classes.dropdownItemIsFocused : undefined;
    var activeClass = value === (path === null || path === void 0 ? void 0 : path[currentLevel + 1]) ? classes.dropdownItemIsActive : undefined;
    useDidMountEffect(function() {
        if (focusedClass && (ref === null || ref === void 0 ? void 0 : ref.current)) {
            ref.current.scrollIntoView({
                behavior: 'smooth',
                block: 'center',
                inline: 'center'
            });
        }
    }, [
        focusedClass
    ]);
    var handleChange = function handleChange(e) {
        if (itemDisabled) return;
        e.stopPropagation();
        handleCheckboxChange(item);
    };
    var handleClick = function handleClick(e) {
        if (itemDisabled) {
            e.stopPropagation();
            return;
        }
        handleItemClick(item, e);
    };
    return /*#__PURE__*/ React.createElement(Wrapper, _object_spread_props(_object_spread({}, rest), {
        className: cx(disabledClassName, focusedClass, activeClass, className),
        id: getItemId(treeId, value.toString()),
        ref: ref,
        onClick: handleClick,
        variant: variant,
        role: "treeitem",
        "aria-controls": ariaControls,
        "aria-expanded": ariaExpanded,
        "aria-level": ariaLevel,
        "aria-label": ariaLabel,
        "aria-selected": Boolean(checkedValue)
    }), /*#__PURE__*/ React.createElement(IconWrapper, {
        variant: variant
    }, renderSelectionIcon ? renderSelectionIcon(mapCheckedVariants(checkedValue, multiselect)) : /*#__PURE__*/ React.createElement(React.Fragment, null, multiselect && /*#__PURE__*/ React.createElement(StyledCheckboxWrapper, {
        onClick: function onClick(e) {
            return e.stopPropagation();
        }
    }, /*#__PURE__*/ React.createElement(StyledCheckbox, {
        disabled: itemDisabled,
        checked: Boolean(checkedValue),
        indeterminate: checkedValue === 'indeterminate',
        onChange: handleChange,
        appearance: _checkboxAppearance !== null && _checkboxAppearance !== void 0 ? _checkboxAppearance : 'default'
    })), !multiselect && checkedValue === 'dot' && /*#__PURE__*/ React.createElement(StyledIndicator, {
        size: "s",
        view: "default"
    }), !multiselect && checkedValue === 'done' && /*#__PURE__*/ React.createElement(StyledIconDone, {
        size: sizeToIconSize(size, variant),
        color: "inherit"
    }))), renderItem ? /*#__PURE__*/ React.createElement(StyledText, null, renderItem(item)) : /*#__PURE__*/ React.createElement(StyledCell, {
        contentLeft: contentLeft,
        contentRight: contentRight,
        title: label,
        className: singleLine ? classes.singleLineMode : ''
    }), !isEmpty(item.items) && /*#__PURE__*/ React.createElement(DisclosureIconWrapper, null, /*#__PURE__*/ React.createElement(StyledArrow, {
        size: sizeToIconSize(size, variant),
        color: "inherit"
    })));
};
