"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TabList = void 0;
var _react = _interopRequireWildcard(require("@ariakit/react"));
var Ariakit = _react;
var _warning = _interopRequireDefault(require("@wordpress/warning"));
var _element = require("@wordpress/element");
var _context = require("./context");
var _styles = require("./styles");
var _clsx = _interopRequireDefault(require("clsx"));
var _elementRect = require("../utils/element-rect");
var _useOnValueUpdate = require("../utils/hooks/use-on-value-update");
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (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; }
/**
 * External dependencies
 */

/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const TabList = exports.TabList = (0, _element.forwardRef)(function TabList({
  children,
  ...otherProps
}, ref) {
  const context = (0, _context.useTabsContext)();
  const tabStoreState = (0, _react.useStoreState)(context?.store);
  const selectedId = tabStoreState?.selectedId;
  const indicatorPosition = (0, _elementRect.useTrackElementOffsetRect)(context?.store.item(selectedId)?.element);
  const [animationEnabled, setAnimationEnabled] = (0, _element.useState)(false);
  (0, _useOnValueUpdate.useOnValueUpdate)(selectedId, ({
    previousValue
  }) => previousValue && setAnimationEnabled(true));
  if (!context || !tabStoreState) {
    globalThis.SCRIPT_DEBUG === true ? (0, _warning.default)('`Tabs.TabList` must be wrapped in a `Tabs` component.') : void 0;
    return null;
  }
  const {
    store
  } = context;
  const {
    activeId,
    selectOnMove
  } = tabStoreState;
  const {
    setActiveId
  } = store;
  const onBlur = () => {
    if (!selectOnMove) {
      return;
    }

    // When automatic tab selection is on, make sure that the active tab is up
    // to date with the selected tab when leaving the tablist. This makes sure
    // that the selected tab will receive keyboard focus when tabbing back into
    // the tablist.
    if (selectedId !== activeId) {
      setActiveId(selectedId);
    }
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(Ariakit.TabList, {
    ref: ref,
    store: store,
    render: /*#__PURE__*/(0, _jsxRuntime.jsx)(_styles.TabListWrapper, {
      onTransitionEnd: event => {
        if (event.pseudoElement === '::after') {
          setAnimationEnabled(false);
        }
      }
    }),
    onBlur: onBlur,
    ...otherProps,
    style: {
      '--indicator-top': indicatorPosition.top,
      '--indicator-right': indicatorPosition.right,
      '--indicator-left': indicatorPosition.left,
      '--indicator-width': indicatorPosition.width,
      '--indicator-height': indicatorPosition.height,
      ...otherProps.style
    },
    className: (0, _clsx.default)(animationEnabled ? 'is-animation-enabled' : '', otherProps.className),
    children: children
  });
});
//# sourceMappingURL=tablist.js.map