"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TabBarItem = exports.TabBar = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _nativeProps = require("../../utils/native-props");
var _withDefaultProps = require("../../utils/with-default-props");
var _badge = _interopRequireDefault(require("../badge"));
var _safeArea = _interopRequireDefault(require("../safe-area"));
var _usePropsValue = require("../../utils/use-props-value");
var _traverseReactNode = require("../../utils/traverse-react-node");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
/* istanbul ignore next */
const TabBarItem = () => {
  return null;
};
exports.TabBarItem = TabBarItem;
const classPrefix = `adm-tab-bar`;
const defaultProps = {
  safeArea: false
};
const TabBar = p => {
  var _a;
  const props = (0, _withDefaultProps.mergeProps)(defaultProps, p);
  let firstActiveKey = null;
  const items = [];
  (0, _traverseReactNode.traverseReactNode)(props.children, (child, index) => {
    if (!(0, _react.isValidElement)(child)) return;
    const key = child.key;
    if (typeof key !== 'string') return;
    if (index === 0) {
      firstActiveKey = key;
    }
    items.push(child);
  });
  const [activeKey, setActiveKey] = (0, _usePropsValue.usePropsValue)({
    value: props.activeKey,
    defaultValue: (_a = props.defaultActiveKey) !== null && _a !== void 0 ? _a : firstActiveKey,
    onChange: v => {
      var _a;
      if (v === null) return;
      (_a = props.onChange) === null || _a === void 0 ? void 0 : _a.call(props, v);
    }
  });
  return (0, _nativeProps.withNativeProps)(props, _react.default.createElement("div", {
    className: classPrefix
  }, _react.default.createElement("div", {
    className: `${classPrefix}-wrap`
  }, items.map(item => {
    const active = item.key === activeKey;
    function renderContent() {
      const iconElement = item.props.icon && _react.default.createElement("div", {
        className: `${classPrefix}-item-icon`
      }, typeof item.props.icon === 'function' ? item.props.icon(active) : item.props.icon);
      const titleElement = item.props.title && _react.default.createElement("div", {
        className: (0, _classnames.default)(`${classPrefix}-item-title`, Boolean(iconElement) && `${classPrefix}-item-title-with-icon`)
      }, typeof item.props.title === 'function' ? item.props.title(active) : item.props.title);
      if (iconElement) {
        return _react.default.createElement(_react.default.Fragment, null, _react.default.createElement(_badge.default, {
          content: item.props.badge,
          className: `${classPrefix}-icon-badge`
        }, iconElement), titleElement);
      } else if (titleElement) {
        return _react.default.createElement(_badge.default, {
          content: item.props.badge,
          className: `${classPrefix}-title-badge`
        }, titleElement);
      }
      return null;
    }
    return (0, _nativeProps.withNativeProps)(item.props, _react.default.createElement("div", {
      key: item.key,
      onClick: () => {
        var _a, _b;
        const {
          key
        } = item;
        if (key === undefined || key === null) return;
        setActiveKey(key.toString());
        (_b = (_a = item.props).onClick) === null || _b === void 0 ? void 0 : _b.call(_a);
      },
      className: (0, _classnames.default)(`${classPrefix}-item`, {
        [`${classPrefix}-item-active`]: active
      })
    }, renderContent()));
  })), props.safeArea && _react.default.createElement(_safeArea.default, {
    position: 'bottom'
  })));
};
exports.TabBar = TabBar;