"use client";
import { jsx, jsxs, Fragment } from "react/jsx-runtime";
import { useState } from "react";
import { ExperimentalTabSet } from "../TabSet.js";
function useTabSet(tabs, options) {
  const tabKeys = Object.keys(tabs);
  if (tabKeys.length === 0) {
    throw new Error("useTabSet: `tabs` must contain at least one tab");
  }
  const getIndex = (tab) => {
    if (tab === void 0) return 0;
    const index = tabKeys.indexOf(tab);
    return index === -1 ? 0 : index;
  };
  const initialTabIndex = getIndex((options == null ? void 0 : options.initialTab) ?? tabKeys[0]);
  const [selectedIndex, setSelectedIndex] = useState(initialTabIndex);
  const selectedTab = tabKeys[selectedIndex];
  const setSelectedTab = (tab) => {
    setSelectedIndex(getIndex(tab));
  };
  const tabsProps = {
    children: tabKeys.map((tabKey) => {
      const tab = tabs[tabKey];
      return /* @__PURE__ */ jsx(ExperimentalTabSet.Tab, { disabled: tab.disabled, children: tab.label }, tabKey);
    })
  };
  const panelsProps = {
    children: tabKeys.map((tabKey) => {
      const tab = tabs[tabKey];
      return /* @__PURE__ */ jsx(ExperimentalTabSet.Panel, { children: tab.contents }, tabKey);
    })
  };
  const props = {
    selectedIndex,
    onChange: (index) => {
      var _a;
      setSelectedIndex(index);
      const tab = tabKeys[index];
      if (tab) (_a = options == null ? void 0 : options.onChange) == null ? void 0 : _a.call(options, tab);
    },
    children: /* @__PURE__ */ jsxs(Fragment, { children: [
      /* @__PURE__ */ jsx(ExperimentalTabSet.Tabs, { ...tabsProps }),
      /* @__PURE__ */ jsx(ExperimentalTabSet.Panels, { ...panelsProps })
    ] })
  };
  return {
    selectedTab,
    setSelectedTab,
    props,
    tabsProps,
    panelsProps
  };
}
export {
  useTabSet
};
