var x = Object.defineProperty;
var p = (i, t, r) => t in i ? x(i, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : i[t] = r;
var s = (i, t, r) => p(i, typeof t != "symbol" ? t + "" : t, r);
import { D as u, Q as d, d as m } from "./index-U54PFR25.js";
const h = {
  capture: !0
}, E = /* @__PURE__ */ new Set(["P", "DIV", "LI", "BLOCKQUOTE", "PRE", "TR", "TD", "TH", "SECTION", "ARTICLE", "H1", "H2", "H3", "H4", "H5", "H6"]), C = /<(?:br|p|div|li|blockquote|pre|tr|td|th|section|article|h[1-6])\b|[\r\n\t]/i, T = /[\r\n\t]+/g, f = /\s{2,}/g;
function b(i) {
  return typeof i.insert == "string" ? i.insert.length : 1;
}
class L {
  constructor(t, r = {}) {
    s(this, "clipboard");
    s(this, "quill");
    s(this, "cleanupFunctions", []);
    this.clipboard = t.getModule("clipboard"), this.quill = t, r.isTitleMode && this.setupTitleModePaste();
  }
  setupTitleModePaste() {
    const t = this.quill.root, r = (e) => {
      if (e.preventDefault(), !e.clipboardData) return;
      const a = e.clipboardData.getData("text/html"), l = e.clipboardData.getData("text/plain"), n = this.quill.getSelection(!0);
      if (!n) return;
      if (a) {
        const y = this.normalizeTitlePasteHtml(a), o = this.clipboard.convert({
          html: y
        });
        this.applyPasteDelta(n.index, n.length, o), this.scheduleSelection(n.index + this.getDeltaLength(o));
        return;
      }
      const c = this.normalizeTitlePasteText(l);
      this.applyPasteText(n.index, n.length, c), this.scheduleSelection(n.index + c.length);
    };
    t.addEventListener("paste", r, h), this.cleanupFunctions.push(() => {
      t.removeEventListener("paste", r, h);
    });
  }
  normalizeTitlePasteText(t) {
    return t.replaceAll(T, " ").replaceAll(f, " ").trim();
  }
  normalizeTitlePasteHtml(t) {
    var n, c;
    if (!C.test(t)) return t;
    const r = document.createElement("div");
    r.innerHTML = t, this.flattenTitlePasteNode(r);
    const e = document.createTreeWalker(r, NodeFilter.SHOW_TEXT);
    let a = e.nextNode();
    for (; a; )
      a.textContent = ((n = a.textContent) == null ? void 0 : n.replaceAll(f, " ")) ?? "", a = e.nextNode();
    const l = this.findLastTextNode(r);
    return l && (l.textContent = ((c = l.textContent) == null ? void 0 : c.trimEnd()) ?? ""), r.innerHTML;
  }
  flattenTitlePasteNode(t) {
    var e;
    const r = Array.from(t.childNodes);
    for (const a of r) {
      if (a.nodeType === Node.TEXT_NODE) {
        a.textContent = ((e = a.textContent) == null ? void 0 : e.replaceAll(T, " ")) ?? "";
        continue;
      }
      if (a instanceof HTMLElement) {
        if (a.tagName === "BR") {
          a.replaceWith(document.createTextNode(" "));
          continue;
        }
        this.flattenTitlePasteNode(a), E.has(a.tagName) && a.nextSibling && !this.endsWithWhitespace(a) && a.appendChild(document.createTextNode(" "));
      }
    }
  }
  endsWithWhitespace(t) {
    var r;
    return /\s$/.test(((r = this.findLastTextNode(t)) == null ? void 0 : r.textContent) ?? "");
  }
  findLastTextNode(t) {
    const r = Array.from(t.childNodes).reverse();
    for (const e of r) {
      if (e.nodeType === Node.TEXT_NODE) return e;
      const a = this.findLastTextNode(e);
      if (a) return a;
    }
    return null;
  }
  getDeltaLength(t) {
    var r;
    return ((r = t.ops) == null ? void 0 : r.reduce((e, a) => e + b(a), 0)) ?? 0;
  }
  applyPasteDelta(t, r, e) {
    const a = new u().retain(t).delete(r).concat(e);
    this.quill.updateContents(a, d.sources.USER);
  }
  applyPasteText(t, r, e) {
    const a = new u().retain(t).delete(r);
    e && a.insert(e), this.quill.updateContents(a, d.sources.USER);
  }
  scheduleSelection(t) {
    m(() => this.quill.setSelection(t, 0, d.sources.SILENT));
  }
  destroy() {
    this.cleanupFunctions.forEach((t) => t()), this.cleanupFunctions = [];
  }
  async copyContent(t) {
    var r;
    try {
      const e = this.quill.getText(), a = this.quill.root.innerHTML;
      return this.copyToClipboard(e, a, t);
    } catch (e) {
      return console.error("Error in copyContent:", e), (r = t == null ? void 0 : t.onError) == null || r.call(t), !1;
    }
  }
  async copyToClipboard(t, r, e) {
    var a, l;
    try {
      return await this.tryModernClipboardWithHtml(t, r, e) || await this.tryModernClipboardText(t, e) || r && this.tryLegacyClipboardWithHtml(r, e) || this.tryLegacyClipboardText(t, e) ? !0 : ((a = e == null ? void 0 : e.onError) == null || a.call(e), !1);
    } catch (n) {
      return console.error("복사 중 오류 발생:", n), (l = e == null ? void 0 : e.onError) == null || l.call(e), !1;
    }
  }
  async tryModernClipboardWithHtml(t, r, e) {
    var a;
    if (!navigator.clipboard || !r || !globalThis.ClipboardItem || !globalThis.isSecureContext) return !1;
    try {
      return await navigator.clipboard.write([new ClipboardItem({
        "text/html": new Blob([r], {
          type: "text/html"
        }),
        "text/plain": new Blob([t], {
          type: "text/plain"
        })
      })]), (a = e == null ? void 0 : e.onSuccess) == null || a.call(e), !0;
    } catch {
      return !1;
    }
  }
  async tryModernClipboardText(t, r) {
    var e;
    if (!navigator.clipboard || !globalThis.isSecureContext) return !1;
    try {
      return await navigator.clipboard.writeText(t), (e = r == null ? void 0 : r.onSuccess) == null || e.call(r), !0;
    } catch {
      return !1;
    }
  }
  tryLegacyClipboardWithHtml(t, r) {
    const e = document.createElement("div");
    e.innerHTML = t, e.style.cssText = "position:fixed;left:-9999px;top:-9999px;opacity:0;", document.body.appendChild(e);
    try {
      const a = document.createRange();
      a.selectNodeContents(e);
      const l = globalThis.getSelection();
      return l ? (l.removeAllRanges(), l.addRange(a), this.tryClipboardEventCopy({
        html: t,
        text: e.textContent ?? ""
      }, r)) : !1;
    } finally {
      e.remove();
    }
  }
  tryLegacyClipboardText(t, r) {
    const e = document.createElement("textarea");
    e.value = t, e.style.cssText = "position:fixed;left:-9999px;top:-9999px;opacity:0;", document.body.appendChild(e);
    try {
      return e.focus(), e.select(), this.tryClipboardEventCopy({
        text: t
      }, r);
    } finally {
      e.remove();
    }
  }
  tryClipboardEventCopy(t, r) {
    var l;
    let e = !1;
    const a = (n) => {
      n.preventDefault(), n.clipboardData && (n.clipboardData.setData("text/plain", t.text), t.html && n.clipboardData.setData("text/html", t.html), e = !0);
    };
    document.addEventListener("copy", a);
    try {
      const n = new ClipboardEvent("copy", {
        clipboardData: new DataTransfer()
      });
      if (document.dispatchEvent(n), e)
        return (l = r == null ? void 0 : r.onSuccess) == null || l.call(r), !0;
    } catch {
      return !1;
    } finally {
      document.removeEventListener("copy", a);
    }
    return !1;
  }
}
export {
  L as default
};
