var x = Object.defineProperty;
var k = (r, t, n) => t in r ? x(r, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : r[t] = n;
var N = (r, t, n) => (k(r, typeof t != "symbol" ? t + "" : t, n), n);
import { Attributor as v, ClassAttributor as A, StyleAttributor as M, Scope as w, EmbedBlot as L, BlockBlot as C } from "../../../../parchment@3.0.0/node_modules/parchment/dist/parchment.mjs";
import c from "../../../../quill-delta@5.1.0/node_modules/quill-delta/dist/Delta.mjs";
import { BlockEmbed as O } from "../blots/block.mjs";
import B from "../core/logger.mjs";
import I from "../core/module.mjs";
import p from "../core/quill.mjs";
import { AlignAttribute as P, AlignStyle as _ } from "../formats/align.mjs";
import { BackgroundStyle as R } from "../formats/background.mjs";
import d from "../formats/code.mjs";
import { ColorStyle as U } from "../formats/color.mjs";
import { DirectionAttribute as F, DirectionStyle as H } from "../formats/direction.mjs";
import { FontStyle as z } from "../formats/font.mjs";
import { SizeStyle as W } from "../formats/size.mjs";
import { deleteRange as $ } from "./keyboard.mjs";
import X from "./normalizeExternalHTML/index.mjs";
const j = B("quill:clipboard"), G = [[Node.TEXT_NODE, lt], [Node.TEXT_NODE, S], ["br", Z], [Node.ELEMENT_NODE, S], [Node.ELEMENT_NODE, K], [Node.ELEMENT_NODE, J], [Node.ELEMENT_NODE, it], ["li", rt], ["ol, ul", nt], ["pre", tt], ["tr", st], ["b", y("bold")], ["i", y("italic")], ["strike", y("strike")], ["style", et]], Q = [P, F].reduce((r, t) => (r[t.keyName] = t, r), {}), T = [_, R, U, H, z, W].reduce((r, t) => (r[t.keyName] = t, r), {});
class V extends I {
  constructor(t, n) {
    super(t, n), this.quill.root.addEventListener("copy", (e) => this.onCaptureCopy(e, !1)), this.quill.root.addEventListener("cut", (e) => this.onCaptureCopy(e, !0)), this.quill.root.addEventListener("paste", this.onCapturePaste.bind(this)), this.matchers = [], G.concat(this.options.matchers ?? []).forEach((e) => {
      let [i, s] = e;
      this.addMatcher(i, s);
    });
  }
  addMatcher(t, n) {
    this.matchers.push([t, n]);
  }
  convert(t) {
    let {
      html: n,
      text: e
    } = t, i = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
    if (i[d.blotName])
      return new c().insert(e || "", {
        [d.blotName]: i[d.blotName]
      });
    if (!n)
      return new c().insert(e || "", i);
    const s = this.convertHTML(n);
    return g(s, `
`) && (s.ops[s.ops.length - 1].attributes == null || i.table) ? s.compose(new c().retain(s.length() - 1).delete(1)) : s;
  }
  normalizeHTML(t) {
    X(t);
  }
  convertHTML(t) {
    const n = new DOMParser().parseFromString(t, "text/html");
    this.normalizeHTML(n);
    const e = n.body, i = /* @__PURE__ */ new WeakMap(), [s, a] = this.prepareMatching(e, i);
    return q(this.quill.scroll, e, s, a, i);
  }
  dangerouslyPasteHTML(t, n) {
    let e = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : p.sources.API;
    if (typeof t == "string") {
      const i = this.convert({
        html: t,
        text: ""
      });
      this.quill.setContents(i, n), this.quill.setSelection(0, p.sources.SILENT);
    } else {
      const i = this.convert({
        html: n,
        text: ""
      });
      this.quill.updateContents(new c().retain(t).concat(i), e), this.quill.setSelection(t + i.length(), p.sources.SILENT);
    }
  }
  onCaptureCopy(t) {
    var a, l;
    let n = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : !1;
    if (t.defaultPrevented)
      return;
    t.preventDefault();
    const [e] = this.quill.selection.getRange();
    if (e == null)
      return;
    const {
      html: i,
      text: s
    } = this.onCopy(e, n);
    (a = t.clipboardData) == null || a.setData("text/plain", s), (l = t.clipboardData) == null || l.setData("text/html", i), n && $({
      range: e,
      quill: this.quill
    });
  }
  /*
   * https://www.iana.org/assignments/media-types/text/uri-list
   */
  normalizeURIList(t) {
    return t.split(/\r?\n/).filter((n) => n[0] !== "#").join(`
`);
  }
  onCapturePaste(t) {
    var a, l, u, o, E;
    if (t.defaultPrevented || !this.quill.isEnabled())
      return;
    t.preventDefault();
    const n = this.quill.getSelection(!0);
    if (n == null)
      return;
    const e = (a = t.clipboardData) == null ? void 0 : a.getData("text/html");
    let i = (l = t.clipboardData) == null ? void 0 : l.getData("text/plain");
    if (!e && !i) {
      const h = (u = t.clipboardData) == null ? void 0 : u.getData("text/uri-list");
      h && (i = this.normalizeURIList(h));
    }
    const s = Array.from(((o = t.clipboardData) == null ? void 0 : o.files) || []);
    if (!e && s.length > 0) {
      this.quill.uploader.upload(n, s);
      return;
    }
    if (e && s.length > 0) {
      const h = new DOMParser().parseFromString(e, "text/html");
      if (h.body.childElementCount === 1 && ((E = h.body.firstElementChild) == null ? void 0 : E.tagName) === "IMG") {
        this.quill.uploader.upload(n, s);
        return;
      }
    }
    this.onPaste(n, {
      html: e,
      text: i
    });
  }
  onCopy(t) {
    const n = this.quill.getText(t);
    return {
      html: this.quill.getSemanticHTML(t),
      text: n
    };
  }
  onPaste(t, n) {
    let {
      text: e,
      html: i
    } = n;
    const s = this.quill.getFormat(t.index), a = this.convert({
      text: e,
      html: i
    }, s);
    j.log("onPaste", a, {
      text: e,
      html: i
    });
    const l = new c().retain(t.index).delete(t.length).concat(a);
    this.quill.updateContents(l, p.sources.USER), this.quill.setSelection(l.length() - t.length, p.sources.SILENT), this.quill.scrollSelectionIntoView();
  }
  prepareMatching(t, n) {
    const e = [], i = [];
    return this.matchers.forEach((s) => {
      const [a, l] = s;
      switch (a) {
        case Node.TEXT_NODE:
          i.push(l);
          break;
        case Node.ELEMENT_NODE:
          e.push(l);
          break;
        default:
          Array.from(t.querySelectorAll(a)).forEach((u) => {
            if (n.has(u)) {
              const o = n.get(u);
              o == null || o.push(l);
            } else
              n.set(u, [l]);
          });
          break;
      }
    }), [e, i];
  }
}
N(V, "DEFAULTS", {
  matchers: []
});
function m(r, t, n, e) {
  return e.query(t) ? r.reduce((i, s) => {
    if (!s.insert)
      return i;
    if (s.attributes && s.attributes[t])
      return i.push(s);
    const a = n ? {
      [t]: n
    } : {};
    return i.insert(s.insert, {
      ...a,
      ...s.attributes
    });
  }, new c()) : r;
}
function g(r, t) {
  let n = "";
  for (let e = r.ops.length - 1; e >= 0 && n.length < t.length; --e) {
    const i = r.ops[e];
    if (typeof i.insert != "string")
      break;
    n = i.insert + n;
  }
  return n.slice(-1 * t.length) === t;
}
function f(r, t) {
  if (!(r instanceof Element))
    return !1;
  const n = t.query(r);
  return n && n.prototype instanceof L ? !1 : ["address", "article", "blockquote", "canvas", "dd", "div", "dl", "dt", "fieldset", "figcaption", "figure", "footer", "form", "h1", "h2", "h3", "h4", "h5", "h6", "header", "iframe", "li", "main", "nav", "ol", "output", "p", "pre", "section", "table", "td", "tr", "ul", "video"].includes(r.tagName.toLowerCase());
}
function Y(r, t) {
  return r.previousElementSibling && r.nextElementSibling && !f(r.previousElementSibling, t) && !f(r.nextElementSibling, t);
}
const b = /* @__PURE__ */ new WeakMap();
function D(r) {
  return r == null ? !1 : (b.has(r) || (r.tagName === "PRE" ? b.set(r, !0) : b.set(r, D(r.parentNode))), b.get(r));
}
function q(r, t, n, e, i) {
  return t.nodeType === t.TEXT_NODE ? e.reduce((s, a) => a(t, s, r), new c()) : t.nodeType === t.ELEMENT_NODE ? Array.from(t.childNodes || []).reduce((s, a) => {
    let l = q(r, a, n, e, i);
    return a.nodeType === t.ELEMENT_NODE && (l = n.reduce((u, o) => o(a, u, r), l), l = (i.get(a) || []).reduce((u, o) => o(a, u, r), l)), s.concat(l);
  }, new c()) : new c();
}
function y(r) {
  return (t, n, e) => m(n, r, !0, e);
}
function J(r, t, n) {
  const e = v.keys(r), i = A.keys(r), s = M.keys(r), a = {};
  return e.concat(i).concat(s).forEach((l) => {
    let u = n.query(l, w.ATTRIBUTE);
    u != null && (a[u.attrName] = u.value(r), a[u.attrName]) || (u = Q[l], u != null && (u.attrName === l || u.keyName === l) && (a[u.attrName] = u.value(r) || void 0), u = T[l], u != null && (u.attrName === l || u.keyName === l) && (u = T[l], a[u.attrName] = u.value(r) || void 0));
  }), Object.entries(a).reduce((l, u) => {
    let [o, E] = u;
    return m(l, o, E, n);
  }, t);
}
function K(r, t, n) {
  const e = n.query(r);
  if (e == null)
    return t;
  if (e.prototype instanceof L) {
    const i = {}, s = e.value(r);
    if (s != null)
      return i[e.blotName] = s, new c().insert(i, e.formats(r, n));
  } else if (e.prototype instanceof C && !g(t, `
`) && t.insert(`
`), "blotName" in e && "formats" in e && typeof e.formats == "function")
    return m(t, e.blotName, e.formats(r, n), n);
  return t;
}
function Z(r, t) {
  return g(t, `
`) || t.insert(`
`), t;
}
function tt(r, t, n) {
  const e = n.query("code-block"), i = e && "formats" in e && typeof e.formats == "function" ? e.formats(r, n) : !0;
  return m(t, "code-block", i, n);
}
function et() {
  return new c();
}
function rt(r, t, n) {
  const e = n.query(r);
  if (e == null || // @ts-expect-error
  e.blotName !== "list" || !g(t, `
`))
    return t;
  let i = -1, s = r.parentNode;
  for (; s != null; )
    ["OL", "UL"].includes(s.tagName) && (i += 1), s = s.parentNode;
  return i <= 0 ? t : t.reduce((a, l) => l.insert ? l.attributes && typeof l.attributes.indent == "number" ? a.push(l) : a.insert(l.insert, {
    indent: i,
    ...l.attributes || {}
  }) : a, new c());
}
function nt(r, t, n) {
  const e = r;
  let i = e.tagName === "OL" ? "ordered" : "bullet";
  const s = e.getAttribute("data-checked");
  return s && (i = s === "true" ? "checked" : "unchecked"), m(t, "list", i, n);
}
function S(r, t, n) {
  if (!g(t, `
`)) {
    if (f(r, n) && (r.childNodes.length > 0 || r instanceof HTMLParagraphElement))
      return t.insert(`
`);
    if (t.length() > 0 && r.nextSibling) {
      let e = r.nextSibling;
      for (; e != null; ) {
        if (f(e, n))
          return t.insert(`
`);
        const i = n.query(e);
        if (i && i.prototype instanceof O)
          return t.insert(`
`);
        e = e.firstChild;
      }
    }
  }
  return t;
}
function it(r, t, n) {
  var s;
  const e = {}, i = r.style || {};
  return i.fontStyle === "italic" && (e.italic = !0), i.textDecoration === "underline" && (e.underline = !0), i.textDecoration === "line-through" && (e.strike = !0), ((s = i.fontWeight) != null && s.startsWith("bold") || // @ts-expect-error Fix me later
  parseInt(i.fontWeight, 10) >= 700) && (e.bold = !0), t = Object.entries(e).reduce((a, l) => {
    let [u, o] = l;
    return m(a, u, o, n);
  }, t), parseFloat(i.textIndent || 0) > 0 ? new c().insert("	").concat(t) : t;
}
function st(r, t, n) {
  var i, s;
  const e = ((i = r.parentElement) == null ? void 0 : i.tagName) === "TABLE" ? r.parentElement : (s = r.parentElement) == null ? void 0 : s.parentElement;
  if (e != null) {
    const l = Array.from(e.querySelectorAll("tr")).indexOf(r) + 1;
    return m(t, "table", l, n);
  }
  return t;
}
function lt(r, t, n) {
  var i;
  let e = r.data;
  if (((i = r.parentElement) == null ? void 0 : i.tagName) === "O:P")
    return t.insert(e.trim());
  if (!D(r)) {
    if (e.trim().length === 0 && e.includes(`
`) && !Y(r, n))
      return t;
    const s = (a, l) => {
      const u = l.replace(/[^\u00a0]/g, "");
      return u.length < 1 && a ? " " : u;
    };
    e = e.replace(/\r\n/g, " ").replace(/\n/g, " "), e = e.replace(/\s\s+/g, s.bind(s, !0)), (r.previousSibling == null && r.parentElement != null && f(r.parentElement, n) || r.previousSibling instanceof Element && f(r.previousSibling, n)) && (e = e.replace(/^\s+/, s.bind(s, !1))), (r.nextSibling == null && r.parentElement != null && f(r.parentElement, n) || r.nextSibling instanceof Element && f(r.nextSibling, n)) && (e = e.replace(/\s+$/, s.bind(s, !1)));
  }
  return t.insert(e);
}
export {
  V as default,
  J as matchAttributor,
  K as matchBlot,
  S as matchNewline,
  lt as matchText,
  q as traverse
};
