"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "Item", {
    enumerable: true,
    get: function() {
        return Item;
    }
});
var _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
var _Selecttokens = require("../../../../Select.tokens");
var _utils = require("../../../../utils");
var _utils1 = require("../../../../../../utils");
var _hooks = require("../../../../../../hooks");
var _treePathReducer = require("../../../../reducers/treePathReducer");
var _Selectcontext = require("../../../../Select.context");
var _Itemstyles = require("./Item.styles");
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 _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;
}
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);
}
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, isDisabled = item.isDisabled, contentLeft = item.contentLeft, contentRight = item.contentRight;
    var isLeaf = !(item === null || item === void 0 ? void 0 : item.items);
    var _useContext = (0, _react.useContext)(_Selectcontext.Context), checked = _useContext.checked, multiselect = _useContext.multiselect, 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 itemDisabled = Boolean(disabled || isDisabled);
    var disabledClassName = itemDisabled ? _Selecttokens.classes.dropdownItemIsDisabled : undefined;
    var ref = (0, _react.useRef)(null);
    var currentItemDepth = (((_valueToPathMap_get = valueToPathMap.get(item.value.toString())) === null || _valueToPathMap_get === void 0 ? void 0 : _valueToPathMap_get.length) || 0) - 1;
    var isCurrentLevelOpened = (0, _treePathReducer.keyExists)(treePath, valueToPathMap.get(value.toString()) || []);
    var withArrowInverse = isCurrentLevelOpened ? _Selecttokens.classes.arrowInverse : undefined;
    var focusedClass = (0, _utils1.isArraysEqual)(pathToItem, focusedPath) ? _Selecttokens.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);
    };
    (0, _hooks.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.default.createElement(_Itemstyles.ItemWrapper, null, /*#__PURE__*/ _react.default.createElement(_Itemstyles.Wrapper, {
        ref: ref,
        className: (0, _utils1.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, _utils.sizeToIconSize)(size, variant),
        color: "inherit",
        className: withArrowInverse
    })), multiselect && /*#__PURE__*/ _react.default.createElement(_Itemstyles.IconWrapper, {
        variant: variant
    }, /*#__PURE__*/ _react.default.createElement(_Itemstyles.StyledCheckboxWrapper, {
        onClick: function onClick(e) {
            return e.stopPropagation();
        }
    }, /*#__PURE__*/ _react.default.createElement(_Itemstyles.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'
    }))), !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, _utils.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, _utils.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, _utils.sizeToIconSize)(size, variant),
        color: "inherit",
        className: withArrowInverse
    }))), !isLeaf && isCurrentLevelOpened && /*#__PURE__*/ _react.default.createElement(_Itemstyles.ChildItems, null, (_item_items = item.items) === null || _item_items === void 0 ? void 0 : _item_items.map(function(item, index) {
        return /*#__PURE__*/ _react.default.createElement(Item, {
            item: item,
            key: item.value,
            pathToItem: _to_consumable_array(pathToItem).concat([
                index
            ])
        });
    })));
};
