'use strict';

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

var _rollupPluginBabelHelpers = require('../../../../../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var cls = require('classnames');
var Dropdown_context = require('../../../../Dropdown.context.js');
var Dropdown_tokens = require('../../../../Dropdown.tokens.js');
var Item_styles = require('./Item.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 cls__default = /*#__PURE__*/_interopDefault(cls);

var _excluded = ["value", "label", "disabled", "contentLeft", "contentRight", "dividerBefore", "dividerAfter", "className"];
var Item = function Item(_ref) {
  var _item$items;
  var item = _ref.item,
    path = _ref.path,
    currentLevel = _ref.currentLevel,
    index = _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,
    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,
    treeId = _useContext.treeId,
    renderItem = _useContext.renderItem;
  var hasDescendants = Boolean((_item$items = item.items) === null || _item$items === void 0 ? void 0 : _item$items.length);
  var disclosureIconSize = size === 'xs' ? 'xs' : 's';
  var isDisabledClassName = disabled ? Dropdown_tokens.classes.dropdownItemIsDisabled : undefined;
  var focusedClass = currentLevel === focusedPath.length - 1 && index === (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 (disabled) {
      event.stopPropagation();
      return;
    }
    if (onItemSelect) {
      onItemSelect(item, event);
    }

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

exports.Item = Item;
