import { objectWithoutProperties as _objectWithoutProperties, extends as _extends, objectSpread2 as _objectSpread2 } from '../../../../../_virtual/_rollupPluginBabelHelpers.js';
import React__default, { forwardRef, useRef, useContext } from 'react';
import { cx } from '../../../../../utils/index.js';
import { classes } from '../../../tokens.js';
import { useTabItem } from '../../../hooks/useTabItem.js';
import { base as base$2 } from './variations/_view/base.js';
import { base as base$1 } from './variations/_size/base.js';
import { base as base$4 } from './variations/_pilled/base.js';
import { base as base$3 } from './variations/_disabled/base.js';
import { LeftContent, StyledContent, TabItemValue, RightContent, ActionContent, base } from './HorizontalTabItem.styles.js';
import { useForkRef } from '../../../../../hooks/useForkRef.js';
import { TabsContext } from '../../../TabsContext.js';

var _excluded = ["size", "view", "isActive", "selected", "disabled", "pilled", "children", "value", "contentLeft", "contentRight", "actionContent", "animated", "onIndexChange", "itemIndex", "tabIndex", "className", "onClick", "maxItemWidth"];
var horizontalTabItemRoot = function horizontalTabItemRoot(Root) {
  return /*#__PURE__*/forwardRef(function (props, outerRef) {
    var size = props.size,
      view = props.view,
      isActive = props.isActive,
      selected = props.selected,
      _props$disabled = props.disabled,
      disabled = _props$disabled === void 0 ? false : _props$disabled,
      _props$pilled = props.pilled,
      pilled = _props$pilled === void 0 ? false : _props$pilled,
      children = props.children,
      value = props.value,
      contentLeft = props.contentLeft,
      contentRight = props.contentRight,
      actionContent = props.actionContent,
      _props$animated = props.animated,
      animated = _props$animated === void 0 ? true : _props$animated,
      onIndexChange = props.onIndexChange,
      itemIndex = props.itemIndex,
      tabIndex = props.tabIndex,
      className = props.className,
      onClick = props.onClick,
      _props$maxItemWidth = props.maxItemWidth,
      maxWidth = _props$maxItemWidth === void 0 ? 'auto' : _props$maxItemWidth,
      rest = _objectWithoutProperties(props, _excluded);
    var innerRef = useRef(null);
    var ref = useForkRef(outerRef, innerRef);
    var refs = useContext(TabsContext);
    var role = 'tab';
    var pilledAttr = view !== 'clear' && pilled;
    var animatedClass = animated ? classes.tabItemAnimated : undefined;
    var pilledClass = pilledAttr ? classes.tabsPilled : undefined;
    var selectedClass = isActive || selected ? classes.selectedTabsItem : undefined;
    var truncateClass = maxWidth !== 'auto' ? classes.tabsTruncate : undefined;
    var _useTabItem = useTabItem({
        refs: refs,
        innerRef: innerRef,
        itemIndex: itemIndex,
        selected: selected,
        disabled: disabled,
        onIndexChange: onIndexChange,
        onClick: onClick,
        orientation: rest.orientation
      }),
      hasKeyNavigation = _useTabItem.hasKeyNavigation,
      navigationTabIndex = _useTabItem.navigationTabIndex,
      onItemFocus = _useTabItem.onItemFocus,
      handleClick = _useTabItem.handleClick;
    return /*#__PURE__*/React__default.createElement(Root, _extends({
      ref: ref,
      disabled: disabled,
      pilled: pilled,
      role: role,
      view: view
      // TODO: убрать каст any, когда будут удалены deprecated props
      ,
      size: size,
      onFocus: onItemFocus,
      tabIndex: hasKeyNavigation ? navigationTabIndex : tabIndex,
      className: cx(pilledClass, selectedClass, animatedClass, truncateClass, className),
      onClick: handleClick
    }, rest, {
      style: _objectSpread2(_objectSpread2({}, rest.style), {}, {
        maxWidth: maxWidth
      })
    }), /*#__PURE__*/React__default.createElement(React__default.Fragment, null, contentLeft && /*#__PURE__*/React__default.createElement(LeftContent, {
      className: classes.tabLeftContent
    }, contentLeft), /*#__PURE__*/React__default.createElement(StyledContent, {
      className: classes.tabContent
    }, children), !contentRight && value && /*#__PURE__*/React__default.createElement(TabItemValue, null, value), !value && contentRight && /*#__PURE__*/React__default.createElement(RightContent, {
      className: classes.tabRightContent
    }, contentRight), actionContent && /*#__PURE__*/React__default.createElement(ActionContent, null, actionContent)));
  });
};
var horizontalTabItemConfig = {
  name: 'HorizontalTabItem',
  tag: 'button',
  layout: horizontalTabItemRoot,
  base: base,
  variations: {
    size: {
      css: base$1
    },
    view: {
      css: base$2
    },
    disabled: {
      css: base$3,
      attrs: true
    },
    pilled: {
      css: base$4
    }
  },
  defaults: {
    view: 'default'
  }
};

export { horizontalTabItemConfig, horizontalTabItemRoot };
