"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _bem = require("@zarm-design/bem");

var _react = _interopRequireWildcard(require("react"));

var _configProvider = require("../config-provider");

var TabBar = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
  var _React$useContext = _react.default.useContext(_configProvider.ConfigContext),
      prefixCls = _React$useContext.prefixCls;

  var bem = (0, _bem.createBEM)('tab-bar', {
    prefixCls: prefixCls
  });
  var className = props.className,
      children = props.children,
      onChange = props.onChange,
      activeKey = props.activeKey,
      defaultActiveKey = props.defaultActiveKey,
      rest = (0, _objectWithoutProperties2.default)(props, ["className", "children", "onChange", "activeKey", "defaultActiveKey"]);

  var _useState = (0, _react.useState)(defaultActiveKey),
      _useState2 = (0, _slicedToArray2.default)(_useState, 2),
      selectedKey = _useState2[0],
      setSelectedKey = _useState2[1];

  var onChildChange = (0, _react.useCallback)(function (value) {
    if (!activeKey) {
      setSelectedKey(value);
    }

    onChange === null || onChange === void 0 ? void 0 : onChange(value);
  }, [activeKey, onChange]);

  var getSelected = function getSelected(index, itemKey) {
    if (!activeKey) {
      if (!defaultActiveKey && index === 0) {
        return true;
      }

      return defaultActiveKey === itemKey;
    }

    return activeKey === itemKey;
  };

  var items = _react.default.Children.map(children, function (element, index) {
    if (! /*#__PURE__*/_react.default.isValidElement(element)) return null;
    var itemKey = element.props.itemKey || index;
    var selected = getSelected(index, itemKey);

    if (!activeKey) {
      selected = selectedKey === itemKey;

      if (!selectedKey && index === 0) {
        selected = true;
      }
    }

    return /*#__PURE__*/(0, _react.cloneElement)(element, {
      key: index,
      // disabled: element.props.disabled,
      onClick: function onClick() {
        return onChildChange(itemKey);
      },
      badge: element.props.badge,
      title: element.props.title,
      icon: element.props.icon,
      itemKey: itemKey,
      style: element.props.style,
      selected: selected
    });
  });

  return /*#__PURE__*/_react.default.createElement("div", (0, _extends2.default)({}, rest, {
    ref: ref,
    className: bem([className])
  }), /*#__PURE__*/_react.default.createElement("div", {
    className: bem('wrapper')
  }, items));
});

TabBar.displayName = 'TabBar';
var _default = TabBar;
exports.default = _default;