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