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