"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.JumboTabs = exports.JumboTab = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _web = require("@react-spring/web");
var _nativeProps = require("../../utils/native-props");
var _usePropsValue = require("../../utils/use-props-value");
var _useResizeEffect = require("../../utils/use-resize-effect");
var _useTabListScroll = require("../../utils/use-tab-list-scroll");
var _scrollMask = _interopRequireDefault(require("../scroll-mask"));
var _shouldRender = require("../../utils/should-render");
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); }
const classPrefix = `adm-jumbo-tabs`;
const JumboTab = () => {
  return null;
};
exports.JumboTab = JumboTab;
const JumboTabs = props => {
  var _a;
  const tabListContainerRef = (0, _react.useRef)(null);
  const rootRef = (0, _react.useRef)(null);
  const keyToIndexRecord = {};
  let firstActiveKey = null;
  const panes = [];
  (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;
    }
    const length = panes.push(child);
    keyToIndexRecord[key] = length - 1;
  });
  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);
    }
  });
  const {
    scrollLeft,
    animate
  } = (0, _useTabListScroll.useTabListScroll)(tabListContainerRef, keyToIndexRecord[activeKey]);
  (0, _useResizeEffect.useResizeEffect)(() => {
    animate(true);
  }, rootRef);
  return (0, _nativeProps.withNativeProps)(props, _react.default.createElement("div", {
    className: classPrefix,
    ref: rootRef
  }, _react.default.createElement("div", {
    className: `${classPrefix}-header`
  }, _react.default.createElement(_scrollMask.default, {
    scrollTrackRef: tabListContainerRef
  }), _react.default.createElement(_web.animated.div, {
    className: `${classPrefix}-tab-list`,
    ref: tabListContainerRef,
    scrollLeft: scrollLeft
  }, panes.map(pane => (0, _nativeProps.withNativeProps)(pane.props, _react.default.createElement("div", {
    key: pane.key,
    className: `${classPrefix}-tab-wrapper`
  }, _react.default.createElement("div", {
    onClick: () => {
      const {
        key
      } = pane;
      if (pane.props.disabled) return;
      if (key === undefined || key === null) {
        return;
      }
      setActiveKey(key.toString());
    },
    className: (0, _classnames.default)(`${classPrefix}-tab`, {
      [`${classPrefix}-tab-active`]: pane.key === activeKey,
      [`${classPrefix}-tab-disabled`]: pane.props.disabled
    })
  }, _react.default.createElement("div", {
    className: `${classPrefix}-tab-title`
  }, pane.props.title), _react.default.createElement("div", {
    className: `${classPrefix}-tab-description`
  }, pane.props.description))))))), panes.map(pane => {
    if (pane.props.children === undefined) {
      return null;
    }
    const active = pane.key === activeKey;
    return _react.default.createElement(_shouldRender.ShouldRender, {
      key: pane.key,
      active: active,
      forceRender: pane.props.forceRender,
      destroyOnClose: pane.props.destroyOnClose
    }, _react.default.createElement("div", {
      className: `${classPrefix}-content`,
      style: {
        display: active ? 'block' : 'none'
      }
    }, pane.props.children));
  })));
};
exports.JumboTabs = JumboTabs;