import S from "./purify.es-CKpwg8Tk.mjs";
let n = null, s = null, d = null, o = null, m = "", g = "", b = "insert";
const M = [
  {
    id: "formal-letter",
    name: "Formal Letter",
    category: "Letters",
    description: "Professional business letter template",
    html: `<p><strong>{{ Company Name }}</strong></p>
<p>{{ Today }}</p>
<p>Dear {{ first_name }} {{ last_name }},</p>
<p>I hope this letter finds you well. [Your letter content here]</p>
<p>Thank you for your time and consideration.</p>
<p>Sincerely,<br>Your Name</p>`
  },
  {
    id: "meeting-notes",
    name: "Meeting Notes",
    category: "Notes",
    description: "Template for meeting notes with attendees and action items",
    html: `<h2>Meeting Notes - {{ today }}</h2>
<p><strong>Attendees:</strong> [List attendees]</p>
<p><strong>Agenda:</strong></p>
<ul>
  <li>[Item 1]</li>
  <li>[Item 2]</li>
  <li>[Item 3]</li>
</ul>
<p><strong>Action Items:</strong></p>
<ul>
  <li>[Owner]: [Task] - [Due Date]</li>
</ul>
<p><strong>Next Meeting:</strong> [Date]</p>`
  },
  {
    id: "proposal",
    name: "Project Proposal",
    category: "Business",
    description: "Structured project proposal template",
    html: `<h1>Project Proposal</h1>
<h2>Executive Summary</h2>
<p>[Summary of the proposal]</p>
<h2>Objectives</h2>
<ul>
  <li>[Objective 1]</li>
  <li>[Objective 2]</li>
</ul>
<h2>Scope</h2>
<p>[Project scope details]</p>
<h2>Timeline</h2>
<p>[Project timeline]</p>
<h2>Budget</h2>
<p>[Budget details]</p>
<h2>Contact</h2>
<p>{{ first_name }} {{ last_name }}<br>{{ email }}<br>{{ phone }}</p>`
  },
  {
    id: "faq",
    name: "FAQ Template",
    category: "Documentation",
    description: "FAQ document structure",
    html: `<h1>Frequently Asked Questions</h1>
<h2>General Questions</h2>
<h3>Q: What is this about?</h3>
<p>A: [Answer here]</p>
<h3>Q: Who should use this?</h3>
<p>A: [Answer here]</p>
<h2>Technical Questions</h2>
<h3>Q: How do I get started?</h3>
<p>A: [Answer here]</p>
<h3>Q: What are the requirements?</h3>
<p>A: [Answer here]</p>`
  }
];
let h = [...M];
const w = () => h, P = (e) => h.filter((t) => t.category === e), L = () => {
  const e = new Set(h.map((t) => t.category));
  return Array.from(e);
}, R = (e) => {
  const t = e.toLowerCase();
  return h.filter(
    (r) => {
      var i, c;
      return r.name.toLowerCase().includes(t) || ((i = r.description) == null ? void 0 : i.toLowerCase().includes(t)) || ((c = r.tags) == null ? void 0 : c.some((u) => u.toLowerCase().includes(t)));
    }
  );
}, A = (e) => S.sanitize(e, {
  ALLOWED_TAGS: ["p", "br", "strong", "em", "u", "h1", "h2", "h3", "h4", "ul", "ol", "li", "blockquote", "table", "thead", "tbody", "tr", "th", "td", "a", "span"],
  ALLOWED_ATTR: ["href", "target", "class", "data-key", "data-category"]
}), _ = (e) => h.some((t) => t.id === e.id) ? (console.warn(`Template with ID ${e.id} already exists`), !1) : (h.push(e), !0), j = (e) => !!(e.id && e.name && e.category && e.html && e.html.trim().length > 0);
function D() {
  s = document.createElement("div"), s.className = "rte-dialog-overlay", s.addEventListener("click", () => p()), n = document.createElement("div"), n.className = "rte-dialog rte-template-dialog", n.addEventListener("click", (t) => t.stopPropagation());
  const e = L();
  e.length > 0 && !m && (m = e[0]), y(), s.appendChild(n), document.body.appendChild(s);
}
function y() {
  if (!n) return;
  const e = L(), t = T();
  n.innerHTML = `
    <div class="rte-dialog-header">
      <h2>Insert Template</h2>
      <button class="rte-dialog-close" aria-label="Close">✕</button>
    </div>

    <div class="rte-dialog-body">
      <!-- Search -->
      <input
        type="text"
        placeholder="Search templates..."
        value="${g}"
        class="rte-input rte-template-search"
        aria-label="Search templates"
      />

      <!-- Category Tabs -->
      <div class="rte-tabs">
        ${e.map((r) => `
          <button class="rte-tab ${m === r ? "active" : ""}" data-category="${r}">
            ${r}
          </button>
        `).join("")}
      </div>

      <!-- Template List -->
      <div class="rte-template-list">
        ${t.length > 0 ? t.map((r) => `
          <div
            class="rte-template-item ${(o == null ? void 0 : o.id) === r.id ? "selected" : ""}"
            data-template-id="${r.id}"
          >
            <div class="template-name">${r.name}</div>
            ${r.description ? `<div class="template-description">${r.description}</div>` : ""}
          </div>
        `).join("") : '<div class="rte-empty-state">No templates found</div>'}
      </div>

      <!-- Preview -->
      ${o ? `
        <div class="rte-template-preview">
          <strong>Preview:</strong>
          <div class="template-preview-content">${o.html}</div>
        </div>
      ` : ""}

      <!-- Insert Mode Toggle -->
      <div class="rte-insert-mode">
        <label>
          <input type="radio" name="insertMode" value="insert" ${b === "insert" ? "checked" : ""} />
          Insert at cursor
        </label>
        <label>
          <input type="radio" name="insertMode" value="replace" ${b === "replace" ? "checked" : ""} />
          Replace document
        </label>
      </div>
    </div>

    <div class="rte-dialog-footer">
      <button class="rte-button-secondary rte-cancel-btn">Cancel</button>
      <button class="rte-button-primary rte-insert-btn" ${o ? "" : "disabled"}>
        ${b === "insert" ? "Insert" : "Replace"}
      </button>
    </div>
  `, H();
}
function $() {
  if (!n) return;
  n.innerHTML = `
    <div class="rte-dialog-header">
      <h2>Replace Document?</h2>
    </div>
    <div class="rte-dialog-body">
      <p>This will replace your current document content. Continue?</p>
    </div>
    <div class="rte-dialog-footer">
      <button class="rte-button-secondary rte-cancel-warning-btn">Cancel</button>
      <button class="rte-button-primary rte-confirm-replace-btn">Replace</button>
    </div>
  `;
  const e = n.querySelector(".rte-cancel-warning-btn"), t = n.querySelector(".rte-confirm-replace-btn");
  e == null || e.addEventListener("click", () => y()), t == null || t.addEventListener("click", () => N());
}
function T() {
  const e = w();
  return g.trim() ? R(g) : m ? e.filter((t) => t.category === m) : e;
}
function H() {
  if (!n) return;
  const e = n.querySelector(".rte-dialog-close");
  e == null || e.addEventListener("click", () => p());
  const t = n.querySelector(".rte-cancel-btn");
  t == null || t.addEventListener("click", () => p());
  const r = n.querySelector(".rte-insert-btn");
  r == null || r.addEventListener("click", () => v());
  const i = n.querySelector(".rte-template-search");
  i == null || i.addEventListener("input", (a) => {
    g = a.target.value, E();
  }), i == null || i.addEventListener("keydown", (a) => {
    a.key === "Enter" && o ? v() : a.key === "Escape" && p();
  }), n.querySelectorAll(".rte-tab").forEach((a) => {
    a.addEventListener("click", () => {
      const l = a.getAttribute("data-category");
      l && (m = l, g = "", E());
    });
  }), n.querySelectorAll(".rte-template-item").forEach((a) => {
    a.addEventListener("click", () => {
      const l = a.getAttribute("data-template-id");
      if (l) {
        const f = w().find((x) => x.id === l);
        f && (o = f, y());
      }
    }), a.addEventListener("dblclick", () => {
      const l = a.getAttribute("data-template-id");
      if (l) {
        const f = w().find((x) => x.id === l);
        f && (o = f, v());
      }
    });
  }), n.querySelectorAll('input[name="insertMode"]').forEach((a) => {
    a.addEventListener("change", (l) => {
      b = l.target.value, y();
    });
  });
}
function E() {
  const e = T();
  e.length > 0 ? (!o || !e.find((t) => t.id === o.id)) && (o = e[0]) : o = null, y();
}
function v() {
  var e;
  if (o)
    if (b === "replace") {
      let t = null;
      if (d) {
        let r = d.startContainer;
        for (; r && r !== document.body; ) {
          if (r.nodeType === Node.ELEMENT_NODE) {
            const i = r;
            if (i.getAttribute("contenteditable") === "true") {
              t = i;
              break;
            }
          }
          r = r.parentNode;
        }
      }
      if (t || (t = document.querySelector('[contenteditable="true"]')), (e = t == null ? void 0 : t.innerHTML) != null && e.trim()) {
        $();
        return;
      }
      C(o), p();
    } else
      I(o), p();
}
function N() {
  o && (C(o), p());
}
function I(e) {
  if (d) {
    const u = window.getSelection();
    u && (u.removeAllRanges(), u.addRange(d));
  }
  const t = window.getSelection();
  if (!t || t.rangeCount === 0) return;
  const r = t.getRangeAt(0), i = document.createRange().createContextualFragment(A(e.html));
  r.deleteContents(), r.insertNode(i);
  const c = document.createRange();
  c.setStartAfter(r.endContainer), c.collapse(!0), t.removeAllRanges(), t.addRange(c), console.log(`Template inserted: ${e.name}`);
}
function C(e) {
  let t = null;
  if (d) {
    let r = d.startContainer;
    for (; r && r !== document.body; ) {
      if (r.nodeType === Node.ELEMENT_NODE) {
        const i = r;
        if (i.getAttribute("contenteditable") === "true") {
          t = i;
          break;
        }
      }
      r = r.parentNode;
    }
  }
  t || (t = document.querySelector('[contenteditable="true"]')), t && (t.innerHTML = A(e.html), t.dispatchEvent(new Event("input", { bubbles: !0 }))), console.log(`Document replaced with template: ${e.name}`);
}
function p() {
  s && (s.remove(), s = null), n = null, d = null, g = "";
}
function V() {
  const e = window.getSelection();
  e && e.rangeCount > 0 ? d = e.getRangeAt(0).cloneRange() : d = null;
  const t = T();
  t.length > 0 && !o && (o = t[0]), D();
}
function k() {
  if (!window.__templatePluginInitialized && (window.__templatePluginInitialized = !0, !document.getElementById("template-plugin-styles"))) {
    const e = document.createElement("style");
    e.id = "template-plugin-styles", e.textContent = `
      .rte-dialog-overlay {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background-color: rgba(0, 0, 0, 0.5);
        display: flex;
        align-items: center;
        justify-content: center;
        z-index: 10000;
      }

      .rte-dialog {
        background: white;
        border-radius: 8px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
        max-width: 90vw;
        max-height: 90vh;
        overflow: hidden;
        display: flex;
        flex-direction: column;
      }

      .rte-dialog-header {
        padding: 16px;
        border-bottom: 1px solid #eee;
        display: flex;
        justify-content: space-between;
        align-items: center;
      }

      .rte-dialog-header h2 {
        margin: 0;
        font-size: 18px;
        font-weight: 600;
      }

      .rte-dialog-close {
        background: none;
        border: none;
        font-size: 24px;
        cursor: pointer;
        color: #999;
        padding: 0;
        width: 32px;
        height: 32px;
        display: flex;
        align-items: center;
        justify-content: center;
      }

      .rte-dialog-close:hover {
        color: #333;
      }

      .rte-dialog-body {
        padding: 16px;
        overflow-y: auto;
        flex: 1;
      }

      .rte-dialog-footer {
        padding: 12px 16px;
        border-top: 1px solid #eee;
        display: flex;
        gap: 8px;
        justify-content: flex-end;
      }

      .rte-button-primary {
        padding: 8px 16px;
        border: none;
        border-radius: 4px;
        font-size: 14px;
        cursor: pointer;
        background-color: #1976d2;
        color: white;
        transition: all 0.2s;
      }

      .rte-button-primary:hover:not([disabled]) {
        background-color: #1565c0;
      }

      .rte-button-primary[disabled] {
        opacity: 0.5;
        cursor: not-allowed;
      }

      .rte-button-secondary {
        padding: 8px 16px;
        border: 1px solid #ddd;
        border-radius: 4px;
        font-size: 14px;
        cursor: pointer;
        background-color: #f5f5f5;
        color: #333;
        transition: all 0.2s;
      }

      .rte-button-secondary:hover {
        background-color: #eeeeee;
      }

      .rte-template-dialog {
        width: 600px;
        max-height: 700px;
      }

      .rte-input {
        width: 100%;
        padding: 8px 12px;
        border: 1px solid #ddd;
        border-radius: 4px;
        font-size: 14px;
        box-sizing: border-box;
      }

      .rte-input:focus {
        outline: none;
        border-color: #1976d2;
      }

      .rte-tabs {
        display: flex;
        gap: 8px;
        margin-top: 12px;
        border-bottom: 1px solid #eee;
        padding-bottom: 8px;
      }

      .rte-tab {
        padding: 6px 12px;
        border: none;
        background: none;
        cursor: pointer;
        font-size: 14px;
        color: #666;
        border-bottom: 2px solid transparent;
        transition: all 0.2s;
      }

      .rte-tab:hover {
        color: #333;
      }

      .rte-tab.active {
        color: #1976d2;
        border-bottom-color: #1976d2;
        font-weight: 600;
      }

      .rte-template-list {
        border: 1px solid #ddd;
        border-radius: 4px;
        max-height: 250px;
        overflow-y: auto;
        margin: 12px 0;
      }

      .rte-template-item {
        padding: 12px;
        border-bottom: 1px solid #f0f0f0;
        cursor: pointer;
        transition: background-color 0.2s;
      }

      .rte-template-item:last-child {
        border-bottom: none;
      }

      .rte-template-item:hover,
      .rte-template-item.selected {
        background-color: #f5f5f5;
      }

      .template-name {
        font-weight: 600;
        color: #333;
        margin-bottom: 4px;
      }

      .template-description {
        font-size: 12px;
        color: #999;
      }

      .rte-template-preview {
        padding: 12px;
        background-color: #fafafa;
        border: 1px solid #eee;
        border-radius: 4px;
        margin-top: 12px;
        max-height: 200px;
        overflow-y: auto;
      }

      .template-preview-content {
        font-size: 13px;
        line-height: 1.5;
        margin-top: 8px;
      }

      .template-preview-content * {
        margin: 4px 0;
      }

      .rte-insert-mode {
        margin-top: 12px;
        padding: 12px;
        background-color: #f5f5f5;
        border-radius: 4px;
        display: flex;
        gap: 16px;
      }

      .rte-insert-mode label {
        display: flex;
        align-items: center;
        cursor: pointer;
        font-size: 14px;
      }

      .rte-insert-mode input {
        margin-right: 6px;
        cursor: pointer;
      }

      .rte-empty-state {
        padding: 40px;
        text-align: center;
        color: #999;
        font-size: 14px;
      }
    `, document.head.appendChild(e);
  }
}
document.readyState === "loading" ? document.addEventListener("DOMContentLoaded", k) : setTimeout(k, 100);
const O = () => ({
  name: "template",
  toolbar: [
    {
      label: "Template",
      command: "insertTemplate",
      icon: '<svg width="24px" height="24px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"> <path fill-rule="evenodd" clip-rule="evenodd" d="M3 3V9H21V3H3ZM19 5H5V7H19V5Z" fill="#000000"></path> <path fill-rule="evenodd" clip-rule="evenodd" d="M3 11V21H11V11H3ZM9 13H5V19H9V13Z" fill="#000000"></path> <path d="M21 11H13V13H21V11Z" fill="#000000"></path> <path d="M13 15H21V17H13V15Z" fill="#000000"></path> <path d="M21 19H13V21H21V19Z" fill="#000000"></path> </g></svg>'
    }
  ],
  commands: {
    insertTemplate: () => (V(), !0)
  },
  keymap: {}
});
export {
  M as PREDEFINED_TEMPLATES,
  O as TemplatePlugin,
  _ as addCustomTemplate,
  w as getAllTemplates,
  L as getTemplateCategories,
  P as getTemplatesByCategory,
  A as sanitizeTemplate,
  R as searchTemplates,
  j as validateTemplate
};
//# sourceMappingURL=TemplatePlugin.native-BlSn1c9h.mjs.map
