import { j as s } from "./jsx-runtime-OVHDjVDe.js";
import { C as r, j as y, Q as z, i as G, p as H } from "./css-D6ZBcSwh.js";
import { createContext as Q, use as U, useState as I, useCallback as j } from "react";
import { B as w } from "./Button-BFoGC4WW.js";
import { a as N } from "./Box-D5Qv__kr.js";
import { y as q, z as J, B as W, D as Y, L as K } from "./Text-BzTkWLeU.js";
import { a as ee, C as te } from "./Menu-DqkB9vpH.js";
import { b as ne, a as oe } from "./Animated-M9ynNzcI.js";
import { T as se, E as le, c as re } from "./Editable-CxgdRDPb.js";
import { C as k, u as ae } from "./types-BNX_Jj6k.js";
import "./Provider--RxgAoVG.js";
import "./provider-C8In649C.js";
import { z as p } from "zod";
const A = Q({ tabs: [] }), R = A, M = () => U(A), _ = "tabs-selector", V = ({
  className: o,
  altColor: t = !1,
  size: e = "medium",
  direction: l = "x",
  contextMenu: n,
  addTooltip: a,
  actions: c,
  ...d
}) => {
  const {
    tabs: g,
    selected: v,
    onSelect: E,
    onClose: x,
    closable: b,
    onDragEnd: T,
    onDragStart: D,
    onDrop: m,
    onRename: S,
    onCreate: h
  } = M(), C = ee(), [O, f] = I(!1);
  return /* @__PURE__ */ s.jsxs(s.Fragment, { children: [
    n != null && /* @__PURE__ */ s.jsx(
      te,
      {
        style: { height: "fit-content" },
        ...C,
        menu: n
      }
    ),
    /* @__PURE__ */ s.jsxs(
      N,
      {
        className: r(
          r.B(_),
          o,
          C.className,
          O && r.M("drag-over")
        ),
        size: e,
        align: "center",
        justify: "between",
        empty: !0,
        direction: l,
        onContextMenu: C.open,
        onDrop: m,
        ...d,
        children: [
          /* @__PURE__ */ s.jsxs(N, { direction: l, className: r.BE(_, "tabs"), empty: !0, children: [
            g.map((i) => /* @__PURE__ */ s.jsx(
              de,
              {
                selected: v,
                altColor: t,
                onSelect: E,
                onClose: x,
                onDragStart: D,
                onDragEnd: T,
                onRename: S,
                closable: i.closable ?? b,
                size: e,
                ...i
              },
              i.tabKey
            )),
            m != null && /* @__PURE__ */ s.jsx(
              N,
              {
                onDragOver: () => f(!0),
                onDragLeave: () => f(!1),
                onDragEnd: () => f(!1),
                onDrop: () => f(!1),
                grow: !0
              }
            )
          ] }),
          (c != null || h != null) && /* @__PURE__ */ s.jsxs(N, { className: r.BE(_, "actions"), children: [
            h != null && /* @__PURE__ */ s.jsx(
              w,
              {
                size: e,
                sharp: !0,
                onClick: h,
                tooltip: a,
                variant: "text",
                children: /* @__PURE__ */ s.jsx(q, {})
              }
            ),
            c
          ] })
        ]
      }
    )
  ] });
}, ce = ({ unsavedChanges: o, ...t }) => {
  const e = /* @__PURE__ */ s.jsx(W, { ...t });
  return o ? /* @__PURE__ */ s.jsxs(s.Fragment, { children: [
    /* @__PURE__ */ s.jsx(Y, {}),
    e
  ] }) : e;
}, Z = r.BE("tabs-selector", "btn"), ie = (o) => {
  if (!(o.target instanceof HTMLElement)) return "right";
  const t = o.target.closest(`.${Z}`);
  if (t == null) return "right";
  const e = y.construct(t), l = z.construct(o);
  return G.Scale.scale(y.left(e), y.right(e)).scale(0, 1).pos(l.x) < 0.5 ? "left" : "right";
}, ue = ({ loading: o, icon: t, level: e = "p" }) => (o && (t = /* @__PURE__ */ s.jsx(K, {})), J(t, {
  className: r.BE(_, "icon"),
  style: {
    color: r.colorVar(9),
    height: r.levelSizeVar(e),
    width: r.levelSizeVar(e)
  }
})), de = ({
  selected: o,
  altColor: t = !1,
  onSelect: e,
  onClose: l,
  tabKey: n,
  name: a,
  onDragStart: c,
  onDragEnd: d,
  onRename: g,
  closable: v = !0,
  icon: E,
  size: x,
  editable: b = !0,
  unsavedChanges: T = !1,
  loading: D = !1,
  onDrop: m
}) => {
  const S = j(
    (u) => c?.(u, { tabKey: n, name: a }),
    [c, n, a]
  ), h = j(
    (u) => d?.(u, { tabKey: n, name: a }),
    [d, n, a]
  ), C = j(
    (u) => {
      u.preventDefault(), u.stopPropagation(), l?.(n);
    },
    [l, n]
  ), O = j(() => e?.(n), [e, n]), [f, i] = I(null), L = j(
    (u) => {
      i(ie(u));
    },
    [i, m]
  ), P = j(
    (u) => {
      m?.(u), i(null);
    },
    [m, i]
  ), B = o === n, $ = k[x];
  return /* @__PURE__ */ s.jsxs(
    w,
    {
      el: "div",
      size: x,
      id: n,
      variant: "text",
      sharp: !0,
      className: r(
        ne,
        Z,
        B && oe,
        r.selected(B),
        r.altColor(t),
        v && l != null && r.M("closable"),
        r.editable(b && g != null),
        f != null && r.M("drag-over"),
        f != null && r.loc(f)
      ),
      draggable: !0,
      justify: "center",
      align: "center",
      empty: !0,
      tabIndex: 0,
      preventClick: B,
      onClick: O,
      onDragOver: L,
      onDrop: P,
      onDragLeave: () => i(null),
      onDragStart: S,
      onDragEnd: (u) => {
        i(null), h(u);
      },
      bordered: !1,
      rounded: !1,
      children: [
        /* @__PURE__ */ s.jsx(ue, { loading: D, icon: E, level: $ }),
        /* @__PURE__ */ s.jsx(
          fe,
          {
            name: a,
            tabKey: n,
            onRename: g,
            editable: b,
            level: k[x]
          }
        ),
        v && l != null && /* @__PURE__ */ s.jsx(
          w,
          {
            "aria-label": "pluto-tabs__close",
            onClick: C,
            className: r.E("close"),
            variant: "text",
            sharp: !0,
            children: /* @__PURE__ */ s.jsx(ce, { unsavedChanges: T })
          }
        )
      ]
    }
  );
}, fe = ({
  onRename: o,
  name: t,
  tabKey: e,
  editable: l = !0,
  level: n
}) => o == null || !l ? /* @__PURE__ */ s.jsx(se, { overflow: "ellipsis", level: n, children: t }) : /* @__PURE__ */ s.jsx(
  le,
  {
    level: n,
    id: r.B(`tab-${e}`),
    onChange: (a) => o?.(e, a),
    value: t,
    overflow: "ellipsis"
  }
), pe = (o = "", t = []) => {
  if (t.length !== 0)
    return t.find((e) => e.tabKey === o) != null ? o : t[t.length - 1]?.tabKey;
}, ge = (o, t, e) => {
  if (t = t.trim(), t.length === 0) return e;
  const l = e.find((n) => n.tabKey === o);
  return l == null || l.name === t ? e : e.map((n) => n.tabKey === o ? { ...n, name: t } : n);
}, me = ({
  tabs: o,
  content: t,
  selected: e,
  onSelect: l
}) => {
  const [n, a] = ae({
    initial: e ?? o[0]?.tabKey ?? "",
    value: e,
    onChange: l
  }), c = re(e ?? n), d = j(
    (g) => {
      a(g), c.current == null && l?.(g);
    },
    [n, l]
  );
  return {
    tabs: o,
    selected: n,
    content: t,
    onSelect: d
  };
}, xe = ({
  id: o,
  content: t,
  children: e,
  onSelect: l,
  selected: n,
  selectedAltColor: a,
  closable: c,
  tabs: d,
  onClose: g,
  onDragStart: v,
  onDragEnd: E,
  onCreate: x,
  onRename: b,
  emptyContent: T,
  className: D,
  onDragOver: m,
  onDrop: S,
  contextMenu: h,
  actions: C,
  addTooltip: O,
  size: f = "medium",
  direction: i = "y",
  x: L,
  y: P,
  ...B
}) => /* @__PURE__ */ s.jsx(
  N,
  {
    id: o,
    empty: !0,
    className: r(r.B("tabs"), D),
    direction: i,
    x: L,
    y: P,
    onDragOver: m,
    onDrop: S,
    ...B,
    children: /* @__PURE__ */ s.jsxs(
      R,
      {
        value: {
          tabs: d,
          emptyContent: T,
          selected: n,
          closable: c,
          content: e ?? t,
          onSelect: l,
          onClose: g,
          onDragStart: v,
          onDragEnd: E,
          onRename: b,
          onCreate: x,
          onDrop: S
        },
        children: [
          /* @__PURE__ */ s.jsx(
            V,
            {
              size: f,
              direction: H.swap(i),
              altColor: a,
              contextMenu: h,
              addTooltip: O,
              actions: C
            }
          ),
          /* @__PURE__ */ s.jsx(F, {})
        ]
      }
    )
  }
), F = () => {
  const { tabs: o, selected: t, content: e, emptyContent: l, onSelect: n } = M();
  let a = null;
  const c = o.find((d) => d.tabKey === t);
  return t == null || c == null ? a = l ?? null : e != null ? typeof e == "function" ? a = e(c) : a = e : c.content != null && (a = c.content), /* @__PURE__ */ s.jsx(
    "div",
    {
      className: r.B("tabs-content"),
      onClick: () => t != null && n?.(t),
      children: a
    }
  );
}, X = p.object({
  tabKey: p.string(),
  name: p.string(),
  closable: p.boolean().optional(),
  icon: p.unknown().optional(),
  editable: p.boolean().optional(),
  visible: p.boolean().optional(),
  unsavedChanges: p.boolean().optional(),
  loading: p.boolean().optional()
}), he = X.extend({
  content: p.unknown().optional()
}), Pe = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  Content: F,
  Provider: R,
  Selector: V,
  Tabs: xe,
  rename: ge,
  resetSelection: pe,
  specZ: X,
  tabZ: he,
  useContext: M,
  useStatic: me
}, Symbol.toStringTag, { value: "Module" }));
export {
  xe as T,
  ge as a,
  Pe as e,
  pe as r,
  he as t,
  me as u
};
