'use strict';

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

var _rollupPluginBabelHelpers = require('../../../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var Dropdown_tokens = require('../../Dropdown.tokens.js');
var index = require('../../../../utils/index.js');
var Dropdown_context = require('../../Dropdown.context.js');
var DropdownItem_styles = require('./DropdownItem.styles.js');
var getItemId = require('../../utils/getItemId.js');

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

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

var _excluded = ["value", "label", "disabled", "isDisabled", "contentLeft", "contentRight", "dividerBefore", "dividerAfter", "className"];
var DropdownItem = function DropdownItem(_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,
    dividerBefore = item.dividerBefore,
    dividerAfter = item.dividerAfter,
    className = item.className,
    rest = _rollupPluginBabelHelpers.objectWithoutProperties(item, _excluded);
  var ref = React.useRef(null);
  var _useContext = React.useContext(Dropdown_context.Context),
    focusedPath = _useContext.focusedPath,
    size = _useContext.size,
    variant = _useContext.variant,
    itemRole = _useContext.itemRole,
    handleGlobalToggle = _useContext.handleGlobalToggle,
    closeOnSelect = _useContext.closeOnSelect,
    onHover = _useContext.onHover,
    onItemSelect = _useContext.onItemSelect,
    onItemClick = _useContext.onItemClick,
    hasArrow = _useContext.hasArrow,
    treeId = _useContext.treeId,
    renderItem = _useContext.renderItem;
  var hasDescendants = Boolean(item.items);
  var disclosureIconSize = size === 'xs' ? 'xs' : 's';
  var itemDisabled = Boolean(disabled || isDisabled);
  var isDisabledClassName = itemDisabled ? Dropdown_tokens.classes.dropdownItemIsDisabled : undefined;
  var focusedClass = currentLevel === focusedPath.length - 1 && index$1 === (focusedPath === null || focusedPath === void 0 ? void 0 : focusedPath[currentLevel]) ? Dropdown_tokens.classes.dropdownItemIsFocused : undefined;
  var activeClass = value === (path === null || path === void 0 ? void 0 : path[currentLevel + 1]) ? Dropdown_tokens.classes.dropdownItemIsActive : undefined;
  React.useEffect(function () {
    if (focusedClass && ref !== null && ref !== void 0 && ref.current) {
      ref.current.scrollIntoView({
        behavior: 'smooth',
        block: 'center',
        inline: 'center'
      });
    }
  }, [focusedClass]);
  var handleClick = function handleClick(event) {
    if (itemDisabled) {
      event.stopPropagation();
      return;
    }
    if (onItemSelect) {
      onItemSelect(item, event);
    }
    if (onItemClick) {
      onItemClick(item, event);
    }

    // Закрываем весь дропдаун целиком при клике на айтем без потомков. Только при closeOnSelect === true.
    if (closeOnSelect && !hasDescendants) {
      handleGlobalToggle(false, event);
    }
  };
  var handleHover = function handleHover() {
    if (onHover) {
      onHover(index$1, item);
    }
  };
  return /*#__PURE__*/React__default.default.createElement(React__default.default.Fragment, null, dividerBefore && /*#__PURE__*/React__default.default.createElement(DropdownItem_styles.Divider, {
    variant: variant
  }), /*#__PURE__*/React__default.default.createElement(DropdownItem_styles.Wrapper, _rollupPluginBabelHelpers.extends({}, rest, {
    ref: ref,
    className: index.cx(isDisabledClassName, focusedClass, activeClass, className),
    id: getItemId.getItemId(treeId, value.toString()),
    role: itemRole,
    onClick: handleClick,
    onMouseEnter: handleHover,
    variant: variant,
    "aria-disabled": disabled || isDisabled,
    "aria-controls": ariaControls,
    "aria-expanded": ariaExpanded,
    "aria-level": ariaLevel,
    "aria-label": ariaLabel
  }), renderItem ? /*#__PURE__*/React__default.default.createElement(DropdownItem_styles.RenderItemWrapper, null, renderItem(item)) : /*#__PURE__*/React__default.default.createElement(DropdownItem_styles.CellWrapper, null, /*#__PURE__*/React__default.default.createElement(DropdownItem_styles.StyledCell, {
    contentLeft: contentLeft,
    contentRight: contentRight,
    alignContentLeft: "center",
    alignContentRight: "center",
    title: label
  })), item.items && hasArrow && /*#__PURE__*/React__default.default.createElement(DropdownItem_styles.DisclosureIconWrapper, null, /*#__PURE__*/React__default.default.createElement(DropdownItem_styles.StyledIconDisclosureRight, {
    size: disclosureIconSize,
    color: "inherit"
  }))), dividerAfter && /*#__PURE__*/React__default.default.createElement(DropdownItem_styles.Divider, {
    variant: variant
  }));
};

exports.DropdownItem = DropdownItem;
//# sourceMappingURL=DropdownItem.js.map
