import { jsx as c, jsxs as w, Fragment as Q } from "react/jsx-runtime";
import { bj as X, al as Y, m as ee } from "./use-page-data.BYLN5MSa.js";
import * as a from "react";
import { useRef as oe, Suspense as te, lazy as re } from "react";
import { a as ne, c as ae, u as y, P as N, b as m, g as se, y as b } from "./index.D1n9HtNk.js";
import { t as R, c as ce, v as ie } from "./radius.prop.CaJVv35S.js";
import { h as le, R as pe, u as ue, F as de, a as _ } from "./icons.fQeDMwI8.js";
import { n as fe, e as ve } from "./base-button.DeoDB8N9.js";
import { c as F, a as me, u as he, A as D, P as S, b as ge, D as Pe, C as Ce, d as xe, R as Re } from "./theme.BwnlJUJD.js";
import { a as E, c as O } from "./twind.Cg2g8xBZ.js";
import { b as Ae } from "./index.CbebiNuJ.js";
var C = "Popover", [M] = ae(C, [
  F
]), h = F(), [Ee, p] = M(C), T = (e) => {
  const {
    __scopePopover: o,
    children: t,
    open: s,
    defaultOpen: n,
    onOpenChange: r,
    modal: i = !1
  } = e, l = h(o), f = a.useRef(null), [v, g] = a.useState(!1), [P, u] = ne({
    prop: s,
    defaultProp: n ?? !1,
    onChange: r,
    caller: C
  });
  return /* @__PURE__ */ c(me, { ...l, children: /* @__PURE__ */ c(
    Ee,
    {
      scope: o,
      contentId: he(),
      triggerRef: f,
      open: P,
      onOpenChange: u,
      onOpenToggle: a.useCallback(() => u((x) => !x), [u]),
      hasCustomAnchor: v,
      onCustomAnchorAdd: a.useCallback(() => g(!0), []),
      onCustomAnchorRemove: a.useCallback(() => g(!1), []),
      modal: i,
      children: t
    }
  ) });
};
T.displayName = C;
var k = "PopoverAnchor", I = a.forwardRef(
  (e, o) => {
    const { __scopePopover: t, ...s } = e, n = p(k, t), r = h(t), { onCustomAnchorAdd: i, onCustomAnchorRemove: l } = n;
    return a.useEffect(() => (i(), () => l()), [i, l]), /* @__PURE__ */ c(D, { ...r, ...s, ref: o });
  }
);
I.displayName = k;
var H = "PopoverTrigger", B = a.forwardRef(
  (e, o) => {
    const { __scopePopover: t, ...s } = e, n = p(H, t), r = h(t), i = y(o, n.triggerRef), l = /* @__PURE__ */ c(
      N.button,
      {
        type: "button",
        "aria-haspopup": "dialog",
        "aria-expanded": n.open,
        "aria-controls": n.contentId,
        "data-state": G(n.open),
        ...s,
        ref: i,
        onClick: m(e.onClick, n.onOpenToggle)
      }
    );
    return n.hasCustomAnchor ? l : /* @__PURE__ */ c(D, { asChild: !0, ...r, children: l });
  }
);
B.displayName = H;
var A = "PopoverPortal", [Oe, we] = M(A, {
  forceMount: void 0
}), L = (e) => {
  const { __scopePopover: o, forceMount: t, children: s, container: n } = e, r = p(A, o);
  return /* @__PURE__ */ c(Oe, { scope: o, forceMount: t, children: /* @__PURE__ */ c(S, { present: t || r.open, children: /* @__PURE__ */ c(ge, { asChild: !0, container: n, children: s }) }) });
};
L.displayName = A;
var d = "PopoverContent", W = a.forwardRef(
  (e, o) => {
    const t = we(d, e.__scopePopover), { forceMount: s = t.forceMount, ...n } = e, r = p(d, e.__scopePopover);
    return /* @__PURE__ */ c(S, { present: s || r.open, children: r.modal ? /* @__PURE__ */ c(Ne, { ...n, ref: o }) : /* @__PURE__ */ c(be, { ...n, ref: o }) });
  }
);
W.displayName = d;
var ye = se("PopoverContent.RemoveScroll"), Ne = a.forwardRef(
  (e, o) => {
    const t = p(d, e.__scopePopover), s = a.useRef(null), n = y(o, s), r = a.useRef(!1);
    return a.useEffect(() => {
      const i = s.current;
      if (i) return le(i);
    }, []), /* @__PURE__ */ c(pe, { as: ye, allowPinchZoom: !0, children: /* @__PURE__ */ c(
      $,
      {
        ...e,
        ref: n,
        trapFocus: t.open,
        disableOutsidePointerEvents: !0,
        onCloseAutoFocus: m(e.onCloseAutoFocus, (i) => {
          i.preventDefault(), r.current || t.triggerRef.current?.focus();
        }),
        onPointerDownOutside: m(
          e.onPointerDownOutside,
          (i) => {
            const l = i.detail.originalEvent, f = l.button === 0 && l.ctrlKey === !0, v = l.button === 2 || f;
            r.current = v;
          },
          { checkForDefaultPrevented: !1 }
        ),
        onFocusOutside: m(
          e.onFocusOutside,
          (i) => i.preventDefault(),
          { checkForDefaultPrevented: !1 }
        )
      }
    ) });
  }
), be = a.forwardRef(
  (e, o) => {
    const t = p(d, e.__scopePopover), s = a.useRef(!1), n = a.useRef(!1);
    return /* @__PURE__ */ c(
      $,
      {
        ...e,
        ref: o,
        trapFocus: !1,
        disableOutsidePointerEvents: !1,
        onCloseAutoFocus: (r) => {
          e.onCloseAutoFocus?.(r), r.defaultPrevented || (s.current || t.triggerRef.current?.focus(), r.preventDefault()), s.current = !1, n.current = !1;
        },
        onInteractOutside: (r) => {
          e.onInteractOutside?.(r), r.defaultPrevented || (s.current = !0, r.detail.originalEvent.type === "pointerdown" && (n.current = !0));
          const i = r.target;
          t.triggerRef.current?.contains(i) && r.preventDefault(), r.detail.originalEvent.type === "focusin" && n.current && r.preventDefault();
        }
      }
    );
  }
), $ = a.forwardRef(
  (e, o) => {
    const {
      __scopePopover: t,
      trapFocus: s,
      onOpenAutoFocus: n,
      onCloseAutoFocus: r,
      disableOutsidePointerEvents: i,
      onEscapeKeyDown: l,
      onPointerDownOutside: f,
      onFocusOutside: v,
      onInteractOutside: g,
      ...P
    } = e, u = p(d, t), x = h(t);
    return ue(), /* @__PURE__ */ c(
      de,
      {
        asChild: !0,
        loop: !0,
        trapped: s,
        onMountAutoFocus: n,
        onUnmountAutoFocus: r,
        children: /* @__PURE__ */ c(
          Pe,
          {
            asChild: !0,
            disableOutsidePointerEvents: i,
            onInteractOutside: g,
            onEscapeKeyDown: l,
            onPointerDownOutside: f,
            onFocusOutside: v,
            onDismiss: () => u.onOpenChange(!1),
            children: /* @__PURE__ */ c(
              Ce,
              {
                "data-state": G(u.open),
                role: "dialog",
                id: u.contentId,
                ...x,
                ...P,
                ref: o,
                style: {
                  ...P.style,
                  "--radix-popover-content-transform-origin": "var(--radix-popper-transform-origin)",
                  "--radix-popover-content-available-width": "var(--radix-popper-available-width)",
                  "--radix-popover-content-available-height": "var(--radix-popper-available-height)",
                  "--radix-popover-trigger-width": "var(--radix-popper-anchor-width)",
                  "--radix-popover-trigger-height": "var(--radix-popper-anchor-height)"
                }
              }
            )
          }
        )
      }
    );
  }
), z = "PopoverClose", j = a.forwardRef(
  (e, o) => {
    const { __scopePopover: t, ...s } = e, n = p(z, t);
    return /* @__PURE__ */ c(
      N.button,
      {
        type: "button",
        ...s,
        ref: o,
        onClick: m(e.onClick, () => n.onOpenChange(!1))
      }
    );
  }
);
j.displayName = z;
var _e = "PopoverArrow", Fe = a.forwardRef(
  (e, o) => {
    const { __scopePopover: t, ...s } = e, n = h(t);
    return /* @__PURE__ */ c(xe, { ...n, ...s, ref: o });
  }
);
Fe.displayName = _e;
function G(e) {
  return e ? "open" : "closed";
}
var De = T, Se = I, Me = B, Te = L, ke = W, Ie = j;
const U = a.forwardRef(({ className: e, ...o }, t) => a.createElement(fe, { ...o, ref: t, className: b("rt-IconButton", e) }));
U.displayName = "IconButton";
const He = ["1", "2", "3", "4"], Be = { ...ce, size: { type: "enum", className: "rt-r-size", values: He, default: "2", responsive: !0 }, width: R.width, minWidth: R.minWidth, maxWidth: { ...R.maxWidth, default: "480px" }, ...ve }, K = (e) => a.createElement(De, { ...e });
K.displayName = "Popover.Root";
const q = a.forwardRef(({ children: e, ...o }, t) => a.createElement(Me, { ...o, ref: t, asChild: !0 }, _(e)));
q.displayName = "Popover.Trigger";
const V = a.forwardRef((e, o) => {
  const { className: t, forceMount: s, container: n, ...r } = ie(e, Be);
  return a.createElement(Te, { container: n, forceMount: s }, a.createElement(Re, { asChild: !0 }, a.createElement(ke, { align: "start", sideOffset: 8, collisionPadding: 10, ...r, ref: o, className: b("rt-PopperContent", "rt-PopoverContent", t) })));
});
V.displayName = "Popover.Content";
const Le = a.forwardRef(({ children: e, ...o }, t) => a.createElement(Ie, { ...o, ref: t, asChild: !0 }, _(e)));
Le.displayName = "Popover.Close";
const We = a.forwardRef(({ children: e, ...o }, t) => a.createElement(Se, { ...o, ref: t }));
We.displayName = "Popover.Anchor";
function Z(e) {
  return [
    {
      type: "mention",
      attrs: { "data-id": e, label: e }
    },
    {
      type: "text",
      text: " "
    }
  ];
}
function J(e, o) {
  console.log("Inserting content %s", o), e.chain().insertContent(o, {
    parseOptions: {
      preserveWhitespace: "full"
    }
  }).run();
}
function $e({
  brickId: e,
  onlyAlias: o,
  onFieldSelect: t
}) {
  return null;
}
function ze({
  editor: e,
  brickId: o,
  children: t,
  onFieldClick: s
}) {
  const n = X(o);
  return /* @__PURE__ */ w(K, { children: [
    /* @__PURE__ */ c(q, { children: t }),
    /* @__PURE__ */ c(V, { minWidth: "260px", side: "top", align: "start", size: "2", maxHeight: "70dvh", sideOffset: 10, children: /* @__PURE__ */ c($e, { onFieldSelect: (i) => {
      if (s?.(i), !e)
        return;
      const l = Z(i);
      J(e, l);
    }, brickId: o, onlyAlias: n }) })
  ] });
}
const je = re(() => import("./TextEditor.9VsHc2Dd.js"));
function eo(e, o, t = !1, s = !0) {
  const n = ee();
  if (s)
    return (r) => {
      console.log("Updating text editor props", e, o), t ? n.updateBrickProps(e, {
        [o]: r.editor.getText()
      }) : n.updateBrickProps(e, { [o]: r.editor.getHTML() });
    };
}
function oo({
  currentValue: e,
  brickId: o,
  schema: t,
  onChange: s,
  placeholder: n
}) {
  const r = oe(null), i = Ae(s, 300);
  return /* @__PURE__ */ w(Q, { children: [
    /* @__PURE__ */ c(
      "div",
      {
        className: E(
          "rounded border border-gray-300 px-2 py-[4px] text-sm flex-grow max-w-[calc(100%-34px)] bg-white",
          O({
            "&:has([contenteditable='true']:focus)": {
              outline: "1px solid var(--violet-8)",
              borderColor: "var(--violet-8)"
            }
          })
        ),
        children: /* @__PURE__ */ c(te, { fallback: null, children: /* @__PURE__ */ c(
          je,
          {
            content: e,
            brickId: o,
            ref: r,
            className: E(
              O({ lineHeight: "1.5" }),
              // @ts-ignore
              t["ui:multiline"] && `scrollbar-thin ${t["ui:textarea-class"] ?? "min-h-24"}`
            ),
            placeholder: t["ui:placeholder"] ?? n,
            spellCheck: !!t["ui:spellcheck"],
            singleline: !0,
            noMenuBar: !0,
            dynamic: !0,
            onChange: (l) => {
              i(l.editor.getText());
            }
          }
        ) })
      }
    ),
    /* @__PURE__ */ c(
      ze,
      {
        brickId: o,
        onFieldClick: (l) => {
          r.current?.editor && J(r.current.editor, Z(l));
        },
        children: /* @__PURE__ */ c(U, { variant: "surface", radius: "large", className: "!w-[31px] !h-[31px]", children: /* @__PURE__ */ c(Y, { className: "w-4 h-4" }) })
      }
    )
  ] });
}
export {
  ze as D,
  K as P,
  eo as a,
  V as i,
  U as o,
  q as s,
  oo as u
};
