import { u as $, g as H, t as K, L as y, j as t, a as A, U as B, p as q, s as J, Z as Q, b as ee, c as te, $ as ne, d as se, h as oe, e as le, P as ie, f as ae, T as re, S as ce, D as ue, B as de } from "./main-C21LZUoC.js";
import { useEffect as L, useState as C, useCallback as F, useRef as _, Suspense as me } from "react";
import { d as pe } from "./LexicalNestedComposer.prod-DxTOTjO8.js";
import { $getSelection as D, $isRangeSelection as U, SELECTION_CHANGE_COMMAND as Y, COMMAND_PRIORITY_CRITICAL as fe, KEY_ESCAPE_COMMAND as Z, COMMAND_PRIORITY_HIGH as ge, COMMAND_PRIORITY_LOW as w, $isTextNode as he, FORMAT_TEXT_COMMAND as M, $isNodeSelection as O, $getNodeByKey as V, $setSelection as z, KEY_ENTER_COMMAND as xe, KEY_BACKSPACE_COMMAND as Ce, KEY_DELETE_COMMAND as ve, DRAGSTART_COMMAND as Ee, CLICK_COMMAND as be } from "lexical";
import { createPortal as W } from "react-dom";
function Ne({ defaultSelection: e }) {
  const [n] = $();
  return L(() => {
    n.focus(() => {
      const i = document.activeElement, l = n.getRootElement();
      l === null || i !== null && l.contains(i) || l.focus({ preventScroll: !0 });
    }, { defaultSelection: e });
  }, [e, n]), null;
}
const je = 10, ke = 5;
function G(e, n, i, l = je, c = ke) {
  const u = i.parentElement;
  if (e === null || !u) {
    n.style.opacity = "0", n.style.transform = "translate(-10000px, -10000px)";
    return;
  }
  const a = n.getBoundingClientRect(), d = i.getBoundingClientRect(), p = u.getBoundingClientRect();
  let f = e.top - l, o = e.left - c;
  f < p.top && (f += a.height + e.height + l * 2), o + a.width > p.right && (o = p.right - a.width - c), f -= d.top, o -= d.left, n.style.opacity = "1", n.style.transform = `translate(${o}px, ${f}px)`;
}
function Se({
  editor: e,
  isLink: n,
  setIsLink: i,
  anchorElem: l
}) {
  const c = _(null), u = _(null), [a, d] = C(""), [p, f] = C(""), [o, k] = C(!1), [N, v] = C(null), h = F(() => {
    var R, T;
    const s = D();
    if (U(s)) {
      const g = H(s), S = g.getParent();
      A(S) ? d(S.getURL()) : A(g) ? d(g.getURL()) : d("");
    }
    const r = c.current, E = window.getSelection(), j = document.activeElement;
    if (r === null)
      return;
    const m = e.getRootElement();
    if (s !== null && E !== null && m !== null && m.contains(E.anchorNode) && e.isEditable()) {
      const g = (T = (R = E.focusNode) == null ? void 0 : R.parentElement) == null ? void 0 : T.getBoundingClientRect();
      g && (g.y += 40, G(g, r, l)), v(s);
    } else (!j || j.className !== "link-input") && (m !== null && G(null, r, l), v(null), k(!1), d(""));
    return !0;
  }, [l, e]);
  L(() => {
    const s = l.parentElement, r = () => {
      e.getEditorState().read(() => {
        h();
      });
    };
    return window.addEventListener("resize", r), s && s.addEventListener("scroll", r), () => {
      window.removeEventListener("resize", r), s && s.removeEventListener("scroll", r);
    };
  }, [l.parentElement, e, h]), L(() => y(
    e.registerUpdateListener(({ editorState: s }) => {
      s.read(() => {
        h();
      });
    }),
    e.registerCommand(
      Y,
      () => (h(), !0),
      w
    ),
    e.registerCommand(
      Z,
      () => n ? (i(!1), !0) : !1,
      ge
    )
  ), [e, h, i, n]), L(() => {
    e.getEditorState().read(() => {
      h();
    });
  }, [e, h]), L(() => {
    o && u.current && u.current.focus();
  }, [o]);
  const x = (s) => {
    s.key === "Enter" ? (s.preventDefault(), I()) : s.key === "Escape" && (s.preventDefault(), k(!1));
  }, I = () => {
    N !== null && (a !== "" && e.dispatchCommand(B, J(p)), k(!1));
  };
  return /* @__PURE__ */ t.jsx("div", { ref: c, className: "link-editor", children: n ? o ? /* @__PURE__ */ t.jsxs(t.Fragment, { children: [
    /* @__PURE__ */ t.jsx(
      "input",
      {
        ref: u,
        className: "link-input",
        value: p,
        onChange: (s) => {
          f(s.target.value);
        },
        onKeyDown: (s) => {
          x(s);
        }
      }
    ),
    /* @__PURE__ */ t.jsxs("div", { children: [
      /* @__PURE__ */ t.jsx(
        "div",
        {
          className: "link-cancel",
          role: "button",
          tabIndex: 0,
          onMouseDown: (s) => s.preventDefault(),
          onClick: () => {
            k(!1);
          }
        }
      ),
      /* @__PURE__ */ t.jsx(
        "div",
        {
          className: "link-confirm",
          role: "button",
          tabIndex: 0,
          onMouseDown: (s) => s.preventDefault(),
          onClick: I
        }
      )
    ] })
  ] }) : /* @__PURE__ */ t.jsxs("div", { className: "link-view", children: [
    /* @__PURE__ */ t.jsx("a", { href: a, target: "_blank", rel: "noopener noreferrer", children: a }),
    /* @__PURE__ */ t.jsx(
      "div",
      {
        className: "link-edit",
        role: "button",
        tabIndex: 0,
        onMouseDown: (s) => s.preventDefault(),
        onClick: () => {
          f(a), k(!0);
        }
      }
    ),
    /* @__PURE__ */ t.jsx(
      "div",
      {
        className: "link-trash",
        role: "button",
        tabIndex: 0,
        onMouseDown: (s) => s.preventDefault(),
        onClick: () => {
          e.dispatchCommand(B, null);
        }
      }
    )
  ] }) : null });
}
function Le(e, n) {
  const [i, l] = C(e), [c, u] = C(!1), a = F(() => {
    const d = D();
    if (U(d)) {
      const p = H(d), f = K(p, A), o = K(p, q);
      f != null && o == null ? u(!0) : u(!1);
    }
  }, []);
  return L(() => y(
    e.registerUpdateListener(({ editorState: d }) => {
      d.read(() => {
        a();
      });
    }),
    e.registerCommand(
      Y,
      (d, p) => (a(), l(p), !1),
      fe
    )
  ), [e, a]), W(
    /* @__PURE__ */ t.jsx(
      Se,
      {
        editor: i,
        isLink: c,
        anchorElem: n,
        setIsLink: u
      }
    ),
    n
  );
}
function Ie({
  anchorElem: e = document.body
}) {
  const [n] = $();
  return Le(n, e);
}
function Re(e, n) {
  const i = e.getRangeAt(0);
  let l;
  if (e.anchorNode === n) {
    let c = n;
    for (; c.firstElementChild != null; )
      c = c.firstElementChild;
    l = c.getBoundingClientRect();
  } else
    l = i.getBoundingClientRect();
  return l;
}
const we = 10, Te = 5;
function Fe(e, n, i, l = we, c = Te) {
  const u = i.parentElement;
  if (e === null || !u) {
    n.style.opacity = "0", n.style.transform = "translate(-10000px, -10000px)";
    return;
  }
  const a = n.getBoundingClientRect(), d = i.getBoundingClientRect(), p = u.getBoundingClientRect();
  let f = e.top - a.height - l, o = e.left - c;
  f < p.top && (f += a.height + e.height + l * 2), o + a.width > p.right && (o = p.right - a.width - c), f -= d.top, o -= d.left, n.style.opacity = "1", n.style.transform = `translate(${o}px, ${f}px)`;
}
function Me({
  editor: e,
  anchorElem: n,
  isLink: i,
  isBold: l,
  isItalic: c,
  isUnderline: u,
  isCode: a,
  isStrikethrough: d,
  isSubscript: p,
  isSuperscript: f
}) {
  const o = _(null), k = F(() => {
    i ? e.dispatchCommand(B, null) : e.dispatchCommand(B, "https://");
  }, [e, i]), N = 5;
  let v = { x: 0, y: 0 };
  function h(r) {
    v = { x: r.clientX, y: r.clientY };
  }
  function x(r) {
    o != null && o.current && (r.buttons === 1 || r.buttons === 3) && Math.sqrt(
      Math.pow(r.clientX - v.x, 2) + Math.pow(r.clientY - v.y, 2)
    ) > N && (o.current.style.pointerEvents = "none");
  }
  function I(r) {
    o != null && o.current && (o.current.style.pointerEvents = "auto");
  }
  L(() => {
    if (o != null && o.current)
      return document.addEventListener("mousedown", h), document.addEventListener("mousemove", x), document.addEventListener("mouseup", I), () => {
        document.removeEventListener("mousedown", h), document.removeEventListener("mousemove", x), document.removeEventListener("mouseup", I);
      };
  }, [o]);
  const s = F(() => {
    const r = D(), E = o.current, j = window.getSelection();
    if (E === null)
      return;
    const m = e.getRootElement();
    if (r !== null && j !== null && !j.isCollapsed && m !== null && m.contains(j.anchorNode)) {
      const R = Re(j, m);
      Fe(R, E, n);
    }
  }, [e, n]);
  return L(() => {
    const r = n.parentElement, E = () => {
      e.getEditorState().read(() => {
        s();
      });
    };
    return window.addEventListener("resize", E), r && r.addEventListener("scroll", E), () => {
      window.removeEventListener("resize", E), r && r.removeEventListener("scroll", E);
    };
  }, [e, s, n]), L(() => (e.getEditorState().read(() => {
    s();
  }), y(
    e.registerUpdateListener(({ editorState: r }) => {
      r.read(() => {
        s();
      });
    }),
    e.registerCommand(
      Y,
      () => (s(), !1),
      w
    )
  )), [e, s]), /* @__PURE__ */ t.jsx("div", { ref: o, className: "floating-text-format-popup", children: e.isEditable() && /* @__PURE__ */ t.jsxs(t.Fragment, { children: [
    /* @__PURE__ */ t.jsx(
      "button",
      {
        onClick: () => {
          e.dispatchCommand(M, "bold");
        },
        className: "popup-item spaced " + (l ? "active" : ""),
        "aria-label": "Format text as bold",
        children: /* @__PURE__ */ t.jsx("i", { className: "format bold" })
      }
    ),
    /* @__PURE__ */ t.jsx(
      "button",
      {
        onClick: () => {
          e.dispatchCommand(M, "italic");
        },
        className: "popup-item spaced " + (c ? "active" : ""),
        "aria-label": "Format text as italics",
        children: /* @__PURE__ */ t.jsx("i", { className: "format italic" })
      }
    ),
    /* @__PURE__ */ t.jsx(
      "button",
      {
        onClick: () => {
          e.dispatchCommand(M, "underline");
        },
        className: "popup-item spaced " + (u ? "active" : ""),
        "aria-label": "Format text to underlined",
        children: /* @__PURE__ */ t.jsx("i", { className: "format underline" })
      }
    ),
    /* @__PURE__ */ t.jsx(
      "button",
      {
        onClick: () => {
          e.dispatchCommand(M, "strikethrough");
        },
        className: "popup-item spaced " + (d ? "active" : ""),
        "aria-label": "Format text with a strikethrough",
        children: /* @__PURE__ */ t.jsx("i", { className: "format strikethrough" })
      }
    ),
    /* @__PURE__ */ t.jsx(
      "button",
      {
        onClick: () => {
          e.dispatchCommand(M, "subscript");
        },
        className: "popup-item spaced " + (p ? "active" : ""),
        title: "Subscript",
        "aria-label": "Format Subscript",
        children: /* @__PURE__ */ t.jsx("i", { className: "format subscript" })
      }
    ),
    /* @__PURE__ */ t.jsx(
      "button",
      {
        onClick: () => {
          e.dispatchCommand(M, "superscript");
        },
        className: "popup-item spaced " + (f ? "active" : ""),
        title: "Superscript",
        "aria-label": "Format Superscript",
        children: /* @__PURE__ */ t.jsx("i", { className: "format superscript" })
      }
    ),
    /* @__PURE__ */ t.jsx(
      "button",
      {
        onClick: () => {
          e.dispatchCommand(M, "code");
        },
        className: "popup-item spaced " + (a ? "active" : ""),
        "aria-label": "Insert code block",
        children: /* @__PURE__ */ t.jsx("i", { className: "format code" })
      }
    ),
    /* @__PURE__ */ t.jsx(
      "button",
      {
        onClick: k,
        className: "popup-item spaced " + (i ? "active" : ""),
        "aria-label": "Insert link",
        children: /* @__PURE__ */ t.jsx("i", { className: "format link" })
      }
    )
  ] }) });
}
function De(e, n) {
  const [i, l] = C(!1), [c, u] = C(!1), [a, d] = C(!1), [p, f] = C(!1), [o, k] = C(!1), [N, v] = C(!1), [h, x] = C(!1), [I, s] = C(!1), [r, E] = C(!1), j = F(() => {
    e.getEditorState().read(() => {
      if (e.isComposing())
        return;
      const m = D(), R = window.getSelection(), T = e.getRootElement();
      if (R !== null && (!U(m) || T === null || !T.contains(R.anchorNode))) {
        l(!1);
        return;
      }
      if (!U(m))
        return;
      const g = H(m);
      d(m.hasFormat("bold")), f(m.hasFormat("italic")), k(m.hasFormat("underline")), v(m.hasFormat("strikethrough")), x(m.hasFormat("subscript")), s(m.hasFormat("superscript")), E(m.hasFormat("code"));
      const S = g.getParent();
      A(S) || A(g) ? u(!0) : u(!1), !Q(m.anchor.getNode()) && m.getTextContent() !== "" ? l(he(g)) : l(!1);
      const b = m.getTextContent().replace(/\n/g, "");
      if (!m.isCollapsed() && b === "") {
        l(!1);
        return;
      }
    });
  }, [e]);
  return L(() => (document.addEventListener("selectionchange", j), () => {
    document.removeEventListener("selectionchange", j);
  }), [j]), L(() => y(
    e.registerUpdateListener(() => {
      j();
    }),
    e.registerRootListener(() => {
      e.getRootElement() === null && l(!1);
    })
  ), [e, j]), !i || c ? null : W(
    /* @__PURE__ */ t.jsx(
      Me,
      {
        editor: e,
        anchorElem: n,
        isLink: c,
        isBold: a,
        isItalic: p,
        isStrikethrough: N,
        isSubscript: h,
        isSuperscript: I,
        isUnderline: o,
        isCode: r
      }
    ),
    n
  );
}
function _e({
  anchorElem: e = document.body
}) {
  const [n] = $();
  return De(n, e);
}
const X = /* @__PURE__ */ new Set();
function Ae(e) {
  if (!X.has(e))
    throw new Promise((n) => {
      const i = new Image();
      i.src = e, i.onload = () => {
        X.add(e), n(null);
      };
    });
}
function ye({
  altText: e,
  className: n,
  imageRef: i,
  src: l,
  width: c,
  height: u,
  position: a
}) {
  return Ae(l), /* @__PURE__ */ t.jsx(
    "img",
    {
      className: n || void 0,
      src: l,
      alt: e,
      ref: i,
      "data-position": a,
      style: {
        height: u,
        width: c,
        display: "block"
      },
      draggable: "false"
    }
  );
}
function Pe({ activeEditor: e, nodeKey: n, onClose: i }) {
  const c = e.getEditorState().read(() => V(n)), [u, a] = C(c.getAltText()), [d, p] = C(c.getShowCaption()), [f, o] = C(c.getPosition()), k = (h) => {
    p(h.target.checked);
  }, N = (h) => {
    o(h.target.value);
  }, v = () => {
    const h = { altText: u, showCaption: d, position: f };
    c && e.update(() => {
      c.update(h);
    }), i();
  };
  return /* @__PURE__ */ t.jsxs(t.Fragment, { children: [
    /* @__PURE__ */ t.jsx("div", { style: { marginBottom: "1em" }, children: /* @__PURE__ */ t.jsx(
      re,
      {
        label: "Alt Text",
        placeholder: "Descriptive alternative text",
        onChange: a,
        value: u,
        "data-test-id": "image-modal-alt-text-input"
      }
    ) }),
    /* @__PURE__ */ t.jsxs(
      ce,
      {
        style: { marginBottom: "1em", width: "208px" },
        value: f,
        label: "Position",
        name: "position",
        id: "position-select",
        onChange: N,
        children: [
          /* @__PURE__ */ t.jsx("option", { value: "left", children: "Left" }),
          /* @__PURE__ */ t.jsx("option", { value: "right", children: "Right" }),
          /* @__PURE__ */ t.jsx("option", { value: "full", children: "Full Width" })
        ]
      }
    ),
    /* @__PURE__ */ t.jsxs("div", { className: "Input__wrapper", children: [
      /* @__PURE__ */ t.jsx(
        "input",
        {
          id: "caption",
          type: "checkbox",
          checked: d,
          onChange: k
        }
      ),
      /* @__PURE__ */ t.jsx("label", { htmlFor: "caption", children: "Show Caption" })
    ] }),
    /* @__PURE__ */ t.jsx(ue, { children: /* @__PURE__ */ t.jsx(
      de,
      {
        "data-test-id": "image-modal-file-upload-btn",
        onClick: () => v(),
        children: "Confirm"
      }
    ) })
  ] });
}
function He({
  src: e,
  altText: n,
  nodeKey: i,
  width: l,
  height: c,
  showCaption: u,
  caption: a,
  position: d
}) {
  const [p, f] = ee(), o = _(null), k = _(null), [N, v, h] = te(i), [x] = $(), [I, s] = C(null), r = _(null), E = F(
    (g) => {
      if (N && O(D())) {
        g.preventDefault();
        const b = V(i);
        ne(b) && (b == null || b.remove()), v(!1);
      }
      return !1;
    },
    [N, i, v]
  ), j = F(
    (g) => {
      const S = D(), b = k.current;
      if (N && O(S) && S.getNodes().length === 1) {
        if (u)
          return z(null), g.preventDefault(), a.focus(), !0;
        if (b !== null && b !== document.activeElement)
          return g.preventDefault(), b.focus(), !0;
      }
      return !1;
    },
    [a, N, u]
  ), m = F(
    (g) => r.current === a || k.current === g.target ? (z(null), x.update(() => {
      v(!0);
      const S = x.getRootElement();
      S !== null && S.focus();
    }), !0) : !1,
    [a, x, v]
  );
  L(() => {
    let g = !0;
    const S = y(
      x.registerUpdateListener(({ editorState: b }) => {
        g && s(b.read(() => D()));
      }),
      x.registerCommand(
        Y,
        (b, P) => (r.current = P, !1),
        w
      ),
      x.registerCommand(
        be,
        (b) => {
          const P = b;
          return P.target === o.current ? (P.shiftKey ? v(!N) : (h(), v(!0)), !0) : !1;
        },
        w
      ),
      x.registerCommand(
        Ee,
        (b) => b.target === o.current ? (b.preventDefault(), !0) : !1,
        w
      ),
      x.registerCommand(
        ve,
        E,
        w
      ),
      x.registerCommand(
        Ce,
        E,
        w
      ),
      x.registerCommand(xe, j, w),
      x.registerCommand(Z, m, w)
    );
    return () => {
      g = !1, S();
    };
  }, [
    h,
    x,
    N,
    i,
    E,
    j,
    m,
    v
  ]);
  const R = N && O(I), T = N;
  return /* @__PURE__ */ t.jsxs(me, { fallback: null, children: [
    /* @__PURE__ */ t.jsxs(t.Fragment, { children: [
      /* @__PURE__ */ t.jsxs("div", { draggable: R, children: [
        T && /* @__PURE__ */ t.jsx(
          "button",
          {
            className: "image-edit-button",
            ref: k,
            onClick: () => {
              f("Update Inline Image", (g) => /* @__PURE__ */ t.jsx(
                Pe,
                {
                  activeEditor: x,
                  nodeKey: i,
                  onClose: g
                }
              ));
            },
            children: "Edit"
          }
        ),
        /* @__PURE__ */ t.jsx(
          ye,
          {
            className: T ? `focused ${O(I) ? "draggable" : ""}` : null,
            src: e,
            altText: n,
            imageRef: o,
            width: l,
            height: c,
            position: d
          }
        )
      ] }),
      u && /* @__PURE__ */ t.jsx("div", { className: "image-caption-container", children: /* @__PURE__ */ t.jsxs(pe, { initialEditor: a, children: [
        /* @__PURE__ */ t.jsx(Ne, {}),
        /* @__PURE__ */ t.jsx(se, {}),
        /* @__PURE__ */ t.jsx(Ie, {}),
        /* @__PURE__ */ t.jsx(_e, {}),
        /* @__PURE__ */ t.jsx(
          oe,
          {
            contentEditable: /* @__PURE__ */ t.jsx(le, { className: "InlineImageNode__contentEditable" }),
            placeholder: /* @__PURE__ */ t.jsx(ie, { className: "InlineImageNode__placeholder", children: "Enter a caption..." }),
            ErrorBoundary: ae
          }
        )
      ] }) })
    ] }),
    p
  ] });
}
export {
  Pe as UpdateInlineImageDialog,
  He as default
};
