import { j as o } from "./jsx-runtime-B6kdoens.js";
import { y as S, I as _, e as k, W as M, f as L, Q as O } from "./Toggle-B5c5wLIa.js";
import { createContext as R, useCallback as v, useContext as Z } from "react";
import { S as P, P as z } from "./Pack-CcDPYCmE.js";
import { C as c, g as A } from "./css-BHfxGWOW.js";
import { j as Q, C as q } from "./aggregator-J7DZKy5x.js";
import { z as p } from "zod";
const F = (l = "", s = []) => {
  var n;
  if (s.length !== 0)
    return s.find((t) => t.tabKey === l) != null ? l : (n = s[s.length - 1]) == null ? void 0 : n.tabKey;
}, G = (l, s, n) => {
  if (s = s.trim(), s.length === 0) return n;
  const t = n.find((e) => e.tabKey === l);
  return t == null || t.name === s ? n : n.map((e) => e.tabKey === l ? { ...e, name: s } : e);
}, H = ({
  tabs: l,
  content: s,
  selected: n,
  onSelect: t
}) => {
  var u;
  const [e, r] = Q({
    initial: n ?? ((u = l[0]) == null ? void 0 : u.tabKey) ?? "",
    value: n,
    onChange: t
  }), a = S(n ?? e), i = v(
    (d) => {
      r(d), a.current == null && (t == null || t(d));
    },
    [e, t]
  );
  return {
    tabs: l,
    selected: e,
    content: s,
    onSelect: i
  };
}, y = R({ tabs: [] }), N = () => Z(y), J = ({
  id: l,
  content: s,
  children: n,
  onSelect: t,
  selected: e,
  selectedAltColor: r,
  closable: a,
  tabs: i,
  onClose: u,
  onDragStart: d,
  onDragEnd: b,
  onCreate: x,
  onRename: C,
  emptyContent: g,
  className: j,
  onDragOver: f,
  onDrop: B,
  size: m = "medium",
  direction: T = "y",
  ...I
}) => /* @__PURE__ */ o.jsx(
  P,
  {
    id: l,
    empty: !0,
    className: c(c.B("tabs"), j),
    onDragOver: f,
    onDrop: B,
    direction: T,
    ...I,
    children: /* @__PURE__ */ o.jsxs(
      y.Provider,
      {
        value: {
          tabs: i,
          emptyContent: g,
          selected: e,
          closable: a,
          content: n ?? s,
          onSelect: t,
          onClose: u,
          onDragStart: d,
          onDragEnd: b,
          onRename: C,
          onCreate: x,
          onDrop: B
        },
        children: [
          /* @__PURE__ */ o.jsx(
            W,
            {
              size: m,
              direction: A.swap(T),
              altColor: r
            }
          ),
          /* @__PURE__ */ o.jsx(E, {})
        ]
      }
    )
  }
), U = y.Provider, E = () => {
  const {
    tabs: l,
    selected: s,
    content: n,
    emptyContent: t,
    onSelect: e
  } = N();
  let r = null;
  const a = l.find((i) => i.tabKey === s);
  return s == null || a == null ? t ?? null : (n != null ? typeof n == "function" ? r = n(a) : r = n : a.content != null && (r = a.content), /* @__PURE__ */ o.jsx("div", { className: c.B("tabs-content"), onClick: () => e == null ? void 0 : e(s), children: r }));
}, h = "tabs-selector", W = ({
  className: l,
  altColor: s = !1,
  size: n = "medium",
  direction: t = "x",
  ...e
}) => {
  const {
    tabs: r,
    selected: a,
    onSelect: i,
    onClose: u,
    closable: d,
    onDragEnd: b,
    onDragStart: x,
    onDrop: C,
    onRename: g,
    onCreate: j
  } = N();
  return /* @__PURE__ */ o.jsxs(
    P,
    {
      className: c(c.B(h), c.size(n), l),
      align: "center",
      justify: "spaceBetween",
      onDrop: C,
      empty: !0,
      direction: t,
      ...e,
      children: [
        /* @__PURE__ */ o.jsx(P, { direction: t, className: c.BE(h, "tabs"), empty: !0, children: r.map((f) => /* @__PURE__ */ o.jsx(
          V,
          {
            selected: a,
            altColor: s,
            onSelect: i,
            onClose: u,
            onDragStart: x,
            onDragEnd: b,
            onRename: g,
            closable: f.closable ?? d,
            size: n,
            ...f
          },
          f.tabKey
        )) }),
        j != null && /* @__PURE__ */ o.jsx(P, { className: c.BE(h, "actions"), children: /* @__PURE__ */ o.jsx(_, { size: n, sharp: !0, onClick: j, children: /* @__PURE__ */ o.jsx(k.Add, {}) }) })
      ]
    }
  );
}, V = ({
  selected: l,
  altColor: s = !1,
  onSelect: n,
  onClose: t,
  tabKey: e,
  name: r,
  onDragStart: a,
  onDragEnd: i,
  onRename: u,
  closable: d = !0,
  icon: b,
  size: x,
  editable: C = !0
}) => {
  const g = v(
    (m) => a == null ? void 0 : a(m, { tabKey: e, name: r }),
    [a, e, r]
  ), j = v(
    (m) => i == null ? void 0 : i(m, { tabKey: e, name: r }),
    [i, e, r]
  ), f = v(
    (m) => {
      m.stopPropagation(), t == null || t(e);
    },
    [t, e]
  ), B = v(() => n == null ? void 0 : n(e), [n, e]);
  return /* @__PURE__ */ o.jsxs(
    z,
    {
      size: x,
      id: e,
      className: c(
        c.BE(h, "btn"),
        u == null && c.BEM(h, "btn", "uneditable"),
        c.selected(l === e),
        c.altColor(s),
        // TODO: this line
        d && t != null && c.BEM(h, "btn", "closable")
      ),
      draggable: !0,
      direction: "x",
      justify: "center",
      align: "center",
      onClick: B,
      onDragStart: g,
      onDragEnd: j,
      bordered: !1,
      rounded: !1,
      children: [
        /* @__PURE__ */ o.jsx(
          X,
          {
            name: r,
            tabKey: e,
            onRename: u,
            icon: b,
            editable: C,
            level: q[x]
          }
        ),
        d && t != null && /* @__PURE__ */ o.jsx(_, { onClick: f, children: /* @__PURE__ */ o.jsx(k.Close, { "aria-label": "pluto-tabs__close" }) })
      ]
    }
  );
}, X = ({
  onRename: l,
  name: s,
  tabKey: n,
  icon: t,
  editable: e = !0,
  ...r
}) => l == null || !e ? t != null ? /* @__PURE__ */ o.jsx(M, { startIcon: t, noWrap: !0, ...r, children: s }) : /* @__PURE__ */ o.jsx(L, { noWrap: !0, ...r, children: s }) : /* @__PURE__ */ o.jsx(
  O,
  {
    onChange: (a) => l(n, a),
    value: s,
    noWrap: !0,
    ...r
  }
), w = p.object({
  tabKey: p.string(),
  name: p.string(),
  closable: p.boolean().optional(),
  icon: p.unknown().optional(),
  editable: p.boolean().optional()
}), Y = w.extend({
  content: p.unknown().optional()
}), re = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  Content: E,
  Provider: U,
  Selector: W,
  Tabs: J,
  TabsContext: y,
  rename: G,
  resetSelection: F,
  specZ: w,
  tabZ: Y,
  useStatic: H,
  useTabsContext: N
}, Symbol.toStringTag, { value: "Module" }));
export {
  J as T,
  G as a,
  re as e,
  F as r,
  Y as t,
  H as u
};
