import { jsx as _jsx } from "react/jsx-runtime";
import { useMemo, forwardRef, useContext } from 'react';
import clsx from 'clsx';
import { isDef } from '../utils';
import Badge from '../badge';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';
const TabsTitle = forwardRef((props, ref) => {
  const {
    prefixCls,
    createNamespace
  } = useContext(ConfigProviderContext);
  const [bem] = createNamespace('tab', prefixCls);
  const {
    type,
    color,
    isActive,
    activeColor,
    inactiveColor,
    disabled,
    className
  } = props;
  const customStyle = useMemo(() => {
    const style = Object.assign({}, props.style);
    const isCard = type === 'card'; // card theme color

    if (color && isCard) {
      style.borderColor = color;

      if (!disabled) {
        if (isActive) {
          style.backgroundColor = color;
        } else {
          style.color = color;
        }
      }
    }

    const titleColor = isActive ? activeColor : inactiveColor;

    if (titleColor) {
      style.color = titleColor;
    }

    return style;
  }, [type, color, disabled, isActive, activeColor, inactiveColor]);

  const renderText = () => {
    const Text = _jsx("span", Object.assign({
      className: clsx(bem('text', {
        ellipsis: !props.scrollable
      }))
    }, {
      children: (() => {
        if (typeof props.renderTitle === 'function') {
          return props.renderTitle(isActive);
        }

        return props.renderTitle || props.title;
      })()
    }));

    if (props.dot || isDef(props.badge) && props.badge !== '') {
      return _jsx(Badge, Object.assign({
        dot: props.dot,
        content: props.badge,
        showZero: props.showZeroBadge
      }, {
        children: Text
      }));
    }

    return Text;
  };

  return _jsx("div", Object.assign({
    ref: ref,
    className: clsx([bem({
      active: props.isActive,
      disabled: props.disabled
    }), className]),
    style: customStyle,
    "aria-selected": props.isActive,
    onClick: props.onClick
  }, {
    children: renderText()
  }));
});
TabsTitle.defaultProps = {
  showZeroBadge: true
};
export default TabsTitle;