import { j as x } from "./jsx-runtime-BbD0H88J.js";
import { useCallback as y, useRef as l, useState as N, isValidElement as v, Children as C, useLayoutEffect as z } from "react";
import { C as i } from "./css-BkBrZtKZ.js";
import { e as I, c as B } from "./types-B5-Tni3G.js";
import { a as U } from "./Box-DLO7l9hM.js";
const ie = (e) => {
  const t = l(I(e)), n = y((s) => {
    t.current = B(s, t.current);
  }, []);
  return [t, n];
}, q = (e) => {
  const t = l(e);
  return t.current = e, t;
}, le = (e) => {
  const t = l(!1), n = l(null);
  return t.current || (n.current = e(), t.current = !0), n;
}, W = (...e) => y(
  (t) => e.forEach((n) => {
    n != null && (typeof n == "function" ? n(t) : n.current = t);
  }, t),
  []
), ae = (e) => {
  const t = l(null), [n, s] = N(() => {
    const c = I(e);
    return t.current = c, c;
  }), u = y((c) => {
    s((a) => (t.current = B(c, a), t.current));
  }, []);
  return [n, u, t];
}, fe = (e) => {
  const t = l(void 0), n = t.current;
  return t.current = e, n;
}, O = (e) => (
  // The hydrate props check lets us avoid considering Astro slots as valid react
  // elements.
  v(e) && !("hydrate" in e.props)
), D = "://", X = `https${D}`, G = (e, t = !1) => e == null ? e : t && !e.includes(D) ? X + e : e, Y = (e) => {
  if (C.count(e) !== 1) {
    const t = C.toArray(e).filter(
      (n) => typeof n != "boolean"
    );
    if (t.length !== 1) return !1;
    e = t[0];
  }
  return typeof e == "string" ? e.length === 1 : !!(O(e) && typeof e.props == "object" && e.props != null && !("children" in e.props) && !("role" in e.props));
}, F = (e, t, n, s, u) => t ?? (u != null || s === "link" ? "a" : n ?? e ?? "p"), J = (e, t, n) => {
  const s = {
    ...e,
    fontWeight: t
  };
  return n != null && (s.WebkitLineClamp = n), s;
}, L = ({
  level: e = "p",
  className: t,
  style: n,
  weight: s,
  defaultEl: u,
  el: c,
  variant: a,
  overflow: d,
  href: S,
  autoFormatHref: T,
  status: f,
  lineClamp: p,
  ...o
}) => /* @__PURE__ */ x.jsx(
  U,
  {
    direction: "x",
    el: F(e, c, u, a, S),
    style: J(n, s, p),
    className: i(
      i.B("text"),
      a != null && i.BM("text", a),
      i.BM("text", e),
      d != null && i.BM("text", "overflow", d),
      p != null && i.BM("text", "line-clamp"),
      f != null && i.M("status", f),
      t
    ),
    square: Y(o.children),
    gap: "small",
    href: G(S, T),
    ...o
  }
), R = (e) => {
  e != null && (e.style.display = "none", e.offsetHeight, e.style.display = "");
}, Q = ["Escape", "Enter"], w = i.BM("text", "editable"), Z = 10, k = "renamed", V = "escaped", j = "start-editing", ee = (e, t) => {
  let n = 0;
  const s = () => {
    n++;
    const u = document.querySelector(`#${CSS.escape(e)}.${w}`);
    if (u == null || !u.classList.contains(w)) {
      if (n < Z) setTimeout(() => s(), 100);
      else throw new Error(`Could not find element with id ${e}`);
      return;
    }
    u.dispatchEvent(new Event(j)), u.setAttribute("contenteditable", "true"), t != null && (u.addEventListener(
      k,
      (c) => t(g(c.target), !0)
    ), u.addEventListener(
      V,
      (c) => t(g(c.target), !1)
    ));
  };
  s();
}, Ee = (e) => new Promise((t) => {
  ee(e, (s, u) => t([s, u]));
}), g = (e) => e.innerText.trim(), te = (e, t) => e == null || t == null ? !1 : e.maxInlineSize === t.maxInlineSize, ne = ({
  onChange: e,
  value: t,
  className: n,
  useEditableState: s = N,
  allowDoubleClick: u = !0,
  onDoubleClick: c,
  allowEmpty: a = !1,
  style: d,
  outline: S = !0,
  ...T
}) => {
  const [f, p] = s(!1), o = l(null), M = q(t), b = l(d), A = l(t);
  (b.current != null && !te(d, b.current) || t !== A.current) && (R(o.current), b.current = d, A.current = t);
  const h = (r) => {
    u && (p(!0), R(o.current)), c?.(r);
  }, _ = (r, m = !1) => {
    const E = g(r);
    M.current === E && (E.length > 0 || a) || (m || E.length === 0 && !a ? (r.innerText = t, r.dispatchEvent(new Event(V))) : (e?.(E), M.current = E, r.dispatchEvent(new Event(k))));
  }, H = (r) => {
    if (o.current == null || (R(o.current), !f || !Q.includes(r.key))) return;
    r.stopPropagation(), r.preventDefault();
    const m = o.current;
    o.current != null && (p(!1), _(m, r.key === "Escape"), m.blur());
  }, P = (r) => r.preventDefault();
  z(() => {
    if (o.current == null || !f) return;
    R(o.current);
    const { current: r } = o;
    r.focus();
    const m = document.createRange();
    m.selectNodeContents(r);
    const E = window.getSelection();
    E?.removeAllRanges(), E?.addRange(m);
  }, [f]), o.current !== null && !f && (o.current.innerHTML = t);
  const $ = y((r) => {
    r?.addEventListener(j, () => p(!0));
  }, []), K = W(o, $);
  return /* @__PURE__ */ x.jsx(
    L,
    {
      ref: K,
      className: i(
        n,
        i.BM("text", "editable"),
        S && i.M("outline")
      ),
      onBlur: () => {
        p(!1);
        const r = o.current;
        r != null && _(r);
      },
      onKeyDown: H,
      onKeyUp: P,
      onDoubleClick: h,
      contentEditable: f,
      suppressContentEditableWarning: !0,
      style: d,
      ...T,
      children: t
    }
  );
}, de = ({
  onChange: e,
  disabled: t = !1,
  value: n,
  allowDoubleClick: s,
  ...u
}) => t || e == null || e === !1 ? /* @__PURE__ */ x.jsx(L, { ...u, children: n }) : (e === !0 && (e = () => {
}), /* @__PURE__ */ x.jsx(
  ne,
  {
    allowDoubleClick: s,
    onChange: e,
    value: n,
    ...u
  }
));
export {
  ne as E,
  de as M,
  L as T,
  Ee as a,
  le as b,
  q as c,
  ae as d,
  ee as e,
  W as f,
  fe as g,
  Y as i,
  R as t,
  ie as u
};
