'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('../../../../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", "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,
    contentLeft = item.contentLeft,
    contentRight = item.contentRight,
    className = item.className,
    rest = _rollupPluginBabelHelpers.objectWithoutProperties(item, _excluded);
  var ref = React.useRef(null);
  var _useSelectContext = Select_context.useSelectContext(),
    focusedPath = _useSelectContext.focusedPath,
    checked = _useSelectContext.checked,
    multiselect = _useSelectContext.multiselect,
    size = _useSelectContext.size,
    handleCheckboxChange = _useSelectContext.handleCheckboxChange,
    handleItemClick = _useSelectContext.handleItemClick,
    variant = _useSelectContext.variant,
    renderItem = _useSelectContext.renderItem,
    renderSelectionIcon = _useSelectContext.renderSelectionIcon,
    treeId = _useSelectContext.treeId,
    singleLine = _useSelectContext.singleLine,
    _checkboxAppearance = _useSelectContext._checkboxAppearance;
  var checkedValue = checked.get(item.value) || false;
  var disabledClassName = disabled ? 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 (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.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: disabled,
    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;
