'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 Combobox_context = require('../../../../Combobox.context.js');
var ItemView = require('../../../ItemView/ItemView.js');
var Item_styles = require('./Item.styles.js');
var useDidMountEffect = require('../../../../../../hooks/useDidMountEffect.js');
var getItemId = require('../../../../utils/getItemId.js');
var isEmpty = require('../../../../../../utils/isEmpty.js');
var sizeToIconSize = require('../../../../utils/sizeToIconSize.js');

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

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

var _excluded = ["value", "disabled", "className"];
var Item = function Item(_ref) {
  var item = _ref.item,
    path = _ref.path,
    currentLevel = _ref.currentLevel,
    index$1 = _ref.index,
    ariaControls = _ref.ariaControls,
    ariaExpanded = _ref.ariaExpanded,
    ariaLevel = _ref.ariaLevel,
    ariaLabel = _ref.ariaLabel;
  var value = item.value,
    disabled = item.disabled,
    className = item.className,
    rest = _rollupPluginBabelHelpers.objectWithoutProperties(item, _excluded);
  var ref = React.useRef(null);
  var _useContext = React.useContext(Combobox_context.Context),
    focusedPath = _useContext.focusedPath,
    checked = _useContext.checked,
    multiple = _useContext.multiple,
    size = _useContext.size,
    handleCheckboxChange = _useContext.handleCheckboxChange,
    handleItemClick = _useContext.handleItemClick,
    variant = _useContext.variant,
    renderItem = _useContext.renderItem,
    renderSelectionIcon = _useContext.renderSelectionIcon,
    treeId = _useContext.treeId,
    singleLine = _useContext.singleLine,
    _checkboxAppearance = _useContext._checkboxAppearance;
  var checkedValue = checked.get(item.value) || false;
  var disabledClassName = disabled ? Combobox_tokens.classes.dropdownItemIsDisabled : undefined;
  var focusedClass = currentLevel === focusedPath.length - 1 && index$1 === (focusedPath === null || focusedPath === void 0 ? void 0 : focusedPath[currentLevel]) ? Combobox_tokens.classes.dropdownItemIsFocused : undefined;
  var activeClass = value === (path === null || path === void 0 ? void 0 : path[currentLevel + 1]) ? Combobox_tokens.classes.dropdownItemIsActive : undefined;
  useDidMountEffect.useDidMountEffect(function () {
    if (focusedClass && ref !== null && ref !== void 0 && ref.current) {
      ref.current.scrollIntoView({
        behavior: 'smooth',
        block: 'center',
        inline: 'center'
      });
    }
  }, [focusedClass]);
  var handleChange = function handleChange(e) {
    if (disabled) return;
    e.stopPropagation();
    handleCheckboxChange(item);
  };
  var handleClick = function handleClick(e) {
    if (disabled) {
      e.stopPropagation();
      return;
    }
    handleItemClick(item, e);
  };
  return /*#__PURE__*/React__default.default.createElement(Item_styles.Wrapper, _rollupPluginBabelHelpers.extends({}, rest, {
    className: index.cx(disabledClassName, focusedClass, activeClass, className),
    id: getItemId.getItemId(treeId, value),
    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__default.default.createElement(ItemView.ItemSelectionIcon, {
    checked: checkedValue,
    disabled: disabled,
    multiple: multiple,
    size: size,
    variant: variant,
    renderSelectionIcon: renderSelectionIcon,
    checkboxAppearance: _checkboxAppearance,
    withMargin: true,
    onChange: handleChange
  }), /*#__PURE__*/React__default.default.createElement(ItemView.ItemContent, {
    item: item,
    renderItem: renderItem,
    singleLine: singleLine
  }), !isEmpty.isEmpty(item.items) && /*#__PURE__*/React__default.default.createElement(Item_styles.DisclosureIconWrapper, null, /*#__PURE__*/React__default.default.createElement(Item_styles.StyledArrow, {
    size: sizeToIconSize.sizeToIconSize(size, variant),
    color: "inherit"
  })));
};

exports.Item = Item;
