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