const H = ".rte-content, .editora-content";
const b = "__editoraCommandEditorRoot", d = /* @__PURE__ */ new Map(), l = {};
let T = !1, c = null;
function _(t) {
  if (!t) return null;
  const e = t.querySelector('[contenteditable="true"]');
  return e instanceof HTMLElement ? e : null;
}
function R() {
  if (typeof window > "u") return null;
  const t = window[b];
  if (!(t instanceof HTMLElement)) return null;
  window[b] = null;
  const e = t.closest("[data-editora-editor], .rte-editor, .editora-editor, editora-editor") || (t.matches("[data-editora-editor], .rte-editor, .editora-editor, editora-editor") ? t : null);
  if (e) {
    const o = _(e);
    if (o) return o;
    if (e.getAttribute("contenteditable") === "true") return e;
  }
  if (t.getAttribute("contenteditable") === "true")
    return t;
  const n = t.closest('[contenteditable="true"]');
  return n instanceof HTMLElement ? n : null;
}
function I(t) {
  return t ? t.nodeType === Node.ELEMENT_NODE ? t : t.parentElement : null;
}
function s(t) {
  const e = I(t);
  if (!e) return null;
  const n = e.closest(H);
  if (n) return n;
  const o = e.closest('[contenteditable="true"]');
  if (!o) return null;
  let r = o, i = r.parentElement;
  for (; i; )
    i.getAttribute("contenteditable") === "true" && (r = i), i = i.parentElement;
  return r;
}
function E() {
  const t = R();
  if (t && document.contains(t))
    return t;
  const e = window.getSelection();
  if (e && e.rangeCount > 0) {
    const i = s(e.getRangeAt(0).startContainer);
    if (i) return i;
  }
  const n = document.activeElement;
  if (n) {
    const i = s(n);
    if (i) return i;
  }
  if (c?.isConnected) return c;
  const o = Array.from(d.keys()).find((i) => i.isConnected);
  if (o) return o;
  const r = document.querySelector(H);
  return r || document.querySelector('[contenteditable="true"]');
}
function h() {
  for (const t of d.keys())
    t.isConnected || (d.delete(t), c === t && (c = null));
}
function p(t) {
  h();
  let e = d.get(t);
  return e || (e = { undoStack: [], redoStack: [] }, d.set(t, e)), e;
}
function a(t) {
  t && t.dispatchEvent(new Event("input", { bubbles: !0 }));
}
function f(t, e) {
  if (!t) return;
  const n = p(t);
  n.undoStack.push(e), n.redoStack.length = 0, n.undoStack.length > 100 && n.undoStack.splice(0, n.undoStack.length - 100), c = t;
}
function S(t, e = "undo") {
  if (h(), t?.isConnected)
    return t;
  const n = E();
  if (n?.isConnected)
    return n;
  if (c?.isConnected)
    return c;
  const o = (r) => e === "undo" ? r.undoStack.length > 0 : r.redoStack.length > 0;
  for (const [r, i] of d.entries())
    if (r.isConnected && o(i))
      return r;
  return null;
}
function y(t) {
  const e = {};
  for (const r of t.getAttributeNames()) {
    const i = t.getAttribute(r);
    i !== null && (e[r] = i);
  }
  const n = t instanceof HTMLInputElement || t instanceof HTMLTextAreaElement || t instanceof HTMLSelectElement, o = t instanceof HTMLInputElement;
  return {
    attributes: e,
    innerHTML: t.innerHTML,
    value: n ? t.value : null,
    checked: o ? t.checked : null
  };
}
function g(t, e) {
  const n = new Set(t.getAttributeNames());
  Object.keys(e.attributes).forEach((o) => n.delete(o)), n.forEach((o) => t.removeAttribute(o)), Object.entries(e.attributes).forEach(([o, r]) => {
    t.setAttribute(o, r);
  }), t.innerHTML !== e.innerHTML && (t.innerHTML = e.innerHTML), e.value !== null && (t instanceof HTMLInputElement || t instanceof HTMLTextAreaElement || t instanceof HTMLSelectElement) && (t.value = e.value), e.checked !== null && t instanceof HTMLInputElement && (t.checked = e.checked);
}
function u(t, e) {
  if (l[t] = e, typeof window > "u") return;
  const n = window.registerEditorCommand;
  typeof n == "function" ? n(t, e) : (window.registerEditorCommand = (o, r) => {
    l[o] = r;
  }, window.registerEditorCommand(t, e));
}
const w = (t, ...e) => {
  const n = l[t];
  return n ? n(...e) : !1;
};
function m() {
  T || typeof window > "u" || (T = !0, window.execEditorCommand || (window.execEditorCommand = w), window.executeEditorCommand || (window.executeEditorCommand = w), u("undo", A), u("redo", L), u("setAttribute", v), u("setText", x), u("autoFixA11y", k), u("recordDomTransaction", O), u("undoDom", C), u("redoDom", M));
}
const A = () => {
  const t = E();
  return C(t ?? void 0);
}, L = () => {
  const t = E();
  return M(t ?? void 0);
}, v = (t, e, n) => {
  if (!(t instanceof HTMLElement)) return;
  const o = s(t), r = t.hasAttribute(e), i = t.getAttribute(e);
  t.setAttribute(e, n), f(o, {
    undo: () => {
      t.isConnected && (r && i !== null ? t.setAttribute(e, i) : t.removeAttribute(e));
    },
    redo: () => {
      t.isConnected && t.setAttribute(e, n);
    }
  }), a(o);
}, x = (t, e) => {
  if (!(t instanceof HTMLElement)) return;
  const n = s(t), o = t.textContent ?? "";
  t.textContent = e, f(n, {
    undo: () => {
      t.isConnected && (t.textContent = o);
    },
    redo: () => {
      t.isConnected && (t.textContent = e);
    }
  }), a(n);
}, k = (t) => {
  const e = t?.element;
  if (!(e instanceof HTMLElement)) return;
  const n = s(e), o = window.a11yRuleRegistry?.find((D) => D.id === t.rule);
  if (!o || typeof o.fix != "function") return;
  const r = y(e);
  o.fix(t);
  const i = y(e);
  f(n, {
    undo: () => {
      e.isConnected && g(e, r);
    },
    redo: () => {
      e.isConnected && g(e, i);
    }
  }), a(n);
}, O = (t, e, n) => {
  if (!(t instanceof HTMLElement)) return !1;
  const o = typeof n == "string" ? n : t.innerHTML;
  return e === o ? !1 : (f(t, {
    undo: () => {
      t.isConnected && (t.innerHTML = e);
    },
    redo: () => {
      t.isConnected && (t.innerHTML = o);
    }
  }), !0);
}, C = (t) => {
  const e = S(t, "undo");
  if (!e) return !1;
  const n = p(e), o = n.undoStack.pop();
  return o ? (o.undo(), n.redoStack.push(o), c = e, a(e), !0) : !1;
}, M = (t) => {
  const e = S(t, "redo");
  if (!e) return !1;
  const n = p(e), o = n.redoStack.pop();
  return o ? (o.redo(), n.undoStack.push(o), c = e, a(e), !0) : !1;
};
typeof window < "u" && (document.readyState === "loading" ? document.addEventListener("DOMContentLoaded", m, { once: !0 }) : m());
const N = () => (m(), {
  name: "history",
  toolbar: [
    {
      label: "Undo",
      command: "undo",
      type: "button",
      icon: '<svg width="24" height="24" focusable="false"><path d="M6.4 8H12c3.7 0 6.2 2 6.8 5.1.6 2.7-.4 5.6-2.3 6.8a1 1 0 0 1-1-1.8c1.1-.6 1.8-2.7 1.4-4.6-.5-2.1-2.1-3.5-4.9-3.5H6.4l3.3 3.3a1 1 0 1 1-1.4 1.4l-5-5a1 1 0 0 1 0-1.4l5-5a1 1 0 0 1 1.4 1.4L6.4 8Z" fill-rule="nonzero"></path></svg>',
      shortcut: "Mod-z"
    },
    {
      label: "Redo",
      command: "redo",
      type: "button",
      icon: '<svg width="24" height="24" focusable="false"><path d="M17.6 10H12c-2.8 0-4.4 1.4-4.9 3.5-.4 2 .3 4 1.4 4.6a1 1 0 1 1-1 1.8c-2-1.2-2.9-4.1-2.3-6.8.6-3 3-5.1 6.8-5.1h5.6l-3.3-3.3a1 1 0 1 1 1.4-1.4l5 5a1 1 0 0 1 0 1.4l-5 5a1 1 0 0 1-1.4-1.4l3.3-3.3Z" fill-rule="nonzero"></path></svg>',
      shortcut: "Mod-y"
    }
  ],
  commands: {
    undo: A,
    redo: L,
    setAttribute: v,
    setText: x,
    autoFixA11y: k,
    recordDomTransaction: O,
    undoDom: C,
    redoDom: M
  },
  keymap: {
    "Mod-z": "undo",
    "Mod-Z": "undo",
    "Mod-y": "redo",
    "Mod-Y": "redo",
    "Mod-Shift-z": "redo",
    "Mod-Shift-Z": "redo"
  }
});
export {
  N as HistoryPlugin
};
