import { jsx as g, jsxs as P, Fragment as U } from "react/jsx-runtime";
import { useState as j, useEffect as b, useMemo as q, Suspense as F, useLayoutEffect as I, useRef as M } from "react";
import { g as N, W as V, O as a, y as c, a4 as u, a5 as G, a6 as Q, a7 as A, a8 as J, a9 as Z, aa as ee, ab as te, T as ne, ac as W, q as oe, ad as x, p as re, ae as se, af as O, ag as z, ah as ie, ai as ae, aj as ce, ak as le, al as ue, am as de, an as me, ao as fe, u as _, ap as ge, aq as pe, ar as ye, as as Ce, at as he, l as ve, t as Ee, G as ke, H as we, M as De, P as be, Q as Le, S as R, au as $ } from "./index-BtS8RM-z.js";
import { d as Se } from "./LexicalNestedComposer.prod-nuvzNal3.js";
import { flushSync as xe, createPortal as Re } from "react-dom";
import { c as $e } from "./collaboration-DO8v_8W0.js";
const E = typeof window < "u" && window.document !== void 0 && window.document.createElement !== void 0, Pe = E && "documentMode" in document ? document.documentMode : null, Te = !(!E || !("InputEvent" in window) || Pe) && "getTargetRanges" in new window.InputEvent("input"), Ne = E && /Version\/[\d.]+.*Safari/.test(navigator.userAgent), _e = E && /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream, Be = E && /^(?=.*Chrome).*/i.test(navigator.userAgent), Me = E && /AppleWebKit\/[\d.]+/.test(navigator.userAgent) && !Be;
function X(n, t) {
  t.update(() => {
    if (n !== null) {
      const e = me(n, KeyboardEvent) ? null : n.clipboardData, s = a();
      if (s !== null && e != null) {
        n.preventDefault();
        const i = fe(t);
        i !== null && e.setData("text/html", i), e.setData("text/plain", s.getTextContent());
      }
    }
  });
}
function Ae(n) {
  return N(n.registerCommand(x, (t) => {
    const e = a();
    return !!c(e) && (e.deleteCharacter(t), !0);
  }, u), n.registerCommand(de, (t) => {
    const e = a();
    return !!c(e) && (e.deleteWord(t), !0);
  }, u), n.registerCommand(ue, (t) => {
    const e = a();
    return !!c(e) && (e.deleteLine(t), !0);
  }, u), n.registerCommand(le, (t) => {
    const e = a();
    if (!c(e)) return !1;
    if (typeof t == "string") e.insertText(t);
    else {
      const s = t.dataTransfer;
      if (s != null) A(s, e);
      else {
        const i = t.data;
        i && e.insertText(i);
      }
    }
    return !0;
  }, u), n.registerCommand(ce, () => {
    const t = a();
    return !!c(t) && (t.removeText(), !0);
  }, u), n.registerCommand(W, (t) => {
    const e = a();
    return !!c(e) && (e.insertLineBreak(t), !0);
  }, u), n.registerCommand(ae, () => {
    const t = a();
    return !!c(t) && (t.insertLineBreak(), !0);
  }, u), n.registerCommand(ie, (t) => {
    const e = a();
    if (!c(e)) return !1;
    const s = t, i = s.shiftKey;
    return !!O(e, !0) && (s.preventDefault(), z(e, i, !0), !0);
  }, u), n.registerCommand(se, (t) => {
    const e = a();
    if (!c(e)) return !1;
    const s = t, i = s.shiftKey;
    return !!O(e, !1) && (s.preventDefault(), z(e, i, !1), !0);
  }, u), n.registerCommand(re, (t) => {
    const e = a();
    return !!c(e) && (t.preventDefault(), n.dispatchCommand(x, !0));
  }, u), n.registerCommand(oe, (t) => {
    const e = a();
    return !!c(e) && (t.preventDefault(), n.dispatchCommand(x, !1));
  }, u), n.registerCommand(ne, (t) => {
    const e = a();
    if (!c(e)) return !1;
    if (t !== null) {
      if ((_e || Ne || Me) && Te) return !1;
      t.preventDefault();
    }
    return n.dispatchCommand(W, !1);
  }, u), n.registerCommand(ee, () => (te(), !0), u), n.registerCommand(Z, (t) => {
    const e = a();
    return !!c(e) && (X(t, n), !0);
  }, u), n.registerCommand(J, (t) => {
    const e = a();
    return !!c(e) && (function(s, i) {
      X(s, i), i.update(() => {
        const r = a();
        c(r) && r.removeText();
      });
    }(t, n), !0);
  }, u), n.registerCommand(Q, (t) => {
    const e = a();
    return !!c(e) && (function(s, i) {
      s.preventDefault(), i.update(() => {
        const r = a(), { clipboardData: d } = s;
        d != null && c(r) && A(d, r);
      }, { tag: "paste" });
    }(t, n), !0);
  }, u), n.registerCommand(G, (t) => {
    const e = a();
    return !!c(e) && (t.preventDefault(), !0);
  }, u), n.registerCommand(V, (t) => {
    const e = a();
    return !!c(e) && (t.preventDefault(), !0);
  }, u));
}
const T = typeof window < "u" && window.document !== void 0 && window.document.createElement !== void 0 ? I : b;
function Y(n) {
  return n.getEditorState().read(ye(n.isComposing()));
}
function We({ contentEditable: n, placeholder: t = null, ErrorBoundary: e }) {
  const [s] = _(), i = function(r, d) {
    const [f, v] = j(() => r.getDecorators());
    return T(() => r.registerDecoratorListener((h) => {
      xe(() => {
        v(h);
      });
    }), [r]), b(() => {
      v(r.getDecorators());
    }, [r]), q(() => {
      const h = [], k = Object.keys(f);
      for (let w = 0; w < k.length; w++) {
        const D = k[w], S = g(d, { onError: (l) => r._onError(l), children: g(F, { fallback: null, children: f[D] }) }), o = r.getElementByKey(D);
        o !== null && h.push(Re(S, o, D));
      }
      return h;
    }, [d, f, r]);
  }(s, e);
  return function(r) {
    T(() => N(Ae(r), ge(r)), [r]);
  }(s), P(U, { children: [n, g(Oe, { content: t }), i] });
}
function Oe({ content: n }) {
  const [t] = _(), e = function(i) {
    const [r, d] = j(() => Y(i));
    return T(() => {
      function f() {
        const v = Y(i);
        d(v);
      }
      return f(), N(i.registerUpdateListener(() => {
        f();
      }), i.registerEditableListener(() => {
        f();
      }));
    }, [i]), r;
  }(t), s = pe();
  return e ? typeof n == "function" ? n(s) : n : null;
}
const ze = Ce ? I : b, Xe = ze, Ye = {
  ...he,
  paragraph: "StickyEditorTheme__paragraph"
}, je = Ye;
function L(n, t) {
  const e = n.style, s = t.rootElementRect, i = s !== null ? s.left : 0, r = s !== null ? s.top : 0;
  e.top = r + t.y + "px", e.left = i + t.x + "px";
}
function Ve({
  x: n,
  y: t,
  nodeKey: e,
  color: s,
  caption: i
}) {
  const [r] = _(), d = M(null), f = M({
    isDragging: !1,
    offsetX: 0,
    offsetY: 0,
    rootElementRect: null,
    x: 0,
    y: 0
  }), { isCollabActive: v } = ve();
  b(() => {
    const o = f.current;
    o.x = n, o.y = t;
    const l = d.current;
    l !== null && L(l, o);
  }, [n, t]), Xe(() => {
    const o = f.current, l = new ResizeObserver((C) => {
      for (let p = 0; p < C.length; p++) {
        const K = C[p], { target: H } = K;
        o.rootElementRect = H.getBoundingClientRect();
        const B = d.current;
        B !== null && L(B, o);
      }
    }), m = r.registerRootListener((C, p) => {
      p !== null && l.unobserve(p), C !== null && l.observe(C);
    }), y = () => {
      const C = r.getRootElement(), p = d.current;
      C !== null && p !== null && (o.rootElementRect = C.getBoundingClientRect(), L(p, o));
    };
    return window.addEventListener("resize", y), () => {
      window.removeEventListener("resize", y), m();
    };
  }, [r]), b(() => {
    const o = d.current;
    o !== null && setTimeout(() => {
      o.style.setProperty("transition", "top 0.3s ease 0s, left 0.3s ease 0s");
    }, 500);
  }, []);
  const h = (o) => {
    const l = d.current, m = f.current, y = m.rootElementRect;
    l !== null && m.isDragging && y !== null && (m.x = o.pageX - m.offsetX - y.left, m.y = o.pageY - m.offsetY - y.top, L(l, m));
  }, k = () => {
    const o = d.current, l = f.current;
    o !== null && (l.isDragging = !1, o.classList.remove("dragging"), r.update(() => {
      const m = R(e);
      $(m) && m.setPosition(l.x, l.y);
    })), document.removeEventListener("pointermove", h), document.removeEventListener("pointerup", k);
  }, w = () => {
    r.update(() => {
      const o = R(e);
      $(o) && o.remove();
    });
  }, D = () => {
    r.update(() => {
      const o = R(e);
      $(o) && o.toggleColor();
    });
  }, { historyState: S } = Ee();
  return /* @__PURE__ */ g("div", { ref: d, className: "sticky-note-container", children: /* @__PURE__ */ P(
    "div",
    {
      className: `sticky-note ${s}`,
      onPointerDown: (o) => {
        const l = d.current;
        if (l == null || o.button === 2 || o.target !== l.firstChild)
          return;
        const m = l, y = f.current;
        if (m !== null) {
          const { top: C, left: p } = m.getBoundingClientRect();
          y.offsetX = o.clientX - p, y.offsetY = o.clientY - C, y.isDragging = !0, m.classList.add("dragging"), document.addEventListener("pointermove", h), document.addEventListener("pointerup", k), o.preventDefault();
        }
      },
      children: [
        /* @__PURE__ */ g(
          "button",
          {
            onClick: w,
            className: "delete",
            "aria-label": "Delete sticky note",
            title: "Delete",
            children: "X"
          }
        ),
        /* @__PURE__ */ g(
          "button",
          {
            onClick: D,
            className: "color",
            "aria-label": "Change sticky note color",
            title: "Color",
            children: /* @__PURE__ */ g("i", { className: "bucket" })
          }
        ),
        /* @__PURE__ */ P(Se, { initialEditor: i, initialTheme: je, children: [
          v ? /* @__PURE__ */ g(
            ke,
            {
              id: i.getKey(),
              providerFactory: $e,
              shouldBootstrap: !0
            }
          ) : /* @__PURE__ */ g(we, { externalHistoryState: S }),
          /* @__PURE__ */ g(
            We,
            {
              contentEditable: /* @__PURE__ */ g(De, { className: "StickyNode__contentEditable" }),
              placeholder: /* @__PURE__ */ g(be, { className: "StickyNode__placeholder", children: "What's up?" }),
              ErrorBoundary: Le
            }
          )
        ] })
      ]
    }
  ) });
}
export {
  Ve as default
};
