import { j as m } from "./jsx-runtime-tc70JA_2.js";
import { TimeSpan as g, location as a, xy as M, box as d, color as V, record as te } from "@synnaxlabs/x";
import { useState as Y, useRef as I, useCallback as v, useMemo as W, createElement as ne, useId as oe, cloneElement as se, isValidElement as re } from "react";
import { C as c } from "./css-D90kZTM8.js";
import { c as le, L as ce, T as ie, F as ue, G as ae } from "./Text-2MISZsc5.js";
import { f as de, T as K, i as me } from "./Editable-C17ILP50.js";
import { a as fe, C as pe, d as Te } from "./types-CGRCBVD4.js";
import { a as xe } from "./Provider-jFWpR0CX.js";
import "./provider-Bt-E2O6g.js";
import { createPortal as Ee } from "react-dom";
import { u as ye } from "./hooks-QNj6PdUo.js";
const [ge, Se] = le({
  defaultValue: { delay: g.milliseconds(750), startAccelerating: () => {
  } },
  displayName: "Tooltip.Context"
}), He = ({
  delay: e = g.milliseconds(700),
  accelerate: t = !0,
  // Disabling this for now because it's annoying.
  acceleratedDelay: o = g.minutes(60),
  accelerationDelay: s = g.seconds(0),
  children: i
}) => {
  const [r, n] = Y(!1), u = I(null), E = v(() => {
    r || !t || (n(!0), u.current = setTimeout(() => {
      n(!1);
    }, new g(s).milliseconds));
  }, [r, s]), y = W(
    () => r ? o : e,
    [r, o, e]
  ), p = W(
    () => ({ delay: y, startAccelerating: E }),
    [y, E]
  );
  return /* @__PURE__ */ m.jsx(ge, { value: p, children: i });
}, Ue = (e) => ({ key: t, ...o }) => /* @__PURE__ */ ne(e, { ...o, key: t }), Ce = (e) => typeof e == "function", Le = 150, Q = ["top", "bottom"], z = ["left", "right"], ee = [
  ...z,
  ...Q
], J = [
  ...ee,
  "center"
], ve = {
  [a.xyToString(a.TOP_RIGHT)]: (e, t) => M.translate(e, "x", -d.width(t)),
  [a.xyToString(a.TOP_LEFT)]: (e, t) => M.translate(e, "x", d.width(t)),
  [a.xyToString(a.BOTTOM_RIGHT)]: (e, t) => M.translate(e, "x", -d.width(t)),
  [a.xyToString(a.BOTTOM_LEFT)]: (e, t) => M.translate(e, "x", d.width(t))
}, U = (e, t, o) => {
  for (const s of o)
    if (Math.abs(d.loc(t, s) - d.loc(e, s)) > Le) return s;
  return o[0];
}, Me = (e, t, o) => {
  const s = a.location.safeParse(e), i = (n) => {
    let u;
    return n === "center" ? u = ee : a.isX(n) ? u = ["center", ...Q] : u = ["center", ...z], a.construct(U(t, o, u));
  };
  if (s.success)
    return a.constructXY(s.data, i(s.data));
  if (e != null) {
    const n = { ...e };
    return n.x == null && n.y != null ? n.x = i(a.construct(n.y)) : n.y == null && n.x != null ? n.y = i(a.construct(n.x)) : n.x == null && n.y == null && (n.x = U(t, o, J), n.y = i(a.construct(n.x))), a.constructXY(n);
  }
  const r = U(t, o, J);
  return a.constructXY(r, i(r));
}, he = (e, t) => {
  let o = e;
  for (; o != null; ) {
    if (o.id === t) return o;
    o = o.parentElement;
  }
  return e;
}, we = ({
  delay: e,
  children: t,
  location: o,
  hide: s = !1
}) => {
  const { startAccelerating: i, delay: r } = Se(), n = new g(e ?? r), [u, E, y] = de(null), [p, h] = Y(""), w = I(null), P = oe(), S = I(null), R = I(null), T = v((l) => {
    l == null && y.current == null || R.current != null || (l != null ? (E(l), R.current = setTimeout(() => {
      h(c.M("loaded")), R.current = null;
    }, 1)) : (h(""), R.current = setTimeout(() => {
      E(null), R.current = null;
    }, 500)));
  }, []), C = v(
    (l, H) => {
      if (!H || s)
        return S.current?.(), T(null);
      i();
      const f = d.construct(he(l.target, P));
      d.contains(f, M.construct(l)) || (S.current?.(), T(null));
      const O = d.construct(document.documentElement), N = Me(o, f, O);
      let L = d.xyLoc(f, N);
      const Z = ve[a.xyToString(N)];
      Z != null && (L = Z(L, f));
      const b = d.construct(document.body);
      T({
        location: N,
        position: M.translate(L, M.scale(d.topLeft(b), -1)),
        triggerDims: d.dims(f)
      }), S.current?.();
      const _ = (k) => {
        const $ = M.construct(k);
        d.contains(f, $) || (T(null), document.removeEventListener("mousemove", _), S.current = null, w.current != null && clearTimeout(w.current));
      };
      document.addEventListener("mousemove", _), S.current = () => document.removeEventListener("mousemove", _), document.addEventListener(
        "mousedown",
        () => {
          T(null), S.current?.();
        },
        { once: !0 }
      );
    },
    [i, o, s, P, n.milliseconds]
  );
  s && u != null && T(null);
  const F = v(
    (l) => {
      w.current = setTimeout(
        () => C(l, !0),
        n.milliseconds
      );
    },
    [C, n.milliseconds]
  ), j = v(
    (l) => {
      w.current != null && clearTimeout(w.current), C(l, !1);
    },
    [C]
  ), [D, A] = t, X = document.body;
  return /* @__PURE__ */ m.jsxs(m.Fragment, { children: [
    u != null && Ee(
      /* @__PURE__ */ m.jsx(
        "div",
        {
          className: c(
            c.B("tooltip"),
            c.loc(u.location.x),
            c.loc(u.location.y),
            p
          ),
          style: {
            [c.var("pos-x")]: c.px(u.position.x),
            [c.var("pos-y")]: c.px(u.position.y)
          },
          children: Ce(D) ? D(u) : Re(D)
        },
        P
      ),
      X
    ),
    se(A, {
      onMouseEnter: (l) => {
        F(l), A.props.onMouseEnter?.(l);
      },
      onMouseLeave: (l) => {
        j(l), A.props.onMouseLeave?.(l);
      },
      onMouseDown: v(
        (l) => {
          j(l), A.props.onMouseDown?.(l);
        },
        [C]
      )
    })
  ] });
}, Re = (e) => typeof e == "string" || typeof e == "number" || !re(e) ? /* @__PURE__ */ m.jsx(K, { level: "small", color: 11, children: e }) : e, Oe = (e) => {
  const t = ({
    tooltipDelay: o,
    tooltip: s,
    tooltipLocation: i,
    ...r
  }) => {
    const n = /* @__PURE__ */ m.jsx(e, { ...r });
    return s == null ? n : /* @__PURE__ */ m.jsxs(we, { delay: o, location: i, children: [
      s,
      n
    ] });
  };
  return t.displayName = `Tooltip.Wrap(${e.displayName ?? e.name})`, t;
}, B = "btn", Ne = (e, t) => {
  if (e === !0) return t;
  if (e != null && e !== !1) return e;
}, be = ({
  size: e,
  variant: t = "outlined",
  className: o,
  disabled: s,
  preventClick: i,
  level: r,
  trigger: n,
  triggerIndicator: u,
  onClickDelay: E = 0,
  onClick: y,
  color: p,
  status: h,
  style: w,
  onMouseDown: P,
  textColor: S,
  textVariant: R,
  tabIndex: T,
  contrast: C,
  children: F,
  defaultEl: j = "button",
  el: D,
  ghost: A,
  propagateClick: X = !1,
  href: l,
  ...H
}) => {
  const f = g.fromMilliseconds(E), O = s === !0 || h === "loading" || h === "disabled";
  t === "preview" && (i = !0), (s || i && T == null) && (T = -1);
  const N = (x) => {
    if (X || x.stopPropagation(), !(O || t === "preview" || i === !0) && f.isZero)
      return y?.(x);
  }, L = I(null), Z = (x) => {
    T == -1 && x.preventDefault(), P?.(x), !(O || t === "preview" || f.isZero) && (document.addEventListener(
      "mouseup",
      () => L.current != null && clearTimeout(L.current)
    ), L.current = setTimeout(() => {
      y?.(x), L.current = null;
    }, f.milliseconds));
  };
  ye({
    triggers: n,
    callback: v(
      ({ stage: x }) => {
        x !== "end" || O || t === "preview" || N(
          new MouseEvent("click")
        );
      },
      [N, O]
    )
  });
  let b = w;
  const _ = V.colorZ.safeParse(p), k = _.success && (t === "filled" || t === "outlined");
  if (k) {
    const x = xe();
    b = {
      ...b,
      [c.var("btn-color")]: V.rgbString(_.data),
      [c.var("btn-text-color")]: V.rgbCSS(
        V.pickByContrast(_.data, x.colors.text, x.colors.textInverted)
      )
    };
  }
  f.isZero || (b = {
    ...b,
    [c.var("btn-delay")]: `${f.seconds.toString()}s`
  }), e == null && r != null ? e = fe[r] : e != null && r == null ? r = pe[e] : j !== "div" && (e ??= "medium"), r ??= "p";
  const $ = h === "loading", G = me(F), q = Ne(u, n);
  return /* @__PURE__ */ m.jsxs(
    K,
    {
      el: D,
      defaultEl: j,
      direction: "x",
      className: c(
        c.B(B),
        C != null && c.BM(B, `contrast-${C}`),
        i === !0 && c.BM(B, "prevent-click"),
        t !== "preview" && c.disabled(O),
        c.BM(B, t),
        k && c.BM(B, "custom-color"),
        A && c.BM(B, "ghost"),
        o
      ),
      size: e,
      tabIndex: T,
      onClick: N,
      onMouseDown: Z,
      style: b,
      color: S,
      gap: e === "small" || e === "tiny" ? "small" : void 0,
      bordered: t !== "text",
      level: r,
      variant: R,
      square: G,
      overflow: "nowrap",
      status: h,
      href: l,
      ...te.purgeUndefined(H),
      children: [
        (!$ || !G) && F,
        $ && /* @__PURE__ */ m.jsx(ce, {}),
        q != null && /* @__PURE__ */ m.jsx(
          ie,
          {
            className: c.B("trigger-indicator"),
            "aria-label": "trigger-indicator",
            trigger: q,
            color: 9,
            gap: "tiny",
            level: Te(r)
          }
        )
      ]
    }
  );
}, _e = Oe(be), Ae = g.seconds(2).milliseconds, Ye = ({
  text: e,
  onCopy: t,
  onCopyError: o,
  copiedDuration: s = Ae,
  tooltip: i = "Copy",
  children: r,
  ...n
}) => {
  const [u, E] = Y(!1), y = v(() => {
    (async () => {
      try {
        const p = typeof e == "function" ? e() : e;
        await navigator.clipboard.writeText(p), E(!0), t?.(), setTimeout(
          () => E(!1),
          g.fromMilliseconds(s).milliseconds
        );
      } catch (p) {
        o?.(p instanceof Error ? p : new Error(String(p)));
      }
    })();
  }, [e, t, o, s]);
  return /* @__PURE__ */ m.jsxs(_e, { tooltip: u ? "Copied!" : i, onClick: y, ...n, children: [
    u ? /* @__PURE__ */ m.jsx(ue, {}) : /* @__PURE__ */ m.jsx(ae, {}),
    " ",
    r
  ] });
};
export {
  _e as B,
  Ye as C,
  we as D,
  He as a,
  Me as c,
  Re as f,
  Ce as i,
  Ue as r,
  Se as u,
  Oe as w
};
