"use strict";

exports.__esModule = true;
exports.useSetActive = exports.useBuildTabs = void 0;
var _react = _interopRequireWildcard(require("react"));
var _tab = require("./tab");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(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 (var _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); }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
var useBuildTabs = exports.useBuildTabs = function useBuildTabs(children, activeIndex, onChange) {
  return (0, _react.useMemo)(function () {
    var indeces = [];
    var nav = [];
    var content = null;
    var firstActiveIndex = -1;
    var activeIsDisabled = false;
    _react.Children.forEach(children, function (tab, index) {
      var props = (tab == null ? void 0 : tab.props) || {};
      if (firstActiveIndex < 0 && !props.disabled) firstActiveIndex = index;
      var isActive = activeIndex === indeces.length;
      var key = index + "-" + props.label;
      if (tab) {
        nav.push(/*#__PURE__*/_react["default"].createElement(_tab.Tab, _extends({
          key: key
        }, props, {
          onChange: onChange,
          index: index,
          active: isActive
        })));
      }
      if (isActive) {
        activeIsDisabled = !!props.disabled;
        content = props.children;
      }
      indeces.push(index);
    });
    return [nav, content, firstActiveIndex, activeIsDisabled];
  }, [children, activeIndex, onChange]);
};
var useSetActive = exports.useSetActive = function useSetActive(selected, onChange) {
  if (selected === void 0) {
    selected = 0;
  }
  var _useState = (0, _react.useState)(onChange ? selected : 0),
    activeIndex = _useState[0],
    setActiveIndex = _useState[1];
  var setActive = (0, _react.useCallback)(function (index) {
    if (onChange) {
      onChange(index);
      return;
    }
    setActiveIndex(index);
  }, [onChange]);
  (0, _react.useEffect)(function () {
    setActiveIndex(selected || 0);
  }, [selected]);
  return [activeIndex, setActive];
};