"use strict";

exports.__esModule = true;
exports.useSetActive = exports.useBuildTabs = void 0;
var _react = _interopRequireWildcard(require("react"));
var _tab = require("./tab");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
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];
};