function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import { __rest } from "tslib";
import classNames from 'classnames';
import React, { createElement, forwardRef, Fragment, useContext, isValidElement } from "react";
import { Text } from "@alifd/meet-react-component-one";
import View from '../view';
import Icon from '../icon';
import { TabContext } from './tab';
import { useGuid } from '../utils/hooks';
import { isString } from '../utils';

var TabItem = function TabItem(props, ref) {
  var _classNames, _classNames2, _classNames3, _classNames4;

  var _props$prefix = props.prefix,
      prefix = _props$prefix === void 0 ? 'mt-' : _props$prefix,
      className = props.className,
      size = props.size,
      itemKey = props.itemKey,
      _props$title = props.title,
      title = _props$title === void 0 ? '' : _props$title,
      _props$icon = props.icon,
      icon = _props$icon === void 0 ? '' : _props$icon,
      children = props.children,
      _props$onClick = props.onClick,
      onClick = _props$onClick === void 0 ? function () {} : _props$onClick,
      _props$renderContent = props.renderContent,
      renderContent = _props$renderContent === void 0 ? null : _props$renderContent,
      others = __rest(props, ["prefix", "className", "size", "itemKey", "title", "icon", "children", "onClick", "renderContent"]);

  var _useContext = useContext(TabContext),
      activeKey = _useContext.activeKey,
      change = _useContext.change,
      fullWidth = _useContext.fullWidth,
      shape = _useContext.shape,
      activeLineType = _useContext.activeLineType;

  var active = "".concat(activeKey) === "".concat(itemKey);
  var clsPrefix = "".concat(prefix, "tab-item");
  var curItemId = useGuid('tab-item-');
  var clses = {
    tab: classNames(clsPrefix, "".concat(clsPrefix, "--").concat(size), "".concat(clsPrefix, "--").concat(shape), (_classNames = {}, _defineProperty(_classNames, "".concat(clsPrefix, "--active"), active), _defineProperty(_classNames, "".concat(clsPrefix, "--active-").concat(shape), active), _defineProperty(_classNames, "".concat(clsPrefix, "--fullwidth"), fullWidth), _classNames), className),
    activeLine: "".concat(clsPrefix, "-active-line ").concat(clsPrefix, "-active-line--").concat(activeLineType, " ").concat(clsPrefix, "-active-line--").concat(activeLineType, "-").concat(size),
    tabIcon: classNames("".concat(clsPrefix, "-icon"), "".concat(clsPrefix, "-icon--").concat(size), "".concat(clsPrefix, "-icon--").concat(shape), (_classNames2 = {}, _defineProperty(_classNames2, "".concat(clsPrefix, "-icon--active"), active), _defineProperty(_classNames2, "".concat(clsPrefix, "-icon--active-").concat(shape), active), _classNames2)),
    tabText: classNames("".concat(clsPrefix, "-text"), "".concat(clsPrefix, "-text--").concat(size), "".concat(clsPrefix, "-text--").concat(shape), (_classNames3 = {}, _defineProperty(_classNames3, "".concat(clsPrefix, "-text--active"), active), _defineProperty(_classNames3, "".concat(clsPrefix, "-text--active-").concat(shape), active), _classNames3))
  };

  var renderTitle = function renderTitle() {
    if (isValidElement(title)) {
      return /*#__PURE__*/React.createElement(View, {
        className: clses.tabText
      }, title);
    } else if (isString(title)) {
      return /*#__PURE__*/React.createElement(Text, {
        className: clses.tabText
      }, title);
    }

    return null;
  };

  var handleClick = function handleClick(e) {
    if (onClick) {
      onClick(Object.assign(Object.assign({}, e), {
        itemKey: itemKey
      }));
    }

    change(itemKey);
  };

  return /*#__PURE__*/React.createElement(View, _extends({}, others, {
    id: curItemId,
    ref: ref,
    className: clses.tab,
    onClick: handleClick
  }), renderContent ? renderContent({
    title: title,
    icon: icon,
    active: active
  }) : /*#__PURE__*/React.createElement(Fragment, null, icon ? /*#__PURE__*/React.createElement(Icon, {
    className: clses.tabIcon,
    name: icon
  }) : null, renderTitle()), shape !== 'capsule' && /*#__PURE__*/React.createElement(View, {
    className: classNames(clses.activeLine, (_classNames4 = {}, _defineProperty(_classNames4, "".concat(clsPrefix, "-active-line--enter"), active), _defineProperty(_classNames4, "".concat(clsPrefix, "-active-line--exit"), !active), _classNames4))
  }));
};

TabItem.displayName = 'Tab.Item';
export default /*#__PURE__*/forwardRef(TabItem);