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