'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _rollupPluginBabelHelpers = require('../../../../../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var index = require('../../../../../../utils/index.js');
var Combobox_tokens = require('../../../../Combobox.tokens.js');
var treePathReducer = require('../../../../reducers/treePathReducer.js');
var Combobox_context = require('../../../../Combobox.context.js');
var ItemView = require('../../../ItemView/ItemView.js');
var Item_styles = require('./Item.styles.js');
var isArraysEqual = require('../../../../../../utils/isArraysEqual.js');
var useDidMountEffect = require('../../../../../../hooks/useDidMountEffect.js');
var sizeToIconSize = require('../../../../utils/sizeToIconSize.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var React__default = /*#__PURE__*/_interopDefault(React);

var Item = function Item(_ref) {
  var _valueToPathMap$get, _item$items;
  var item = _ref.item,
    pathToItem = _ref.pathToItem;
  var value = item.value,
    disabled = item.disabled;
  var isLeaf = !(item !== null && item !== void 0 && item.items);
  var _useContext = React.useContext(Combobox_context.Context),
    checked = _useContext.checked,
    multiple = _useContext.multiple,
    size = _useContext.size,
    variant = _useContext.variant,
    renderItem = _useContext.renderItem,
    renderSelectionIcon = _useContext.renderSelectionIcon,
    _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 = React.useRef(null);
  var itemDisabled = disabled;
  var disabledClassName = itemDisabled ? Combobox_tokens.classes.dropdownItemIsDisabled : undefined;
  var checkedValue = checked.get(item.value) || false;
  var currentItemDepth = (((_valueToPathMap$get = valueToPathMap.get(item.value.toString())) === null || _valueToPathMap$get === void 0 ? void 0 : _valueToPathMap$get.length) || 0) - 1;
  var isCurrentLevelOpened = treePathReducer.keyExists(treePath, valueToPathMap.get(value.toString()) || []);
  var withArrowInverse = isCurrentLevelOpened ? Combobox_tokens.classes.arrowInverse : undefined;
  var focusedClass = isArraysEqual.isArraysEqual(pathToItem, focusedPath) ? Combobox_tokens.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.useDidMountEffect(function () {
    if (focusedClass && ref !== null && ref !== void 0 && ref.current) {
      ref.current.scrollIntoView({
        behavior: 'smooth',
        block: 'center',
        inline: 'center'
      });
    }
  }, [focusedClass]);
  var itemSelectionIcon = /*#__PURE__*/React__default.default.createElement(ItemView.ItemSelectionIcon, {
    checked: checkedValue,
    disabled: itemDisabled,
    multiple: multiple,
    size: size,
    variant: variant,
    renderSelectionIcon: renderSelectionIcon,
    checkboxAppearance: _checkboxAppearance,
    onChange: handleChange
  });
  return /*#__PURE__*/React__default.default.createElement(Item_styles.ItemWrapper, null, /*#__PURE__*/React__default.default.createElement(Item_styles.Wrapper, {
    ref: ref,
    className: index.cx(focusedClass, disabledClassName),
    onClick: handleClick,
    variant: variant,
    role: "treeitem"
  }, /*#__PURE__*/React__default.default.createElement(Item_styles.Offset, {
    depth: currentItemDepth
  }), arrowPlacement === 'left' && /*#__PURE__*/React__default.default.createElement(Item_styles.DisclosureIconWrapper, {
    visibility: isLeaf ? 'hidden' : 'visible'
  }, /*#__PURE__*/React__default.default.createElement(Item_styles.StyledArrowLeft, {
    size: sizeToIconSize.sizeToIconSize(size, variant),
    color: "inherit",
    className: withArrowInverse
  })), multiple && itemSelectionIcon, !multiple && arrowPlacement === 'right' && itemSelectionIcon, /*#__PURE__*/React__default.default.createElement(ItemView.ItemContent, {
    item: item,
    renderItem: renderItem,
    singleLine: singleLine
  }), !multiple && arrowPlacement === 'left' && itemSelectionIcon, arrowPlacement === 'right' && /*#__PURE__*/React__default.default.createElement(Item_styles.DisclosureIconWrapper, {
    visibility: isLeaf ? 'hidden' : 'visible'
  }, /*#__PURE__*/React__default.default.createElement(Item_styles.StyledArrowRight, {
    size: sizeToIconSize.sizeToIconSize(size, variant),
    color: "inherit",
    className: withArrowInverse
  }))), !isLeaf && isCurrentLevelOpened && /*#__PURE__*/React__default.default.createElement(Item_styles.ChildItems, null, (_item$items = item.items) === null || _item$items === void 0 ? void 0 : _item$items.map(function (item, index) {
    return /*#__PURE__*/React__default.default.createElement(Item, {
      item: item,
      key: item.value,
      pathToItem: [].concat(_rollupPluginBabelHelpers.toConsumableArray(pathToItem), [index])
    });
  })));
};

exports.Item = Item;
