import * as o from "react";
import { useControlled as O } from "../../../../../../../@base-ui_utils@0.2.4_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/utils/esm/useControlled.js";
import { useIsoLayoutEffect as j } from "../../../../../../../@base-ui_utils@0.2.4_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/utils/esm/useIsoLayoutEffect.js";
import { useStableCallback as p } from "../../../../../../../@base-ui_utils@0.2.4_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/utils/esm/useStableCallback.js";
import { useRenderElement as z } from "../../utils/useRenderElement.js";
import { CompositeList as H } from "../../composite/list/CompositeList.js";
import { useDirection as L } from "../../direction-provider/DirectionContext.js";
import { TabsRootContext as _ } from "./TabsRootContext.js";
import { tabsStateAttributesMapping as q } from "./stateAttributesMapping.js";
import { jsx as w } from "react/jsx-runtime";
const F = /* @__PURE__ */ o.forwardRef(function(l, y) {
  const {
    className: J,
    defaultValue: i = 0,
    onValueChange: c,
    orientation: f = "horizontal",
    render: K,
    value: M,
    ...A
  } = l, T = L(), g = Object.hasOwn(l, "defaultValue"), I = o.useRef([]), [v, C] = o.useState(() => /* @__PURE__ */ new Map()), [a, d] = O({
    controlled: M,
    default: i,
    name: "Tabs",
    state: "value"
  }), x = M !== void 0, [s, E] = o.useState(() => /* @__PURE__ */ new Map()), [b, m] = o.useState("none"), R = p((e, t) => {
    c == null || c(e, t), !t.isCanceled && (d(e), m(t.activationDirection));
  }), h = p((e, t) => {
    C((n) => {
      if (n.get(e) === t)
        return n;
      const r = new Map(n);
      return r.set(e, t), r;
    });
  }), P = p((e, t) => {
    C((n) => {
      if (!n.has(e) || n.get(e) !== t)
        return n;
      const r = new Map(n);
      return r.delete(e), r;
    });
  }), S = o.useCallback((e) => v.get(e), [v]), V = o.useCallback((e) => {
    for (const t of s.values())
      if (e === (t == null ? void 0 : t.value))
        return t == null ? void 0 : t.id;
  }, [s]), D = o.useCallback((e) => {
    if (e === void 0)
      return null;
    for (const [t, n] of s.entries())
      if (n != null && e === (n.value ?? n.index))
        return t;
    return null;
  }, [s]), N = o.useMemo(() => ({
    direction: T,
    getTabElementBySelectedValue: D,
    getTabIdByPanelValue: V,
    getTabPanelIdByValue: S,
    onValueChange: R,
    orientation: f,
    registerMountedTabPanel: h,
    setTabMap: E,
    unregisterMountedTabPanel: P,
    tabActivationDirection: b,
    value: a
  }), [T, D, V, S, R, f, h, E, P, b, a]), u = o.useMemo(() => {
    for (const e of s.values())
      if (e != null && e.value === a)
        return e;
  }, [s, a]), k = o.useMemo(() => {
    for (const e of s.values())
      if (e != null && !e.disabled)
        return e.value;
  }, [s]);
  j(() => {
    if (x || s.size === 0)
      return;
    const e = u == null ? void 0 : u.disabled, t = u == null && a !== null;
    if (g && e && a === i || !e && !t)
      return;
    const r = k ?? null;
    a !== r && (d(r), m("none"));
  }, [i, k, g, x, u, m, d, s, a]);
  const B = z("div", l, {
    state: {
      orientation: f,
      tabActivationDirection: b
    },
    ref: y,
    props: A,
    stateAttributesMapping: q
  });
  return /* @__PURE__ */ w(_.Provider, {
    value: N,
    children: /* @__PURE__ */ w(H, {
      elementsRef: I,
      children: B
    })
  });
});
process.env.NODE_ENV !== "production" && (F.displayName = "TabsRoot");
export {
  F as TabsRoot
};
