import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/* eslint-disable no-console */

import { useContext, useMemo } from 'react';
import clsx from 'clsx';
import TabbarContext from './TabbarContext';
import { Badge } from '../badge';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';

const TabbarItem = props => {
  const {
    setActive,
    index
  } = props;
  const {
    prefixCls,
    createNamespace
  } = useContext(ConfigProviderContext);
  const [bem] = createNamespace('tabbar-item', prefixCls);
  const {
    parent
  } = useContext(TabbarContext);
  const {
    activeColor,
    inactiveColor
  } = parent;

  if (!parent) {
    if (process.env.NODE_ENV !== 'production') {
      console.error('[React Vant] <TabbarItem> must be a child component of <Tabbar>.');
    }
  }

  const active = useMemo(() => {
    return (props.name || index) === parent.value;
  }, [props.name, index, parent.value]);

  const onClick = event => {
    var _a, _b;

    setActive((_a = props.name) !== null && _a !== void 0 ? _a : index);
    (_b = props.onClick) === null || _b === void 0 ? void 0 : _b.call(props, event);
  };

  const renderIcon = () => {
    if (typeof props.icon === 'function') {
      return props.icon(active);
    }

    if (props.icon) {
      return props.icon;
    }

    return null;
  };

  const color = active ? activeColor : inactiveColor;
  return _jsxs("div", Object.assign({
    className: clsx(props.className, bem({
      active
    })),
    style: Object.assign(Object.assign({}, props.style), {
      color
    }),
    onClick: onClick
  }, {
    children: [_jsx(Badge, Object.assign({}, props.badge, {
      className: clsx(bem('icon'))
    }, {
      children: renderIcon()
    })), _jsx("div", Object.assign({
      className: clsx(bem('text'))
    }, {
      children: typeof props.children === 'function' ? props.children(active) : props.children
    }))]
  }));
};

export default TabbarItem;