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); }

/* eslint-disable no-console */
import React, { useContext, useMemo } from 'react';
import clsx from 'clsx';
import TabbarContext from './TabbarContext';
import { Badge } from '../badge';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';

var TabbarItem = function TabbarItem(props) {
  var setActive = props.setActive,
      index = props.index;

  var _useContext = useContext(ConfigProviderContext),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace;

  var _createNamespace = createNamespace('tabbar-item', prefixCls),
      bem = _createNamespace[0];

  var _useContext2 = useContext(TabbarContext),
      parent = _useContext2.parent;

  var activeColor = parent.activeColor,
      inactiveColor = parent.inactiveColor;

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

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

  var onClick = function onClick(event) {
    var _props$name;

    setActive((_props$name = props.name) != null ? _props$name : index);
    props.onClick == null ? void 0 : props.onClick(event);
  };

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

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

    return null;
  };

  var color = active ? activeColor : inactiveColor;
  return /*#__PURE__*/React.createElement("div", {
    className: clsx(props.className, bem({
      active: active
    })),
    style: _extends({}, props.style, {
      color: color
    }),
    onClick: onClick
  }, /*#__PURE__*/React.createElement(Badge, _extends({}, props.badge, {
    className: clsx(bem('icon'))
  }), renderIcon()), /*#__PURE__*/React.createElement("div", {
    className: clsx(bem('text'))
  }, typeof props.children === 'function' ? props.children(active) : props.children));
};

export default TabbarItem;