'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 Select_tokens = require('../../../../Select.tokens.js');
var Select_context = require('../../../../Select.context.js');
var Item_styles = require('./Item.styles.js');
var useDidMountEffect = require('../../../../../../hooks/useDidMountEffect.js');
var getItemId = require('../../../../utils/getItemId.js');
var mapCheckedVariants = require('../../../../../Combobox/ComboboxNew/utils/mapCheckedVariants.js');
var sizeToIconSize = require('../../../../utils/sizeToIconSize.js');
var isEmpty = require('../../../../../../utils/isEmpty.js');

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

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

var _StyledIndicator;
var _excluded = ["value", "label", "disabled", "isDisabled", "contentLeft", "contentRight", "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,
    label = item.label,
    disabled = item.disabled,
    isDisabled = item.isDisabled,
    contentLeft = item.contentLeft,
    contentRight = item.contentRight,
    className = item.className,
    rest = _rollupPluginBabelHelpers.objectWithoutProperties(item, _excluded);
  var ref = React.useRef(null);
  var _useContext = React.useContext(Select_context.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,
    _checkboxAppearance = _useContext._checkboxAppearance;
  var checkedValue = checked.get(item.value) || false;
  var itemDisabled = Boolean(disabled || isDisabled);
  var disabledClassName = itemDisabled ? Select_tokens.classes.dropdownItemIsDisabled : undefined;
  var focusedClass = currentLevel === focusedPath.length - 1 && index$1 === (focusedPath === null || focusedPath === void 0 ? void 0 : focusedPath[currentLevel]) ? Select_tokens.classes.dropdownItemIsFocused : undefined;
  var activeClass = value === (path === null || path === void 0 ? void 0 : path[currentLevel + 1]) ? Select_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 (itemDisabled) return;
    e.stopPropagation();
    handleCheckboxChange(item);
  };
  var handleClick = function handleClick(e) {
    if (itemDisabled) {
      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.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__default.default.createElement(Item_styles.IconWrapper, {
    variant: variant
  }, renderSelectionIcon ? renderSelectionIcon(mapCheckedVariants.mapCheckedVariants(checkedValue, multiselect)) : /*#__PURE__*/React__default.default.createElement(React__default.default.Fragment, null, multiselect && /*#__PURE__*/React__default.default.createElement(Item_styles.StyledCheckboxWrapper, {
    onClick: function onClick(e) {
      return e.stopPropagation();
    }
  }, /*#__PURE__*/React__default.default.createElement(Item_styles.StyledCheckbox, {
    disabled: itemDisabled,
    checked: Boolean(checkedValue),
    indeterminate: checkedValue === 'indeterminate',
    onChange: handleChange,
    appearance: _checkboxAppearance !== null && _checkboxAppearance !== void 0 ? _checkboxAppearance : 'default'
  })), !multiselect && checkedValue === 'dot' && (_StyledIndicator || (_StyledIndicator = /*#__PURE__*/React__default.default.createElement(Item_styles.StyledIndicator, {
    size: "s",
    view: "default"
  }))), !multiselect && checkedValue === 'done' && /*#__PURE__*/React__default.default.createElement(Item_styles.StyledIconDone, {
    size: sizeToIconSize.sizeToIconSize(size, variant),
    color: "inherit"
  }))), renderItem ? /*#__PURE__*/React__default.default.createElement(Item_styles.StyledText, null, renderItem(item)) : /*#__PURE__*/React__default.default.createElement(Item_styles.StyledCell, {
    contentLeft: contentLeft,
    contentRight: contentRight,
    title: label,
    className: singleLine ? Select_tokens.classes.singleLineMode : ''
  }), !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;
//# sourceMappingURL=Item.js.map
