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