"use client";
import { $getSelection as T, $isRangeSelection as v, $isTextNode as N } from "../../lexical/Lexical.prod.js";
function h(g) {
  const x = window.location.origin, d = (a) => {
    if (a.origin !== x) return;
    const R = g.getRootElement();
    if (document.activeElement !== R) return;
    const f = a.data;
    if (typeof f == "string") {
      let o;
      try {
        o = JSON.parse(f);
      } catch {
        return;
      }
      if (o && o.protocol === "nuanria_messaging" && o.type === "request") {
        const r = o.payload;
        if (r && r.functionId === "makeChanges") {
          const u = r.args;
          if (u) {
            const [c, m, p, l, S, y] = u;
            g.update((() => {
              const i = T();
              if (v(i)) {
                const w = i.anchor;
                let e = w.getNode(), t = 0, n = 0;
                if (N(e) && c >= 0 && m >= 0 && (t = c, n = c + m, i.setTextNodeRange(e, t, e, n)), t === n && p === "" || (i.insertRawText(p), e = w.getNode()), N(e)) {
                  t = l, n = l + S;
                  const s = e.getTextContentSize();
                  t = t > s ? s : t, n = n > s ? s : n, i.setTextNodeRange(e, t, e, n);
                }
                a.stopImmediatePropagation();
              }
            }));
          }
        }
      }
    }
  };
  return window.addEventListener("message", d, !0), () => {
    window.removeEventListener("message", d, !0);
  };
}
export {
  h as registerDragonSupport
};
