import { jsx as g, jsxs as P, Fragment as q } from "react/jsx-runtime";
import { useState as j, useEffect as b, useMemo as F, Suspense as H, useLayoutEffect as I, useRef as M } from "react";
import { g as N, Z as V, O as a, y as c, a5 as u, a6 as J, a7 as Q, a8 as A, a9 as Z, aa as G, ab as ee, ac as te, T as ne, ad as O, q as oe, ae as R, p as re, af as se, ag as W, ah as z, ai as ie, aj as ae, ak as ce, al as le, am as ue, an as de, ao as me, ap as fe, u as _, aq as ge, ar as pe, as as Ce, at as he, au as ye, l as ve, t as Ee, J as ke, M as we, Q as De, R as be, U as Le, S as x, av as $ } from "./DSD.DRGuMxXd.js";
import { d as Se } from "./DSD.D2TwdbM_.js";
import { flushSync as Re, createPortal as xe } from "react-dom";
import { c as $e } from "./DSD.6z6Plwgb.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(R, (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(O, (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 !!W(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 !!W(e, !1) && (s.preventDefault(), z(e, i, !1), !0);
  }, u), n.registerCommand(re, (t) => {
    const e = a();
    return !!c(e) && (t.preventDefault(), n.dispatchCommand(R, !0));
  }, u), n.registerCommand(oe, (t) => {
    const e = a();
    return !!c(e) && (t.preventDefault(), n.dispatchCommand(R, !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(O, !1);
  }, u), n.registerCommand(ee, () => (te(), !0), u), n.registerCommand(G, (t) => {
    const e = a();
    return !!c(e) && (X(t, n), !0);
  }, u), n.registerCommand(Z, (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(J, (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(Ce(n.isComposing()));
}
function Oe({ contentEditable: n, placeholder: t = null, ErrorBoundary: e }) {
  const [s] = _(), i = function(r, d) {
    const [f, v] = j(() => r.getDecorators());
    return T(() => r.registerDecoratorListener((y) => {
      Re(() => {
        v(y);
      });
    }), [r]), b(() => {
      v(r.getDecorators());
    }, [r]), F(() => {
      const y = [], 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(H, { fallback: null, children: f[D] }) }), o = r.getElementByKey(D);
        o !== null && y.push(xe(S, o, D));
      }
      return y;
    }, [d, f, r]);
  }(s, e);
  return function(r) {
    T(() => N(Ae(r), ge(r)), [r]);
  }(s), P(q, { children: [n, g(We, { content: t }), i] });
}
function We({ 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 = he ? I : b, Xe = {
  ...ye,
  paragraph: "StickyEditorTheme__paragraph"
};
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 Fe({
  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]), ze(() => {
    const o = f.current, l = new ResizeObserver((h) => {
      for (let p = 0; p < h.length; p++) {
        const K = h[p], { target: U } = K;
        o.rootElementRect = U.getBoundingClientRect();
        const B = d.current;
        B !== null && L(B, o);
      }
    }), m = r.registerRootListener((h, p) => {
      p !== null && l.unobserve(p), h !== null && l.observe(h);
    }), C = () => {
      const h = r.getRootElement(), p = d.current;
      h !== null && p !== null && (o.rootElementRect = h.getBoundingClientRect(), L(p, 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 y = (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));
  }, k = () => {
    const o = d.current, l = f.current;
    o !== null && (l.isDragging = !1, o.classList.remove("dragging"), r.update(() => {
      const m = x(e);
      $(m) && m.setPosition(l.x, l.y);
    })), document.removeEventListener("pointermove", y), document.removeEventListener("pointerup", k);
  }, w = () => {
    r.update(() => {
      const o = x(e);
      $(o) && o.remove();
    });
  }, D = () => {
    r.update(() => {
      const o = x(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, C = f.current;
        if (m !== null) {
          const { top: h, left: p } = m.getBoundingClientRect();
          C.offsetX = o.clientX - p, C.offsetY = o.clientY - h, C.isDragging = !0, m.classList.add("dragging"), document.addEventListener("pointermove", y), 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: Xe, children: [
          v ? /* @__PURE__ */ g(
            ke,
            {
              id: i.getKey(),
              providerFactory: $e,
              shouldBootstrap: !0
            }
          ) : /* @__PURE__ */ g(we, { externalHistoryState: S }),
          /* @__PURE__ */ g(
            Oe,
            {
              contentEditable: /* @__PURE__ */ g(De, { className: "StickyNode__contentEditable" }),
              placeholder: /* @__PURE__ */ g(be, { className: "StickyNode__placeholder", children: "What's up?" }),
              ErrorBoundary: Le
            }
          )
        ] })
      ]
    }
  ) });
}
export {
  Fe as default
};
