"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.PopupTabs = PopupTabs;

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

var _utils = require("../utils");

var _jsxRuntime = require("react/jsx-runtime");

var _excluded = ["tabs", "contentStyle", "shareHeight", "contentClass", "variant"];

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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

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

function PopupTabs(_ref) {
  var tabs = _ref.tabs,
      contentStyle = _ref.contentStyle,
      _ref$shareHeight = _ref.shareHeight,
      shareHeight = _ref$shareHeight === void 0 ? false : _ref$shareHeight,
      contentClass = _ref.contentClass,
      _ref$variant = _ref.variant,
      variant = _ref$variant === void 0 ? "line" : _ref$variant,
      rest = _objectWithoutProperties(_ref, _excluded);

  var _useState = (0, _react.useState)(0),
      _useState2 = _slicedToArray(_useState, 2),
      sharedHeight = _useState2[0],
      setSharedHeight = _useState2[1];

  var _useState3 = (0, _react.useState)(Object.keys(tabs)[0]),
      _useState4 = _slicedToArray(_useState3, 2),
      selected = _useState4[0],
      setSelected = _useState4[1];

  var buttonStyles = {
    line: {
      container: "flex space-x-4 border-b border-gray-300 z-10",
      button: "border-b-2 font-medium text-sm py-2 px-1",
      active: "border-indigo-500 text-indigo-600",
      inactive: "border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300"
    },
    simple: {
      container: "flex flex-wrap border-b border-gray-300 z-10",
      button: "-mb-px z-10 bg-white px-4 py-2 font-medium text-sm rounded-t-md border-gray-300 hover:text-gray-900",
      active: "text-gray-900 border",
      inactive: "text-gray-500 border-b",
      styles: {
        active: {
          borderBottomColor: "white"
        }
      }
    }
  }; // Get shared tabContent height from the tallest tab element.

  var sharedHeightRef = (0, _react.useRef)();
  (0, _react.useLayoutEffect)(function () {
    if (sharedHeightRef.current && sharedHeightRef.current.children.length > 0) {
      requestAnimationFrame(function () {
        var maxHeight = 0;
        Array.from(sharedHeightRef.current.children).forEach(function (o) {
          maxHeight = Math.max(o.clientHeight, maxHeight);
        });
        setSharedHeight(maxHeight);
      });
    }
  }, [sharedHeightRef]);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", _objectSpread(_objectSpread({}, rest), {}, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: buttonStyles[variant].container,
      "aria-label": "Tabs",
      children: Object.entries(tabs).map(function (_ref2) {
        var _buttonStyles$variant, _buttonStyles$variant2, _buttonStyles$variant3, _buttonStyles$variant4, _buttonStyles$variant5, _buttonStyles$variant6;

        var _ref3 = _slicedToArray(_ref2, 2),
            key = _ref3[0],
            tab = _ref3[1];

        return /*#__PURE__*/(0, _jsxRuntime.jsx)("button", {
          type: "button",
          onClick: function onClick() {
            return setSelected(key);
          },
          className: "".concat(buttonStyles[variant].button, " ").concat(selected === key ? buttonStyles[variant].active : buttonStyles[variant].inactive),
          style: _objectSpread(_objectSpread({}, (_buttonStyles$variant = buttonStyles[variant]) === null || _buttonStyles$variant === void 0 ? void 0 : (_buttonStyles$variant2 = _buttonStyles$variant.styles) === null || _buttonStyles$variant2 === void 0 ? void 0 : _buttonStyles$variant2.button), selected === key ? (_buttonStyles$variant3 = buttonStyles[variant]) === null || _buttonStyles$variant3 === void 0 ? void 0 : (_buttonStyles$variant4 = _buttonStyles$variant3.styles) === null || _buttonStyles$variant4 === void 0 ? void 0 : _buttonStyles$variant4.active : (_buttonStyles$variant5 = buttonStyles[variant]) === null || _buttonStyles$variant5 === void 0 ? void 0 : (_buttonStyles$variant6 = _buttonStyles$variant5.styles) === null || _buttonStyles$variant6 === void 0 ? void 0 : _buttonStyles$variant6.inactive),
          children: tab.label
        }, key);
      })
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      style: contentStyle,
      className: (0, _utils.classNames)(contentClass, shareHeight && "relative ".concat(sharedHeight)),
      children: [Object.entries(tabs).map(function (_ref4) {
        var _ref5 = _slicedToArray(_ref4, 2),
            key = _ref5[0],
            tab = _ref5[1];

        return selected === key && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: (0, _utils.classNames)(tab.class),
          style: {
            height: shareHeight ? "".concat(sharedHeight + 2, "px") : null
          },
          children: tab.content
        }, key);
      }), shareHeight && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: "absolute invisible",
        ref: sharedHeightRef,
        children: Object.entries(tabs).map(function (_ref6) {
          var _ref7 = _slicedToArray(_ref6, 2),
              key = _ref7[0],
              tab = _ref7[1];

          return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
            className: (0, _utils.classNames)(tab.class),
            children: tab.content
          }, key);
        })
      })]
    })]
  }));
}