function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_without_holes(arr) {
    if (Array.isArray(arr)) return _array_like_to_array(arr);
}
function _iterable_to_array(iter) {
    if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
function _non_iterable_spread() {
    throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _to_consumable_array(arr) {
    return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
import React, { useContext, useRef } from "react";
import { useDidMountEffect } from "../../../../../../../hooks";
import { isArraysEqual, cx } from "../../../../../../../utils";
import { sizeToIconSize } from "../../../../utils";
import { classes } from "../../../../Combobox.tokens";
import { keyExists } from "../../../../reducers/treePathReducer";
import { Context } from "../../../../Combobox.context";
import { ItemWrapper, ChildItems, Offset, IconWrapper, StyledCheckboxWrapper, StyledCheckbox, StyledIndicator, StyledIconDone, StyledText, StyledCell, DisclosureIconWrapper, StyledArrowRight, Wrapper, StyledArrowLeft } from "./Item.styles";
export var Item = function Item1(param) {
    var item = param.item, pathToItem = param.pathToItem;
    var _valueToPathMap_get, _item_items;
    var label = item.label, value = item.value, disabled = item.disabled, contentLeft = item.contentLeft, contentRight = item.contentRight;
    var isLeaf = !(item === null || item === void 0 ? void 0 : item.items);
    var _useContext = useContext(Context), checked = _useContext.checked, multiple = _useContext.multiple, size = _useContext.size, variant = _useContext.variant, renderItem = _useContext.renderItem, // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore
    _checkboxAppearance = _useContext._checkboxAppearance, focusedPath = _useContext.focusedPath, treePath = _useContext.treePath, dispatchTreePath = _useContext.dispatchTreePath, arrowPlacement = _useContext.arrowPlacement, handleItemClick = _useContext.handleItemClick, handleCheckboxChange = _useContext.handleCheckboxChange, valueToPathMap = _useContext.valueToPathMap, singleLine = _useContext.singleLine;
    var ref = useRef(null);
    var itemDisabled = disabled;
    var disabledClassName = itemDisabled ? classes.dropdownItemIsDisabled : undefined;
    var currentItemDepth = (((_valueToPathMap_get = valueToPathMap.get(item.value.toString())) === null || _valueToPathMap_get === void 0 ? void 0 : _valueToPathMap_get.length) || 0) - 1;
    var isCurrentLevelOpened = keyExists(treePath, valueToPathMap.get(value.toString()) || []);
    var withArrowInverse = isCurrentLevelOpened ? classes.arrowInverse : undefined;
    var focusedClass = isArraysEqual(pathToItem, focusedPath) ? classes.dropdownItemIsFocused : undefined;
    var handleClick = function handleClick(e) {
        if (itemDisabled) return;
        if (!isLeaf) {
            dispatchTreePath({
                type: 'toggled_level',
                value: valueToPathMap.get(value.toString()) || []
            });
        }
        if (handleItemClick) {
            handleItemClick(item, e);
        }
    };
    var handleChange = function handleChange(e) {
        if (itemDisabled) return;
        e.stopPropagation();
        handleCheckboxChange(item);
    };
    useDidMountEffect(function() {
        if (focusedClass && (ref === null || ref === void 0 ? void 0 : ref.current)) {
            ref.current.scrollIntoView({
                behavior: 'smooth',
                block: 'center',
                inline: 'center'
            });
        }
    }, [
        focusedClass
    ]);
    return /*#__PURE__*/ React.createElement(ItemWrapper, null, /*#__PURE__*/ React.createElement(Wrapper, {
        ref: ref,
        className: cx(focusedClass, disabledClassName),
        onClick: handleClick,
        variant: variant,
        role: "treeitem"
    }, /*#__PURE__*/ React.createElement(Offset, {
        depth: currentItemDepth
    }), arrowPlacement === 'left' && /*#__PURE__*/ React.createElement(DisclosureIconWrapper, {
        visibility: isLeaf ? 'hidden' : 'visible'
    }, /*#__PURE__*/ React.createElement(StyledArrowLeft, {
        size: sizeToIconSize(size, variant),
        color: "inherit",
        className: withArrowInverse
    })), multiple && /*#__PURE__*/ React.createElement(IconWrapper, {
        variant: variant
    }, /*#__PURE__*/ React.createElement(StyledCheckboxWrapper, {
        onClick: function onClick(e) {
            return e.stopPropagation();
        }
    }, /*#__PURE__*/ React.createElement(StyledCheckbox, {
        disabled: itemDisabled,
        checked: Boolean(checked.get(item.value)),
        indeterminate: checked.get(item.value) === 'indeterminate',
        onChange: handleChange,
        appearance: _checkboxAppearance !== null && _checkboxAppearance !== void 0 ? _checkboxAppearance : 'default'
    }))), !multiple && arrowPlacement === 'right' && /*#__PURE__*/ React.createElement(IconWrapper, {
        variant: variant
    }, checked.get(item.value) === 'dot' && /*#__PURE__*/ React.createElement(StyledIndicator, {
        size: "s",
        view: "default"
    }), checked.get(item.value) === '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 : ''
    }), !multiple && arrowPlacement === 'left' && /*#__PURE__*/ React.createElement(IconWrapper, {
        variant: variant
    }, checked.get(item.value) === 'dot' && /*#__PURE__*/ React.createElement(StyledIndicator, {
        size: "s",
        view: "default"
    }), checked.get(item.value) === 'done' && /*#__PURE__*/ React.createElement(StyledIconDone, {
        size: sizeToIconSize(size, variant),
        color: "inherit"
    })), arrowPlacement === 'right' && /*#__PURE__*/ React.createElement(DisclosureIconWrapper, {
        visibility: isLeaf ? 'hidden' : 'visible'
    }, /*#__PURE__*/ React.createElement(StyledArrowRight, {
        size: sizeToIconSize(size, variant),
        color: "inherit",
        className: withArrowInverse
    }))), !isLeaf && isCurrentLevelOpened && /*#__PURE__*/ React.createElement(ChildItems, null, (_item_items = item.items) === null || _item_items === void 0 ? void 0 : _item_items.map(function(item, index) {
        return /*#__PURE__*/ React.createElement(Item, {
            item: item,
            key: item.value,
            pathToItem: _to_consumable_array(pathToItem).concat([
                index
            ])
        });
    })));
};
