import { jsx as t, jsxs as R, Fragment as W } from "react/jsx-runtime";
import { o as j, r as H, t as ae, X as ie, j as K, a as Z, L as _, y as $, g as X, z as V, w as U, E as le, U as B, i as q, W as re, b as ne, B as ce, c as T, P as z, s as ue, d as de, l as pe, _ as M, u as me, e as fe, x as O, f as oe, $ as ge, h as Y, N as he, k as be, O as ve, I as Ce, m as Ee, n as ke, p as Se, q as Le, v as we, A as xe, T as Ne, S as Ie, D as ye, C as Re } from "./main-Dx8wRugy.js";
import { useEffect as I, createContext as Fe, useRef as P, useContext as G, useMemo as Te, useState as E, useCallback as D, Suspense as De } from "react";
import { createPortal as se } from "react-dom";
function Me({ defaultSelection: e }) {
  const [n] = j();
  return I(() => {
    n.focus(() => {
      const a = document.activeElement, i = n.getRootElement();
      i === null || a !== null && i.contains(a) || i.focus({ preventScroll: !0 });
    }, { defaultSelection: e });
  }, [e, n]), null;
}
const J = [["Cat", "rgb(125, 50, 0)"], ["Dog", "rgb(100, 0, 0)"], ["Rabbit", "rgb(150, 0, 0)"], ["Frog", "rgb(200, 0, 0)"], ["Fox", "rgb(200, 75, 0)"], ["Hedgehog", "rgb(0, 75, 0)"], ["Pigeon", "rgb(0, 125, 0)"], ["Squirrel", "rgb(75, 100, 0)"], ["Bear", "rgb(125, 100, 0)"], ["Tiger", "rgb(0, 0, 150)"], ["Leopard", "rgb(0, 0, 200)"], ["Zebra", "rgb(0, 0, 250)"], ["Wolf", "rgb(0, 100, 150)"], ["Owl", "rgb(0, 100, 100)"], ["Gull", "rgb(100, 0, 100)"], ["Squid", "rgb(150, 0, 150)"]], Q = J[Math.floor(Math.random() * J.length)], _e = Fe(null);
function Pe() {
  return { color: Q[1], isCollabActive: !1, name: Q[0], yjsDocMap: /* @__PURE__ */ new Map() };
}
Pe();
function Ue({ initialEditor: e, children: n, initialNodes: a, initialTheme: i, skipCollabChecks: u, skipEditableListener: m }) {
  const r = P(!1), g = G(H);
  g == null && (function(f, ...w) {
    const o = new URL("https://lexical.dev/docs/error"), s = new URLSearchParams();
    s.append("code", f);
    for (const p of w) s.append("v", p);
    throw o.search = s.toString(), Error(`Minified Lexical error #${f}; visit ${o.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`);
  })(9);
  const [d, { getTheme: h }] = g, c = Te(() => {
    const f = i || h() || void 0, w = ae(g, f);
    f !== void 0 && (e._config.theme = f), e._parentEditor = e._parentEditor || d;
    const o = e._createEditorArgs, s = o && o.namespace;
    if (a) {
      s || (e._config.namespace = d._config.namespace);
      for (let p of a) {
        let C = null, l = null;
        if (typeof p != "function") {
          const y = p;
          p = y.replace, C = y.with, l = y.withKlass || null;
        }
        const L = ie(e, p.getType());
        e._nodes.set(p.getType(), { exportDOM: L ? L.exportDOM : void 0, klass: p, replace: C, replaceWithKlass: l, sharedNodeState: Z(p), transforms: K(p) });
      }
    } else if (o && o.nodes) s || (e._config.namespace = d._config.namespace);
    else {
      const p = e._nodes = new Map(d._nodes);
      s || (e._config.namespace = d._config.namespace);
      for (const [C, l] of p) e._nodes.set(C, { exportDOM: l.exportDOM, klass: l.klass, replace: l.replace, replaceWithKlass: l.replaceWithKlass, sharedNodeState: Z(l.klass), transforms: K(l.klass) });
    }
    return [e, w];
  }, []), S = G(_e), { isCollabActive: k, yjsDocMap: v } = S ?? {}, b = u || r.current || v && v.has(e.getKey());
  return I(() => {
    b && (r.current = !0);
  }, [b]), I(() => {
    if (!m) {
      const f = (w) => e.setEditable(w);
      return f(d.isEditable()), d.registerEditableListener(f);
    }
  }, [e, d, m]), t(H.Provider, { value: c, children: !k || b ? n : null });
}
const Be = 10, Ae = 5;
function ee(e, n, a, i = Be, u = Ae) {
  const m = a.parentElement;
  if (e === null || !m) {
    n.style.opacity = "0", n.style.transform = "translate(-10000px, -10000px)";
    return;
  }
  const r = n.getBoundingClientRect(), g = a.getBoundingClientRect(), d = m.getBoundingClientRect();
  let h = e.top - i, c = e.left - u;
  h < d.top && (h += r.height + e.height + i * 2), c + r.width > d.right && (c = d.right - r.width - u), h -= g.top, c -= g.left, n.style.opacity = "1", n.style.transform = `translate(${c}px, ${h}px)`;
}
function Oe({
  editor: e,
  isLink: n,
  setIsLink: a,
  anchorElem: i
}) {
  const u = P(null), m = P(null), [r, g] = E(""), [d, h] = E(""), [c, S] = E(!1), [k, v] = E(null), b = D(() => {
    const o = _();
    if ($(o)) {
      const L = X(o), y = L.getParent();
      U(y) ? g(y.getURL()) : U(L) ? g(L.getURL()) : g("");
    }
    const s = u.current, p = window.getSelection(), C = document.activeElement;
    if (s === null)
      return;
    const l = e.getRootElement();
    if (o !== null && p !== null && l !== null && l.contains(p.anchorNode) && e.isEditable()) {
      const L = p.focusNode?.parentElement?.getBoundingClientRect();
      L && (L.y += 40, ee(L, s, i)), v(o);
    } else (!C || C.className !== "link-input") && (l !== null && ee(null, s, i), v(null), S(!1), g(""));
    return !0;
  }, [i, e]);
  I(() => {
    const o = i.parentElement, s = () => {
      e.getEditorState().read(() => {
        b();
      });
    };
    return window.addEventListener("resize", s), o && o.addEventListener("scroll", s), () => {
      window.removeEventListener("resize", s), o && o.removeEventListener("scroll", s);
    };
  }, [i.parentElement, e, b]), I(() => B(
    e.registerUpdateListener(({ editorState: o }) => {
      o.read(() => {
        b();
      });
    }),
    e.registerCommand(
      q,
      () => (b(), !0),
      T
    ),
    e.registerCommand(
      ne,
      () => n ? (a(!1), !0) : !1,
      ce
    )
  ), [e, b, a, n]), I(() => {
    e.getEditorState().read(() => {
      b();
    });
  }, [e, b]), I(() => {
    c && m.current && m.current.focus();
  }, [c]);
  const f = (o) => {
    o.key === "Enter" ? (o.preventDefault(), w()) : o.key === "Escape" && (o.preventDefault(), S(!1));
  }, w = () => {
    k !== null && (r !== "" && e.dispatchCommand(z, ue(d)), S(!1));
  };
  return /* @__PURE__ */ t("div", { ref: u, className: "link-editor", children: n ? c ? /* @__PURE__ */ R(W, { children: [
    /* @__PURE__ */ t(
      "input",
      {
        ref: m,
        className: "link-input",
        value: d,
        onChange: (o) => {
          h(o.target.value);
        },
        onKeyDown: (o) => {
          f(o);
        }
      }
    ),
    /* @__PURE__ */ R("div", { children: [
      /* @__PURE__ */ t(
        "div",
        {
          className: "link-cancel",
          role: "button",
          tabIndex: 0,
          onMouseDown: (o) => o.preventDefault(),
          onClick: () => {
            S(!1);
          }
        }
      ),
      /* @__PURE__ */ t(
        "div",
        {
          className: "link-confirm",
          role: "button",
          tabIndex: 0,
          onMouseDown: (o) => o.preventDefault(),
          onClick: w
        }
      )
    ] })
  ] }) : /* @__PURE__ */ R("div", { className: "link-view", children: [
    /* @__PURE__ */ t("a", { href: r, target: "_blank", rel: "noopener noreferrer", children: r }),
    /* @__PURE__ */ t(
      "div",
      {
        className: "link-edit",
        role: "button",
        tabIndex: 0,
        onMouseDown: (o) => o.preventDefault(),
        onClick: () => {
          h(r), S(!0);
        }
      }
    ),
    /* @__PURE__ */ t(
      "div",
      {
        className: "link-trash",
        role: "button",
        tabIndex: 0,
        onMouseDown: (o) => o.preventDefault(),
        onClick: () => {
          e.dispatchCommand(z, null);
        }
      }
    )
  ] }) : null });
}
function $e(e, n) {
  const [a, i] = E(e), [u, m] = E(!1), r = D(() => {
    const g = _();
    if ($(g)) {
      const d = X(g), h = V(d, U), c = V(d, le);
      h != null && c == null ? m(!0) : m(!1);
    }
  }, []);
  return I(() => B(
    e.registerUpdateListener(({ editorState: g }) => {
      g.read(() => {
        r();
      });
    }),
    e.registerCommand(
      q,
      (g, d) => (r(), i(d), !1),
      re
    )
  ), [e, r]), se(
    /* @__PURE__ */ t(
      Oe,
      {
        editor: a,
        isLink: u,
        anchorElem: n,
        setIsLink: m
      }
    ),
    n
  );
}
function ze({
  anchorElem: e = document.body
}) {
  const [n] = j();
  return $e(n, e);
}
function We(e, n) {
  const a = e.getRangeAt(0);
  let i;
  if (e.anchorNode === n) {
    let u = n;
    for (; u.firstElementChild != null; )
      u = u.firstElementChild;
    i = u.getBoundingClientRect();
  } else
    i = a.getBoundingClientRect();
  return i;
}
const je = 10, qe = 5;
function Xe(e, n, a, i = je, u = qe) {
  const m = a.parentElement;
  if (e === null || !m) {
    n.style.opacity = "0", n.style.transform = "translate(-10000px, -10000px)";
    return;
  }
  const r = n.getBoundingClientRect(), g = a.getBoundingClientRect(), d = m.getBoundingClientRect();
  let h = e.top - r.height - i, c = e.left - u;
  h < d.top && (h += r.height + e.height + i * 2), c + r.width > d.right && (c = d.right - r.width - u), h -= g.top, c -= g.left, n.style.opacity = "1", n.style.transform = `translate(${c}px, ${h}px)`;
}
function He({
  editor: e,
  anchorElem: n,
  isLink: a,
  isBold: i,
  isItalic: u,
  isUnderline: m,
  isCode: r,
  isStrikethrough: g,
  isSubscript: d,
  isSuperscript: h
}) {
  const c = P(null), S = D(() => {
    a ? e.dispatchCommand(z, null) : e.dispatchCommand(z, "https://");
  }, [e, a]), k = 5;
  let v = { x: 0, y: 0 };
  function b(s) {
    v = { x: s.clientX, y: s.clientY };
  }
  function f(s) {
    c?.current && (s.buttons === 1 || s.buttons === 3) && Math.sqrt(
      Math.pow(s.clientX - v.x, 2) + Math.pow(s.clientY - v.y, 2)
    ) > k && (c.current.style.pointerEvents = "none");
  }
  function w(s) {
    c?.current && (c.current.style.pointerEvents = "auto");
  }
  I(() => {
    if (c?.current)
      return document.addEventListener("mousedown", b), document.addEventListener("mousemove", f), document.addEventListener("mouseup", w), () => {
        document.removeEventListener("mousedown", b), document.removeEventListener("mousemove", f), document.removeEventListener("mouseup", w);
      };
  }, [c]);
  const o = D(() => {
    const s = _(), p = c.current, C = window.getSelection();
    if (p === null)
      return;
    const l = e.getRootElement();
    if (s !== null && C !== null && !C.isCollapsed && l !== null && l.contains(C.anchorNode)) {
      const L = We(C, l);
      Xe(L, p, n);
    }
  }, [e, n]);
  return I(() => {
    const s = n.parentElement, p = () => {
      e.getEditorState().read(() => {
        o();
      });
    };
    return window.addEventListener("resize", p), s && s.addEventListener("scroll", p), () => {
      window.removeEventListener("resize", p), s && s.removeEventListener("scroll", p);
    };
  }, [e, o, n]), I(() => (e.getEditorState().read(() => {
    o();
  }), B(
    e.registerUpdateListener(({ editorState: s }) => {
      s.read(() => {
        o();
      });
    }),
    e.registerCommand(
      q,
      () => (o(), !1),
      T
    )
  )), [e, o]), /* @__PURE__ */ t("div", { ref: c, className: "floating-text-format-popup", children: e.isEditable() && /* @__PURE__ */ R(W, { children: [
    /* @__PURE__ */ t(
      "button",
      {
        onClick: () => {
          e.dispatchCommand(M, "bold");
        },
        className: "popup-item spaced " + (i ? "active" : ""),
        "aria-label": "Format text as bold",
        children: /* @__PURE__ */ t("i", { className: "format bold" })
      }
    ),
    /* @__PURE__ */ t(
      "button",
      {
        onClick: () => {
          e.dispatchCommand(M, "italic");
        },
        className: "popup-item spaced " + (u ? "active" : ""),
        "aria-label": "Format text as italics",
        children: /* @__PURE__ */ t("i", { className: "format italic" })
      }
    ),
    /* @__PURE__ */ t(
      "button",
      {
        onClick: () => {
          e.dispatchCommand(M, "underline");
        },
        className: "popup-item spaced " + (m ? "active" : ""),
        "aria-label": "Format text to underlined",
        children: /* @__PURE__ */ t("i", { className: "format underline" })
      }
    ),
    /* @__PURE__ */ t(
      "button",
      {
        onClick: () => {
          e.dispatchCommand(M, "strikethrough");
        },
        className: "popup-item spaced " + (g ? "active" : ""),
        "aria-label": "Format text with a strikethrough",
        children: /* @__PURE__ */ t("i", { className: "format strikethrough" })
      }
    ),
    /* @__PURE__ */ t(
      "button",
      {
        onClick: () => {
          e.dispatchCommand(M, "subscript");
        },
        className: "popup-item spaced " + (d ? "active" : ""),
        title: "Subscript",
        "aria-label": "Format Subscript",
        children: /* @__PURE__ */ t("i", { className: "format subscript" })
      }
    ),
    /* @__PURE__ */ t(
      "button",
      {
        onClick: () => {
          e.dispatchCommand(M, "superscript");
        },
        className: "popup-item spaced " + (h ? "active" : ""),
        title: "Superscript",
        "aria-label": "Format Superscript",
        children: /* @__PURE__ */ t("i", { className: "format superscript" })
      }
    ),
    /* @__PURE__ */ t(
      "button",
      {
        onClick: () => {
          e.dispatchCommand(M, "code");
        },
        className: "popup-item spaced " + (r ? "active" : ""),
        "aria-label": "Insert code block",
        children: /* @__PURE__ */ t("i", { className: "format code" })
      }
    ),
    /* @__PURE__ */ t(
      "button",
      {
        onClick: S,
        className: "popup-item spaced " + (a ? "active" : ""),
        "aria-label": "Insert link",
        children: /* @__PURE__ */ t("i", { className: "format link" })
      }
    )
  ] }) });
}
function Ke(e, n) {
  const [a, i] = E(!1), [u, m] = E(!1), [r, g] = E(!1), [d, h] = E(!1), [c, S] = E(!1), [k, v] = E(!1), [b, f] = E(!1), [w, o] = E(!1), [s, p] = E(!1), C = D(() => {
    e.getEditorState().read(() => {
      if (e.isComposing())
        return;
      const l = _(), L = window.getSelection(), y = e.getRootElement();
      if (L !== null && (!$(l) || y === null || !y.contains(L.anchorNode))) {
        i(!1);
        return;
      }
      if (!$(l))
        return;
      const N = X(l);
      g(l.hasFormat("bold")), h(l.hasFormat("italic")), S(l.hasFormat("underline")), v(l.hasFormat("strikethrough")), f(l.hasFormat("subscript")), o(l.hasFormat("superscript")), p(l.hasFormat("code"));
      const F = N.getParent();
      U(F) || U(N) ? m(!0) : m(!1), !de(l.anchor.getNode()) && l.getTextContent() !== "" ? i(pe(N)) : i(!1);
      const x = l.getTextContent().replace(/\n/g, "");
      if (!l.isCollapsed() && x === "") {
        i(!1);
        return;
      }
    });
  }, [e]);
  return I(() => (document.addEventListener("selectionchange", C), () => {
    document.removeEventListener("selectionchange", C);
  }), [C]), I(() => B(
    e.registerUpdateListener(() => {
      C();
    }),
    e.registerRootListener(() => {
      e.getRootElement() === null && i(!1);
    })
  ), [e, C]), !a || u ? null : se(
    /* @__PURE__ */ t(
      He,
      {
        editor: e,
        anchorElem: n,
        isLink: u,
        isBold: r,
        isItalic: d,
        isStrikethrough: k,
        isSubscript: b,
        isSuperscript: w,
        isUnderline: c,
        isCode: s
      }
    ),
    n
  );
}
function Ze({
  anchorElem: e = document.body
}) {
  const [n] = j();
  return Ke(n, e);
}
const te = /* @__PURE__ */ new Set();
function Ve(e) {
  if (!te.has(e))
    throw new Promise((n) => {
      const a = new Image();
      a.src = e, a.onload = () => {
        te.add(e), n(null);
      };
    });
}
function Ye({
  altText: e,
  className: n,
  imageRef: a,
  src: i,
  width: u,
  height: m,
  position: r
}) {
  return Ve(i), /* @__PURE__ */ t(
    "img",
    {
      className: n || void 0,
      src: i,
      alt: e,
      ref: a,
      "data-position": r,
      style: {
        height: m,
        width: u,
        display: "block"
      },
      draggable: "false"
    }
  );
}
function Ge({ activeEditor: e, nodeKey: n, onClose: a }) {
  const u = e.getEditorState().read(() => oe(n)), [m, r] = E(u.getAltText()), [g, d] = E(u.getShowCaption()), [h, c] = E(u.getPosition()), S = (b) => {
    d(b.target.checked);
  }, k = (b) => {
    c(b.target.value);
  }, v = () => {
    const b = { altText: m, showCaption: g, position: h };
    u && e.update(() => {
      u.update(b);
    }), a();
  };
  return /* @__PURE__ */ R(W, { children: [
    /* @__PURE__ */ t("div", { style: { marginBottom: "1em" }, children: /* @__PURE__ */ t(
      Ne,
      {
        label: "Alt Text",
        placeholder: "Descriptive alternative text",
        onChange: r,
        value: m,
        "data-test-id": "image-modal-alt-text-input"
      }
    ) }),
    /* @__PURE__ */ R(
      Ie,
      {
        style: { marginBottom: "1em", width: "208px" },
        value: h,
        label: "Position",
        name: "position",
        id: "position-select",
        onChange: k,
        children: [
          /* @__PURE__ */ t("option", { value: "left", children: "Left" }),
          /* @__PURE__ */ t("option", { value: "right", children: "Right" }),
          /* @__PURE__ */ t("option", { value: "full", children: "Full Width" })
        ]
      }
    ),
    /* @__PURE__ */ R("div", { className: "Input__wrapper", children: [
      /* @__PURE__ */ t(
        "input",
        {
          id: "caption",
          type: "checkbox",
          checked: g,
          onChange: S
        }
      ),
      /* @__PURE__ */ t("label", { htmlFor: "caption", children: "Show Caption" })
    ] }),
    /* @__PURE__ */ t(ye, { children: /* @__PURE__ */ t(
      Re,
      {
        "data-test-id": "image-modal-file-upload-btn",
        onClick: () => v(),
        children: "Confirm"
      }
    ) })
  ] });
}
function nt({
  src: e,
  altText: n,
  nodeKey: a,
  width: i,
  height: u,
  showCaption: m,
  caption: r,
  position: g
}) {
  const [d, h] = me(), c = P(null), S = P(null), [k, v, b] = fe(a), [f] = j(), [w, o] = E(null), s = P(null), p = D(
    (N) => {
      if (k && O(_())) {
        N.preventDefault();
        const x = oe(a);
        ge(x) && x?.remove(), v(!1);
      }
      return !1;
    },
    [k, a, v]
  ), C = D(
    (N) => {
      const F = _(), x = S.current;
      if (k && O(F) && F.getNodes().length === 1) {
        if (m)
          return Y(null), N.preventDefault(), r.focus(), !0;
        if (x !== null && x !== document.activeElement)
          return N.preventDefault(), x.focus(), !0;
      }
      return !1;
    },
    [r, k, m]
  ), l = D(
    (N) => s.current === r || S.current === N.target ? (Y(null), f.update(() => {
      v(!0);
      const F = f.getRootElement();
      F !== null && F.focus();
    }), !0) : !1,
    [r, f, v]
  );
  I(() => {
    let N = !0;
    const F = B(
      f.registerUpdateListener(({ editorState: x }) => {
        N && o(x.read(() => _()));
      }),
      f.registerCommand(
        q,
        (x, A) => (s.current = A, !1),
        T
      ),
      f.registerCommand(
        Ee,
        (x) => {
          const A = x;
          return A.target === c.current ? (A.shiftKey ? v(!k) : (b(), v(!0)), !0) : !1;
        },
        T
      ),
      f.registerCommand(
        Ce,
        (x) => x.target === c.current ? (x.preventDefault(), !0) : !1,
        T
      ),
      f.registerCommand(
        ve,
        p,
        T
      ),
      f.registerCommand(
        be,
        p,
        T
      ),
      f.registerCommand(he, C, T),
      f.registerCommand(ne, l, T)
    );
    return () => {
      N = !1, F();
    };
  }, [
    b,
    f,
    k,
    a,
    p,
    C,
    l,
    v
  ]);
  const L = k && O(w), y = k;
  return /* @__PURE__ */ R(De, { fallback: null, children: [
    /* @__PURE__ */ R(W, { children: [
      /* @__PURE__ */ R("div", { draggable: L, children: [
        y && /* @__PURE__ */ t(
          "button",
          {
            className: "image-edit-button",
            ref: S,
            onClick: () => {
              h("Update Inline Image", (N) => /* @__PURE__ */ t(
                Ge,
                {
                  activeEditor: f,
                  nodeKey: a,
                  onClose: N
                }
              ));
            },
            children: "Edit"
          }
        ),
        /* @__PURE__ */ t(
          Ye,
          {
            className: y ? `focused ${O(w) ? "draggable" : ""}` : null,
            src: e,
            altText: n,
            imageRef: c,
            width: i,
            height: u,
            position: g
          }
        )
      ] }),
      m && /* @__PURE__ */ t("div", { className: "image-caption-container", children: /* @__PURE__ */ R(Ue, { initialEditor: r, children: [
        /* @__PURE__ */ t(Me, {}),
        /* @__PURE__ */ t(ke, {}),
        /* @__PURE__ */ t(ze, {}),
        /* @__PURE__ */ t(Ze, {}),
        /* @__PURE__ */ t(
          Se,
          {
            contentEditable: /* @__PURE__ */ t(xe, { className: "InlineImageNode__contentEditable" }),
            placeholder: /* @__PURE__ */ t(we, { className: "InlineImageNode__placeholder", children: "Enter a caption..." }),
            ErrorBoundary: Le
          }
        )
      ] }) })
    ] }),
    d
  ] });
}
export {
  Ge as UpdateInlineImageDialog,
  nt as default
};
