// packages/components/src/tabs/index.tsx
import * as Ariakit from "@ariakit/react";
import { useInstanceId } from "@wordpress/compose";
import { useEffect, useMemo } from "@wordpress/element";
import { isRTL } from "@wordpress/i18n";
import { TabsContext } from "./context.js";
import { Tab } from "./tab.js";
import { TabList } from "./tablist.js";
import { TabPanel } from "./tabpanel.js";
import { jsx as _jsx } from "react/jsx-runtime";
function externalToInternalTabId(externalId, instanceId) {
  return externalId && `${instanceId}-${externalId}`;
}
function internalToExternalTabId(internalId, instanceId) {
  return typeof internalId === "string" ? internalId.replace(`${instanceId}-`, "") : internalId;
}
var Tabs = Object.assign(function Tabs2({
  selectOnMove = true,
  defaultTabId,
  orientation = "horizontal",
  onSelect,
  children,
  selectedTabId,
  activeTabId,
  defaultActiveTabId,
  onActiveTabIdChange
}) {
  const instanceId = useInstanceId(Tabs2, "tabs");
  const store = Ariakit.useTabStore({
    selectOnMove,
    orientation,
    defaultSelectedId: externalToInternalTabId(defaultTabId, instanceId),
    setSelectedId: (newSelectedId) => {
      onSelect?.(internalToExternalTabId(newSelectedId, instanceId));
    },
    selectedId: externalToInternalTabId(selectedTabId, instanceId),
    defaultActiveId: externalToInternalTabId(defaultActiveTabId, instanceId),
    setActiveId: (newActiveId) => {
      onActiveTabIdChange?.(internalToExternalTabId(newActiveId, instanceId));
    },
    activeId: externalToInternalTabId(activeTabId, instanceId),
    rtl: isRTL()
  });
  const {
    items,
    activeId
  } = Ariakit.useStoreState(store);
  const {
    setActiveId
  } = store;
  useEffect(() => {
    requestAnimationFrame(() => {
      const focusedElement = items?.[0]?.element?.ownerDocument.activeElement;
      if (!focusedElement || !items.some((item) => focusedElement === item.element)) {
        return;
      }
      if (activeId !== focusedElement.id) {
        setActiveId(focusedElement.id);
      }
    });
  }, [activeId, items, setActiveId]);
  const contextValue = useMemo(() => ({
    store,
    instanceId
  }), [store, instanceId]);
  return /* @__PURE__ */ _jsx(TabsContext.Provider, {
    value: contextValue,
    children
  });
}, {
  /**
   * Renders a single tab.
   *
   * The currently active tab receives default styling that can be
   * overridden with CSS targeting `[aria-selected="true"]`.
   */
  Tab: Object.assign(Tab, {
    displayName: "Tabs.Tab"
  }),
  /**
   * A wrapper component for the `Tab` components.
   *
   * It is responsible for rendering the list of tabs.
   */
  TabList: Object.assign(TabList, {
    displayName: "Tabs.TabList"
  }),
  /**
   * Renders the content to display for a single tab once that tab is selected.
   */
  TabPanel: Object.assign(TabPanel, {
    displayName: "Tabs.TabPanel"
  }),
  Context: Object.assign(TabsContext, {
    displayName: "Tabs.Context"
  })
});
export {
  Tabs
};
//# sourceMappingURL=index.js.map
