"use strict";

exports.__esModule = true;
exports["default"] = void 0;

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

var _excluded = ["style", "className", "contentAttribute", "captionAttribute", "sndcaptionAttribute", "list", "rectJustify", "tiled", "disabled", "exceptOnClickActive", "onChange", "activeIndex"];

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var Tabbar = (0, _react.forwardRef)(function (_ref, ref) {
  var style = _ref.style,
      _ref$className = _ref.className,
      className = _ref$className === void 0 ? 'tabbar-line tabbar-line-width70 border-b' : _ref$className,
      _ref$contentAttribute = _ref.contentAttribute,
      contentAttribute = _ref$contentAttribute === void 0 ? {} : _ref$contentAttribute,
      _ref$captionAttribute = _ref.captionAttribute,
      captionAttribute = _ref$captionAttribute === void 0 ? {} : _ref$captionAttribute,
      _ref$sndcaptionAttrib = _ref.sndcaptionAttribute,
      sndcaptionAttribute = _ref$sndcaptionAttrib === void 0 ? {} : _ref$sndcaptionAttrib,
      _ref$list = _ref.list,
      list = _ref$list === void 0 ? [] : _ref$list,
      _ref$rectJustify = _ref.rectJustify,
      rectJustify = _ref$rectJustify === void 0 ? true : _ref$rectJustify,
      tiled = _ref.tiled,
      disabled = _ref.disabled,
      _ref$exceptOnClickAct = _ref.exceptOnClickActive,
      exceptOnClickActive = _ref$exceptOnClickAct === void 0 ? true : _ref$exceptOnClickAct,
      onChange = _ref.onChange,
      _ref$activeIndex = _ref.activeIndex,
      activeIndex = _ref$activeIndex === void 0 ? 0 : _ref$activeIndex,
      others = _objectWithoutPropertiesLoose(_ref, _excluded);

  function handleClick(e) {
    var target = e.target;
    if (exceptOnClickActive && target.classList.contains('active')) return;
    var index = target.getAttribute('data-index');
    if (!index) return;

    if (onChange) {
      onChange(e, list[index].name || list[index].caption, [list[index]], Number(index));
      e.stopPropagation();
    }
  }

  function getTabbarStyle() {
    var tabbarStyle = {}; // 矩形tabbar应当有的总宽度

    if (rectJustify && className.hasClass('tabbar-rect')) {
      switch (list.length) {
        case 1:
          tabbarStyle = {
            width: '30%'
          };
          break;

        case 2:
          tabbarStyle = {
            width: '50%'
          };
          break;

        case 3:
          tabbarStyle = {
            width: '50%'
          };
          break;

        default:
          tabbarStyle = {};
      }
    }

    return Object.assign({}, tabbarStyle, style);
  }

  function getTabbarClassName() {
    return 'tabbar animated' + (className ? ' ' + className : ' tabbar-line-width60') + (tiled ? ' tabbar-tiled' : '');
  }

  function getIconDOM(icon, iconActive, isActive) {
    if (isActive) {
      return iconActive ? iconActive : icon;
    }

    return icon;
  }

  function getTabsDOM() {
    if (!Array.isArray(list)) {
      console.log('SeedsUI: Tabbar的传入参数list类型不正确');
      return;
    } // tabStyle高度


    var tabStyle = {};

    if (style && style.height) {
      tabStyle = {
        height: style.height
      };
    } // 遍历


    return list.map(function (item, index) {
      var icon = item.icon,
          iconActive = item.iconActive,
          ricon = item.ricon,
          riconActive = item.riconActive,
          name = item.name,
          caption = item.caption,
          sndcaption = item.sndcaption,
          _item$attribute = item.attribute,
          attribute = _item$attribute === void 0 ? {} : _item$attribute,
          _item$style = item.style,
          style = _item$style === void 0 ? {} : _item$style;
      var isActive = activeIndex === index;
      var liconDOM = null;

      if (icon) {
        liconDOM = getIconDOM(icon, iconActive, isActive);
      }

      var riconDOM = null;

      if (ricon) {
        riconDOM = getIconDOM(ricon, riconActive, isActive);
      }

      return /*#__PURE__*/_react["default"].createElement("li", _extends({
        className: "tab" + (isActive ? ' active' : ''),
        style: Object.assign(tabStyle, style || {}),
        "data-index": index,
        key: index
      }, attribute), liconDOM && liconDOM, /*#__PURE__*/_react["default"].createElement("div", _extends({
        className: "tab-content"
      }, contentAttribute), /*#__PURE__*/_react["default"].createElement("div", _extends({}, captionAttribute, {
        className: "tab-caption" + (captionAttribute.className ? ' ' + captionAttribute.className : '')
      }), caption || name), sndcaption && /*#__PURE__*/_react["default"].createElement("div", _extends({}, sndcaptionAttribute, {
        className: "tab-sndcaption" + (sndcaptionAttribute.className ? ' ' + sndcaptionAttribute.className : '')
      }), sndcaption)), riconDOM && riconDOM);
    });
  } // 获取tabbar样式


  var tabbarStyle = getTabbarStyle(); // 获取tabbar的ClassName

  var tabbarClassName = getTabbarClassName(); // 获取tabs的DOM

  var tabsDOM = getTabsDOM();
  return /*#__PURE__*/_react["default"].createElement("ul", _extends({
    ref: ref
  }, others, {
    className: tabbarClassName,
    disabled: disabled,
    style: tabbarStyle,
    onClick: handleClick
  }), tabsDOM);
});
var _default = Tabbar;
exports["default"] = _default;
module.exports = exports.default;