let D = null, m = null, s = null, _ = [], H = !1, A = null, U = 0, j = 0, q = 0, M = 0, P = 1, R = null;
const F = '[data-theme="dark"], .dark, .editora-theme-dark', I = (t, e) => {
  const r = (a) => {
    a.key !== "Escape" || !t.isConnected || (a.preventDefault(), a.stopPropagation(), e());
  };
  return document.addEventListener("keydown", r, !0), () => {
    document.removeEventListener("keydown", r, !0);
  };
}, X = () => {
  if (typeof document == "undefined" || document.getElementById("rte-media-dialog-styles")) return;
  const t = document.createElement("style");
  t.id = "rte-media-dialog-styles", t.textContent = `
    .rte-media-overlay {
      --rte-media-overlay-bg: rgba(15, 23, 36, 0.56);
      --rte-media-bg: #ffffff;
      --rte-media-text: #101828;
      --rte-media-muted: #5f6b7d;
      --rte-media-border: #d6dbe4;
      --rte-media-surface: #f7f9fc;
      --rte-media-surface-hover: #eef2f7;
      --rte-media-accent: #1f75fe;
      --rte-media-accent-hover: #165fd6;
      --rte-media-danger: #dc3545;
      --rte-media-danger-hover: #b92735;
      --rte-media-ring: rgba(31, 117, 254, 0.18);
      position: fixed;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background: var(--rte-media-overlay-bg);
      backdrop-filter: blur(2px);
      display: flex;
      align-items: center;
      justify-content: center;
      z-index: 99999;
      padding: 16px;
      box-sizing: border-box;
    }

    .rte-media-overlay.rte-ui-theme-dark {
      --rte-media-overlay-bg: rgba(2, 8, 20, 0.72);
      --rte-media-bg: #202938;
      --rte-media-text: #e8effc;
      --rte-media-muted: #a5b1c5;
      --rte-media-border: #49566c;
      --rte-media-surface: #2a3444;
      --rte-media-surface-hover: #344256;
      --rte-media-accent: #58a6ff;
      --rte-media-accent-hover: #4598f4;
      --rte-media-danger: #ff7b72;
      --rte-media-danger-hover: #ff645b;
      --rte-media-ring: rgba(88, 166, 255, 0.22);
    }

    .rte-media-dialog {
      width: min(92vw, 640px);
      max-height: 90vh;
      display: flex;
      flex-direction: column;
      overflow: hidden;
      background: var(--rte-media-bg);
      color: var(--rte-media-text);
      border: 1px solid var(--rte-media-border);
      border-radius: 12px;
      box-shadow: 0 24px 48px rgba(10, 15, 24, 0.28);
    }

    .rte-media-dialog.rte-media-dialog-compact {
      width: min(92vw, 520px);
    }

    .rte-media-header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 16px 20px;
      border-bottom: 1px solid var(--rte-media-border);
      background: linear-gradient(180deg, rgba(127, 154, 195, 0.08) 0%, rgba(127, 154, 195, 0) 100%);
    }

    .rte-media-title {
      margin: 0;
      font-size: 18px;
      font-weight: 600;
      color: var(--rte-media-text);
    }

    .rte-media-close-btn {
      width: 32px;
      height: 32px;
      border: none;
      border-radius: 8px;
      background: transparent;
      color: var(--rte-media-muted);
      font-size: 24px;
      line-height: 1;
      cursor: pointer;
      transition: background-color 0.16s ease, color 0.16s ease;
    }

    .rte-media-close-btn:hover {
      background: var(--rte-media-surface-hover);
      color: var(--rte-media-text);
    }

    .rte-media-tabs {
      display: grid;
      grid-template-columns: 1fr 1fr;
      border-bottom: 1px solid var(--rte-media-border);
      gap: 0;
    }

    .rte-media-tab {
      border: none;
      border-right: 1px solid var(--rte-media-border);
      padding: 12px 14px;
      background: var(--rte-media-surface);
      color: var(--rte-media-muted);
      font-size: 14px;
      font-weight: 600;
      cursor: pointer;
      transition: background-color 0.16s ease, color 0.16s ease;
    }

    .rte-media-tab:last-child {
      border-right: none;
    }

    .rte-media-tab:hover {
      background: var(--rte-media-surface-hover);
      color: var(--rte-media-text);
    }

    .rte-media-tab.active {
      background: var(--rte-media-accent);
      color: #fff;
    }

    .rte-media-body {
      padding: 20px;
      overflow-y: auto;
      flex: 1;
    }

    .rte-media-field {
      margin-bottom: 16px;
    }

    .rte-media-grid {
      display: grid;
      grid-template-columns: 1fr 1fr;
      gap: 12px;
      margin-bottom: 16px;
    }

    .rte-media-label {
      display: block;
      margin-bottom: 8px;
      color: var(--rte-media-text);
      font-size: 14px;
      font-weight: 600;
    }

    .rte-media-input,
    .rte-media-textarea {
      width: 100%;
      box-sizing: border-box;
      padding: 10px 12px;
      border: 1px solid var(--rte-media-border);
      border-radius: 8px;
      background: var(--rte-media-surface);
      color: var(--rte-media-text);
      font-size: 14px;
      transition: border-color 0.16s ease, box-shadow 0.16s ease;
    }

    .rte-media-input::placeholder,
    .rte-media-textarea::placeholder {
      color: var(--rte-media-muted);
    }

    .rte-media-input:focus,
    .rte-media-textarea:focus {
      outline: none;
      border-color: var(--rte-media-accent);
      box-shadow: 0 0 0 3px var(--rte-media-ring);
    }

    .rte-media-textarea {
      min-height: 92px;
      resize: vertical;
      font-family: inherit;
    }

    .rte-media-dropzone {
      border: 2px dashed var(--rte-media-border);
      border-radius: 12px;
      padding: 36px 18px;
      text-align: center;
      cursor: pointer;
      background: var(--rte-media-surface);
      transition: border-color 0.16s ease, background-color 0.16s ease;
    }

    .rte-media-dropzone:hover,
    .rte-media-dropzone.is-dragover {
      border-color: var(--rte-media-accent);
      background: var(--rte-media-surface-hover);
    }

    .rte-media-dropzone-icon {
      font-size: 40px;
      margin-bottom: 10px;
      line-height: 1;
    }

    .rte-media-dropzone-title {
      margin: 0 0 8px 0;
      font-size: 16px;
      font-weight: 600;
      color: var(--rte-media-text);
    }

    .rte-media-muted {
      margin: 0 0 8px 0;
      color: var(--rte-media-muted);
      font-size: 14px;
    }

    .rte-media-hint {
      margin: 0;
      color: var(--rte-media-muted);
      font-size: 12px;
    }

    .rte-media-progress {
      margin-top: 16px;
    }

    .rte-media-progress-track {
      height: 8px;
      border-radius: 999px;
      background: var(--rte-media-surface);
      overflow: hidden;
      border: 1px solid var(--rte-media-border);
    }

    .rte-media-progress-bar {
      height: 100%;
      width: 0;
      background: var(--rte-media-accent);
      transition: width 0.3s ease;
    }

    .rte-media-progress-text {
      margin-top: 8px;
      text-align: center;
      color: var(--rte-media-muted);
      font-size: 13px;
    }

    .rte-media-preview {
      border: 1px solid var(--rte-media-border);
      border-radius: 10px;
      padding: 10px;
      text-align: center;
      background: var(--rte-media-surface);
    }

    .rte-media-preview img,
    .rte-media-preview video {
      max-width: 100%;
      max-height: 240px;
    }

    .rte-media-helper {
      margin-top: 8px;
      margin-bottom: 0;
      font-size: 12px;
      color: var(--rte-media-muted);
      line-height: 1.5;
    }

    .rte-media-footer {
      display: flex;
      justify-content: flex-end;
      gap: 10px;
      padding: 16px 20px;
      border-top: 1px solid var(--rte-media-border);
      background: var(--rte-media-surface);
    }

    .rte-media-footer.rte-media-footer-spread {
      justify-content: space-between;
      align-items: center;
    }

    .rte-media-btn {
      border-radius: 8px;
      border: 1px solid transparent;
      padding: 10px 16px;
      font-size: 14px;
      font-weight: 600;
      cursor: pointer;
      transition: background-color 0.16s ease, border-color 0.16s ease, color 0.16s ease;
    }

    .rte-media-btn-secondary {
      background: var(--rte-media-bg);
      border-color: var(--rte-media-border);
      color: var(--rte-media-text);
    }

    .rte-media-btn-secondary:hover {
      background: var(--rte-media-surface-hover);
    }

    .rte-media-btn-primary {
      background: var(--rte-media-accent);
      color: #fff;
    }

    .rte-media-btn-primary:hover {
      background: var(--rte-media-accent-hover);
    }

    .rte-media-btn-primary:disabled {
      opacity: 0.55;
      cursor: not-allowed;
    }

    .rte-media-btn-danger {
      background: var(--rte-media-danger);
      color: #fff;
    }

    .rte-media-btn-danger:hover {
      background: var(--rte-media-danger-hover);
    }

    .rte-media-checkbox-label {
      display: inline-flex;
      align-items: center;
      gap: 8px;
      color: var(--rte-media-text);
      font-size: 14px;
      cursor: pointer;
    }

    .rte-media-checkbox-label input {
      accent-color: var(--rte-media-accent);
    }

    .rte-media-actions {
      display: flex;
      gap: 10px;
    }

    .rte-media-spacer {
      flex: 1;
    }

    .media-floating-toolbar {
      --rte-media-toolbar-bg: #ffffff;
      --rte-media-toolbar-border: #d6dbe4;
      --rte-media-toolbar-text: #344054;
      --rte-media-toolbar-hover-bg: #f3f6fb;
      --rte-media-toolbar-hover-text: #101828;
      --rte-media-toolbar-active-bg: #e6edf7;
      --rte-media-toolbar-separator: #d9e1eb;
      --rte-media-toolbar-danger-hover-bg: #fee2e2;
      --rte-media-toolbar-danger-hover-text: #b42318;
      position: absolute;
      display: none;
      align-items: center;
      gap: 2px;
      padding: 4px;
      border: 1px solid var(--rte-media-toolbar-border);
      border-radius: 8px;
      background: var(--rte-media-toolbar-bg);
      color: var(--rte-media-toolbar-text);
      box-shadow: 0 10px 24px rgba(15, 23, 36, 0.18);
      z-index: 10000;
      pointer-events: auto;
      backdrop-filter: blur(6px);
    }

    .media-floating-toolbar.rte-ui-theme-dark,
    ${F} .media-floating-toolbar {
      --rte-media-toolbar-bg: #24303f;
      --rte-media-toolbar-border: #4a5a71;
      --rte-media-toolbar-text: #d9e6fb;
      --rte-media-toolbar-hover-bg: #33445a;
      --rte-media-toolbar-hover-text: #f4f8ff;
      --rte-media-toolbar-active-bg: #415875;
      --rte-media-toolbar-separator: #566884;
      --rte-media-toolbar-danger-hover-bg: #5f2a32;
      --rte-media-toolbar-danger-hover-text: #ffd7d5;
      box-shadow: 0 16px 30px rgba(2, 8, 20, 0.42);
    }

    .media-floating-toolbar-btn {
      width: 30px;
      height: 30px;
      border: none;
      border-radius: 6px;
      background: transparent;
      color: inherit;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      transition: background-color 0.16s ease, color 0.16s ease, transform 0.12s ease;
    }

    .media-floating-toolbar-btn:hover {
      background: var(--rte-media-toolbar-hover-bg);
      color: var(--rte-media-toolbar-hover-text);
    }

    .media-floating-toolbar-btn:active {
      background: var(--rte-media-toolbar-active-bg);
      transform: scale(0.96);
    }

    .media-floating-toolbar-btn.btn-remove:hover {
      background: var(--rte-media-toolbar-danger-hover-bg);
      color: var(--rte-media-toolbar-danger-hover-text);
    }

    .media-floating-toolbar-separator {
      width: 1px;
      height: 20px;
      margin: 0 2px;
      background: var(--rte-media-toolbar-separator);
    }
  `, document.head.appendChild(t);
}, Y = () => {
  const t = window.getSelection();
  if (!t || t.rangeCount === 0) return null;
  const e = t.anchorNode, r = e instanceof HTMLElement ? e : e == null ? void 0 : e.parentElement;
  return (r == null ? void 0 : r.closest(".rte-content, .editora-content")) || null;
}, Z = (t) => {
  if (t) return t;
  const e = Y();
  if (e) return e;
  if (R) return R;
  const r = document.activeElement;
  return r ? r.closest(".rte-content, .editora-content") || r : null;
}, G = (t) => {
  const e = Z(t);
  return e ? !!e.closest(F) : !1;
}, N = (t) => {
  X();
  const e = document.createElement("div");
  return e.className = "rte-media-overlay", G(t) && e.classList.add("rte-ui-theme-dark"), e;
}, B = (t = !1) => {
  const e = document.createElement("div");
  return e.className = t ? "rte-media-dialog rte-media-dialog-compact" : "rte-media-dialog", e.setAttribute("role", "dialog"), e.setAttribute("aria-modal", "true"), e;
}, W = (t, e) => {
  const r = window.getSelection();
  r && r.rangeCount > 0 && (D = r.getRangeAt(0).cloneRange());
  const a = N(e), o = B();
  let n = "upload", d = "", p = "", c = "", g = "";
  const h = () => {
    o.innerHTML = `
      <div class="rte-media-header">
        <h2 class="rte-media-title">Insert ${t === "image" ? "Image" : "Video"}</h2>
        <button class="close-btn rte-media-close-btn" type="button" aria-label="Close">×</button>
      </div>

      <div class="rte-media-tabs">
        <button class="tab-upload rte-media-tab ${n === "upload" ? "active" : ""}" type="button">Upload</button>
        <button class="tab-url rte-media-tab ${n === "url" ? "active" : ""}" type="button">URL</button>
      </div>

      <div class="rte-media-body">
        ${n === "upload" ? `
          <div id="upload-section">
            <div class="dropzone rte-media-dropzone">
              <div class="rte-media-dropzone-icon">📁</div>
              <p class="rte-media-dropzone-title">Drag and drop your ${t} here</p>
              <p class="rte-media-muted">or click to browse</p>
              <p class="rte-media-hint">Max file size: 50MB</p>
            </div>
            <input type="file" id="file-input" accept="${t === "image" ? "image/*" : "video/*"}" style="display: none;">
            <div id="upload-progress" class="rte-media-progress" style="display: none;">
              <div class="rte-media-progress-track">
                <div id="progress-bar" class="rte-media-progress-bar"></div>
              </div>
              <p id="progress-text" class="rte-media-progress-text">Uploading...</p>
            </div>
          </div>
        ` : `
          <div id="url-section">
            <div class="rte-media-field">
              <label class="rte-media-label">URL</label>
              <input type="text" id="url-input" class="rte-media-input" placeholder="https://example.com/${t}.${t === "image" ? "jpg" : "mp4"}" value="${d}">
            </div>
            ${t === "image" ? `
              <div class="rte-media-field">
                <label class="rte-media-label">Alt Text (for accessibility)</label>
                <input type="text" id="alt-input" class="rte-media-input" placeholder="Describe the image" value="${g}">
              </div>
            ` : ""}
            <div class="rte-media-grid">
              <div class="rte-media-field">
                <label class="rte-media-label">Width (px)</label>
                <input type="number" id="width-input" class="rte-media-input" placeholder="Auto" value="${p}">
              </div>
              <div class="rte-media-field">
                <label class="rte-media-label">Height (px)</label>
                <input type="number" id="height-input" class="rte-media-input" placeholder="Auto" value="${c}">
              </div>
            </div>
            ${d ? `
              <div class="rte-media-field">
                <label class="rte-media-label">Preview</label>
                <div class="rte-media-preview">
                  ${t === "image" ? `<img src="${d}" alt="Preview">` : `<video src="${d}" controls></video>`}
                </div>
              </div>
            ` : ""}
          </div>
        `}
      </div>

      <div class="rte-media-footer">
        <button class="cancel-btn rte-media-btn rte-media-btn-secondary" type="button">Cancel</button>
        <button id="insert-btn" class="rte-media-btn rte-media-btn-primary" type="button" ${!d && n === "url" ? "disabled" : ""}>Insert</button>
      </div>
    `;
  };
  h(), a.appendChild(o), document.body.appendChild(a);
  let w = () => {
  };
  const f = () => {
    w(), a.parentNode && a.parentNode.removeChild(a);
  };
  w = I(a, f);
  const y = () => {
    if (!d) return;
    const i = t === "image" ? document.createElement("img") : document.createElement("video");
    i.src = d, i.setAttribute("data-media-type", t), t === "image" && g && (i.alt = g), p && (i.style.width = `${p}px`, i.setAttribute("width", p)), c && (i.style.height = `${c}px`, i.setAttribute("height", c)), t === "video" && (i.controls = !0), !p && !c ? i.style.cssText = "max-width: 100%; height: auto; display: block; margin: 1em 0; cursor: pointer;" : i.style.cssText = `display: block; margin: 1em 0; cursor: pointer; ${p ? `width: ${p}px;` : "max-width: 100%;"} ${c ? `height: ${c}px;` : "height: auto;"}`, D && (D.deleteContents(), D.insertNode(i)), f();
  }, x = async (i) => {
    const u = o.querySelector("#upload-progress"), v = o.querySelector("#progress-bar"), L = o.querySelector("#progress-text");
    if (u && v && L) {
      u.style.display = "block";
      let E = 0;
      const b = setInterval(() => {
        E += Math.random() * 30, E > 90 && (E = 90), v.style.width = `${E}%`;
      }, 200);
      try {
        const k = new FileReader();
        k.onload = () => {
          clearInterval(b), v.style.width = "100%", L.textContent = "Upload complete", setTimeout(() => {
            d = k.result, n = "url", h(), l();
          }, 500);
        }, k.readAsDataURL(i);
      } catch (k) {
        clearInterval(b), L.textContent = "Upload failed";
      }
    }
  }, l = () => {
    const i = o.querySelector(".close-btn"), u = o.querySelector(".cancel-btn"), v = o.querySelector("#insert-btn"), L = o.querySelector(".tab-upload"), E = o.querySelector(".tab-url");
    if (i == null || i.addEventListener("click", f), u == null || u.addEventListener("click", f), v == null || v.addEventListener("click", y), L == null || L.addEventListener("click", () => {
      n = "upload", h(), l();
    }), E == null || E.addEventListener("click", () => {
      n = "url", h(), l();
    }), n === "upload") {
      const b = o.querySelector(".dropzone"), k = o.querySelector("#file-input");
      b == null || b.addEventListener("click", () => k == null ? void 0 : k.click()), b == null || b.addEventListener("dragover", (S) => {
        S.preventDefault(), b.classList.add("is-dragover");
      }), b == null || b.addEventListener("dragleave", () => {
        b.classList.remove("is-dragover");
      }), b == null || b.addEventListener("drop", (S) => {
        var T;
        S.preventDefault(), b.classList.remove("is-dragover");
        const $ = (T = S.dataTransfer) == null ? void 0 : T.files[0];
        $ && x($);
      }), k == null || k.addEventListener("change", (S) => {
        var T;
        const $ = (T = S.target.files) == null ? void 0 : T[0];
        $ && x($);
      });
    }
    if (n === "url") {
      const b = o.querySelector("#url-input"), k = o.querySelector("#alt-input"), S = o.querySelector("#width-input"), $ = o.querySelector("#height-input");
      b == null || b.addEventListener("input", () => {
        d = b.value, h(), l();
      }), k == null || k.addEventListener("input", () => {
        g = k.value;
      }), S == null || S.addEventListener("input", () => {
        p = S.value;
      }), $ == null || $.addEventListener("input", () => {
        c = $.value;
      });
    }
  };
  l(), a.addEventListener("click", (i) => {
    i.target === a && f();
  });
}, K = () => {
  ["nw", "ne", "sw", "se"].forEach((e) => {
    const r = document.createElement("div");
    r.className = `media-resize-handle-${e}`, r.style.cssText = `
      position: fixed;
      width: 10px;
      height: 10px;
      background: #007bff;
      border: 2px solid white;
      border-radius: 50%;
      cursor: ${e}-resize;
      z-index: 10001;
      display: none;
      box-shadow: 0 1px 3px rgba(0,0,0,0.3);
    `, r.setAttribute("data-position", e), document.body.appendChild(r), _.push(r);
  });
}, z = () => {
  if (!m) {
    _.forEach((r) => r.style.display = "none");
    return;
  }
  const t = m.getBoundingClientRect(), e = {
    nw: { x: t.left - 5, y: t.top - 5 },
    ne: { x: t.right - 5, y: t.top - 5 },
    sw: { x: t.left - 5, y: t.bottom - 5 },
    se: { x: t.right - 5, y: t.bottom - 5 }
  };
  _.forEach((r) => {
    const a = r.getAttribute("data-position"), o = e[a];
    r.style.left = `${o.x}px`, r.style.top = `${o.y}px`, r.style.display = "block";
  });
}, J = (t) => {
  const e = N(t), r = B(!0);
  r.innerHTML = `
    <div class="rte-media-header">
      <h2 class="rte-media-title">Edit Alt Text</h2>
      <button class="close-btn rte-media-close-btn" type="button" aria-label="Close">×</button>
    </div>
    <div class="rte-media-body">
      <label class="rte-media-label">Alternative Text (for accessibility)</label>
      <textarea id="alt-text-input" class="rte-media-textarea" placeholder="Describe the image for screen readers...">${t.alt || ""}</textarea>
      <p class="rte-media-helper">Good alt text is descriptive and concise. It helps users with visual impairments understand your content.</p>
    </div>
    <div class="rte-media-footer">
      <button class="cancel-btn rte-media-btn rte-media-btn-secondary" type="button">Cancel</button>
      <button class="save-btn rte-media-btn rte-media-btn-primary" type="button">Save</button>
    </div>
  `, e.appendChild(r), document.body.appendChild(e);
  const a = r.querySelector("#alt-text-input"), o = r.querySelector(".close-btn"), n = r.querySelector(".cancel-btn"), d = r.querySelector(".save-btn");
  let p = () => {
  };
  const c = () => {
    p(), e.parentNode && e.parentNode.removeChild(e);
  };
  p = I(e, c), o.addEventListener("click", c), n.addEventListener("click", c), e.addEventListener("click", (g) => {
    g.target === e && c();
  }), d.addEventListener("click", () => {
    t.alt = a.value, c();
  }), a.focus(), a.select();
}, Q = (t) => {
  const e = t.closest("a"), r = (e == null ? void 0 : e.getAttribute("href")) || "", a = (e == null ? void 0 : e.getAttribute("target")) || "_self", o = (e == null ? void 0 : e.getAttribute("title")) || "", n = N(t), d = B(!0);
  d.innerHTML = `
    <div class="rte-media-header">
      <h2 class="rte-media-title">${e ? "Edit Link" : "Add Link"}</h2>
      <button class="close-btn rte-media-close-btn" type="button" aria-label="Close">×</button>
    </div>
    <div class="rte-media-body">
      <div class="rte-media-field">
        <label class="rte-media-label">URL</label>
        <input id="link-url" type="url" class="rte-media-input" value="${r}" placeholder="https://example.com" />
      </div>
      <div class="rte-media-field">
        <label class="rte-media-label">Title (tooltip)</label>
        <input id="link-title" type="text" class="rte-media-input" value="${o}" placeholder="Optional tooltip text" />
      </div>
      <label class="rte-media-checkbox-label">
        <input id="link-target" type="checkbox" ${a === "_blank" ? "checked" : ""} />
        Open in new window/tab
      </label>
    </div>
    <div class="rte-media-footer rte-media-footer-spread">
      ${e ? '<button class="remove-link-btn rte-media-btn rte-media-btn-danger" type="button">Remove Link</button>' : '<span class="rte-media-spacer"></span>'}
      <div class="rte-media-actions">
        <button class="cancel-btn rte-media-btn rte-media-btn-secondary" type="button">Cancel</button>
        <button class="save-btn rte-media-btn rte-media-btn-primary" type="button">Save</button>
      </div>
    </div>
  `, n.appendChild(d), document.body.appendChild(n);
  const p = d.querySelector("#link-url"), c = d.querySelector("#link-title"), g = d.querySelector("#link-target"), h = d.querySelector(".close-btn"), w = d.querySelector(".cancel-btn"), f = d.querySelector(".save-btn"), y = d.querySelector(".remove-link-btn");
  let x = () => {
  };
  const l = () => {
    x(), n.parentNode && n.parentNode.removeChild(n);
  };
  x = I(n, l), h.addEventListener("click", l), w.addEventListener("click", l), n.addEventListener("click", (i) => {
    i.target === n && l();
  }), f.addEventListener("click", () => {
    const i = p.value.trim();
    if (i) {
      const u = i.startsWith("http") ? i : `https://${i}`;
      if (e)
        e.setAttribute("href", u), e.setAttribute("target", g.checked ? "_blank" : "_self"), g.checked ? e.setAttribute("rel", "noopener noreferrer") : e.removeAttribute("rel"), c.value.trim() ? e.setAttribute("title", c.value.trim()) : e.removeAttribute("title");
      else {
        const v = document.createElement("a");
        v.href = u, v.target = g.checked ? "_blank" : "_self", g.checked && (v.rel = "noopener noreferrer"), c.value.trim() && (v.title = c.value.trim()), t.replaceWith(v), v.appendChild(t);
      }
      l(), s && m && V(m);
    }
  }), y == null || y.addEventListener("click", () => {
    e && confirm("Remove link from this media?") && (e.replaceWith(t), l(), s && m && V(m));
  }), p.focus();
}, ee = (t) => {
  const e = N(t), r = B();
  let a = "url", o = t.src;
  const n = () => {
    r.innerHTML = `
      <div class="rte-media-header">
        <h2 class="rte-media-title">Replace Image</h2>
        <button class="close-btn rte-media-close-btn" type="button" aria-label="Close">×</button>
      </div>

      <div class="rte-media-tabs">
        <button class="tab-upload rte-media-tab ${a === "upload" ? "active" : ""}" type="button">Upload</button>
        <button class="tab-url rte-media-tab ${a === "url" ? "active" : ""}" type="button">URL</button>
      </div>

      <div class="rte-media-body">
        ${a === "upload" ? `
          <div id="upload-section">
            <div class="dropzone rte-media-dropzone">
              <div class="rte-media-dropzone-icon">📁</div>
              <p class="rte-media-dropzone-title">Drag and drop your image here</p>
              <p class="rte-media-muted">or click to browse</p>
            </div>
            <input type="file" id="file-input" accept="image/*" style="display: none;">
            <div id="upload-progress" class="rte-media-progress" style="display: none;">
              <div class="rte-media-progress-track">
                <div id="progress-bar" class="rte-media-progress-bar"></div>
              </div>
              <p id="progress-text" class="rte-media-progress-text">Uploading...</p>
            </div>
          </div>
        ` : `
          <div id="url-section">
            <div class="rte-media-field">
              <label class="rte-media-label">Image URL</label>
              <input type="text" id="url-input" class="rte-media-input" placeholder="https://example.com/image.jpg" value="${o}">
            </div>
            ${o ? `
              <div class="rte-media-field">
                <label class="rte-media-label">Preview</label>
                <div class="rte-media-preview">
                  <img src="${o}" alt="Preview" onerror="this.parentElement.innerHTML='<p class=&quot;rte-media-muted&quot;>Failed to load image</p>'">
                </div>
              </div>
            ` : ""}
          </div>
        `}
      </div>

      <div class="rte-media-footer">
        <button class="cancel-btn rte-media-btn rte-media-btn-secondary" type="button">Cancel</button>
        <button id="replace-btn" class="rte-media-btn rte-media-btn-primary" type="button" ${!o && a === "url" ? "disabled" : ""}>Replace</button>
      </div>
    `;
  };
  n(), e.appendChild(r), document.body.appendChild(e);
  let d = () => {
  };
  const p = () => {
    d(), e.parentNode && e.parentNode.removeChild(e);
  };
  d = I(e, p);
  const c = () => {
    o && (t.src = o, p());
  }, g = async (w) => {
    const f = r.querySelector("#upload-progress"), y = r.querySelector("#progress-bar"), x = r.querySelector("#progress-text");
    if (f && y && x) {
      f.style.display = "block";
      let l = 0;
      const i = setInterval(() => {
        l += Math.random() * 30, l > 90 && (l = 90), y.style.width = `${l}%`;
      }, 200);
      try {
        const u = new FileReader();
        u.onload = () => {
          clearInterval(i), y.style.width = "100%", x.textContent = "Upload complete", setTimeout(() => {
            o = u.result, a = "url", n(), h();
          }, 500);
        }, u.readAsDataURL(w);
      } catch (u) {
        clearInterval(i), x.textContent = "Upload failed";
      }
    }
  }, h = () => {
    const w = r.querySelector(".close-btn"), f = r.querySelector(".cancel-btn"), y = r.querySelector("#replace-btn"), x = r.querySelector(".tab-upload"), l = r.querySelector(".tab-url");
    if (w == null || w.addEventListener("click", p), f == null || f.addEventListener("click", p), y == null || y.addEventListener("click", c), x == null || x.addEventListener("click", () => {
      a = "upload", n(), h();
    }), l == null || l.addEventListener("click", () => {
      a = "url", n(), h();
    }), a === "upload") {
      const i = r.querySelector(".dropzone"), u = r.querySelector("#file-input");
      i == null || i.addEventListener("click", () => u == null ? void 0 : u.click()), i == null || i.addEventListener("dragover", (v) => {
        v.preventDefault(), i.classList.add("is-dragover");
      }), i == null || i.addEventListener("dragleave", () => {
        i.classList.remove("is-dragover");
      }), i == null || i.addEventListener("drop", (v) => {
        var E;
        v.preventDefault(), i.classList.remove("is-dragover");
        const L = (E = v.dataTransfer) == null ? void 0 : E.files[0];
        L && g(L);
      }), u == null || u.addEventListener("change", (v) => {
        var E;
        const L = (E = v.target.files) == null ? void 0 : E[0];
        L && g(L);
      });
    }
    if (a === "url") {
      const i = r.querySelector("#url-input");
      i == null || i.addEventListener("input", () => {
        o = i.value, n(), h();
      });
    }
  };
  h(), e.addEventListener("click", (w) => {
    w.target === e && p();
  });
}, C = () => {
  if (!s || !m) return;
  const t = s.offsetHeight || 40, e = m.offsetTop, r = m.offsetLeft, a = m.offsetWidth, o = e - t - 8, n = r + a / 2 - (s.offsetWidth || 120) / 2;
  s.style.top = `${o}px`, s.style.left = `${n}px`, setTimeout(() => {
    s && (s.style.display = "flex");
  }, 100);
}, V = (t) => {
  var p, c, g, h, w, f, y, x;
  s && (s._cleanup && s._cleanup(), s.remove());
  const e = t.parentElement;
  if (e) {
    const l = e.style.position;
    (!l || l === "static") && (e.style.position = "relative", e._originalPosition = l), s = document.createElement("div"), s.className = "media-floating-toolbar", G(t) && s.classList.add("rte-ui-theme-dark"), e.insertBefore(s, e.firstChild), C();
  }
  const r = s;
  if (!r) return;
  const a = t.tagName === "IMG", o = t.closest("a");
  r.innerHTML = `
    <button class="media-floating-toolbar-btn btn-align-left" title="Align Left" type="button">
      <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><line x1="17" y1="10" x2="3" y2="10"></line><line x1="21" y1="6" x2="3" y2="6"></line><line x1="21" y1="14" x2="3" y2="14"></line><line x1="17" y1="18" x2="3" y2="18"></line></svg>
    </button>
    <button class="media-floating-toolbar-btn btn-align-center" title="Align Center" type="button">
      <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><line x1="18" y1="10" x2="6" y2="10"></line><line x1="21" y1="6" x2="3" y2="6"></line><line x1="21" y1="14" x2="3" y2="14"></line><line x1="18" y1="18" x2="6" y2="18"></line></svg>
    </button>
    <button class="media-floating-toolbar-btn btn-align-right" title="Align Right" type="button">
      <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><line x1="21" y1="10" x2="7" y2="10"></line><line x1="21" y1="6" x2="3" y2="6"></line><line x1="21" y1="14" x2="3" y2="14"></line><line x1="21" y1="18" x2="7" y2="18"></line></svg>
    </button>
    <div class="media-floating-toolbar-separator" aria-hidden="true"></div>
    ${a ? `
    <button class="media-floating-toolbar-btn btn-alt" title="Edit Alt Text" type="button">
      <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M17 3a2.828 2.828 0 1 1 4 4L7.5 20.5 2 22l1.5-5.5L17 3z"></path></svg>
    </button>` : ""}
    <button class="media-floating-toolbar-btn btn-link" title="${o ? "Edit/Remove Link" : "Add Link"}" type="button">
      <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg>
    </button>
    ${a ? `
    <button class="media-floating-toolbar-btn btn-replace" title="Replace Image" type="button">
      <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect><circle cx="8.5" cy="8.5" r="1.5"></circle><polyline points="21 15 16 10 5 21"></polyline></svg>
    </button>` : ""}
    <div class="media-floating-toolbar-separator" aria-hidden="true"></div>
    <button class="media-floating-toolbar-btn btn-remove" title="Remove" type="button">
      <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="3 6 5 6 21 6"></polyline><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path></svg>
    </button>
  `, setTimeout(() => {
    C();
  }, 0);
  const n = () => C();
  let d = t.parentElement;
  for (; d; )
    d.addEventListener("scroll", n), d = d.parentElement;
  window.addEventListener("scroll", n), window.addEventListener("resize", n), r._cleanup = () => {
    let l = t.parentElement;
    for (; l; )
      l.removeEventListener("scroll", n), l = l.parentElement;
    window.removeEventListener("scroll", n), window.removeEventListener("resize", n);
  }, (p = r.querySelector(".btn-align-left")) == null || p.addEventListener("click", () => {
    t.style.display = "block", t.style.marginLeft = "0", t.style.marginRight = "auto", C();
  }), (c = r.querySelector(".btn-align-center")) == null || c.addEventListener("click", () => {
    t.style.display = "block", t.style.marginLeft = "auto", t.style.marginRight = "auto", C();
  }), (g = r.querySelector(".btn-align-right")) == null || g.addEventListener("click", () => {
    t.style.display = "block", t.style.marginLeft = "auto", t.style.marginRight = "0", C();
  }), (h = r.querySelector(".btn-alt")) == null || h.addEventListener("click", () => {
    t.tagName === "IMG" && J(t);
  }), (w = r.querySelector(".btn-link")) == null || w.addEventListener("click", () => {
    Q(t);
  }), (f = r.querySelector(".btn-replace")) == null || f.addEventListener("click", () => {
    t.tagName === "IMG" && ee(t);
  }), (y = r.querySelector(".btn-resize")) == null || y.addEventListener("click", () => {
    const l = prompt("Enter width in pixels:", String(t.width || t.offsetWidth));
    if (l && !isNaN(parseInt(l))) {
      const i = parseInt(l);
      t.style.width = `${i}px`, t.setAttribute("width", String(i)), z(), C();
    }
  }), (x = r.querySelector(".btn-remove")) == null || x.addEventListener("click", () => {
    confirm("Remove this media?") && (t.remove(), s && (s._cleanup && s._cleanup(), s.remove(), s = null), m = null, z());
  }), r._cleanup = () => {
    window.removeEventListener("scroll", C), window.removeEventListener("resize", C);
  };
}, O = (t) => {
  R = t || null, K(), document.addEventListener("click", (e) => {
    const r = e.target;
    if (r.tagName === "IMG" || r.tagName === "VIDEO") {
      const a = r;
      let o = !1;
      if (R ? o = R.contains(a) : o = !!a.closest('[contenteditable="true"]'), o) {
        e.preventDefault(), e.stopPropagation(), m = a, m.style.display = "block", V(a), z();
        return;
      }
    }
    if (!r.closest(".btn-link, .btn-resize, .btn-remove") && s && !r.closest("button")) {
      if (s._cleanup && s._cleanup(), s.remove(), s = null, m && m.parentElement) {
        const a = m.parentElement;
        a._originalPosition !== void 0 && (a.style.position = a._originalPosition, delete a._originalPosition);
      }
      m = null, z();
    }
  }), _.forEach((e) => {
    e.addEventListener("mousedown", (r) => {
      if (!m) return;
      r.preventDefault(), r.stopPropagation(), H = !0, A = e.getAttribute("data-position"), U = r.clientX, j = r.clientY;
      const a = m.getBoundingClientRect();
      q = a.width, M = a.height, P = q / M, document.body.style.userSelect = "none", document.body.style.cursor = `${A}-resize`;
    });
  }), document.addEventListener("mousemove", (e) => {
    if (!H || !m || !A) return;
    const r = e.clientX - U, a = e.clientY - j;
    let o = q, n = M;
    switch (A) {
      case "se":
        o = q + r, n = M + a;
        break;
      case "sw":
        o = q - r, n = M + a;
        break;
      case "ne":
        o = q + r, n = M - a;
        break;
      case "nw":
        o = q - r, n = M - a;
        break;
    }
    Math.abs(r) > Math.abs(a) ? n = o / P : o = n * P, o = Math.max(50, o), n = Math.max(50, n), m.style.width = `${o}px`, m.style.height = `${n}px`, m.setAttribute("width", String(Math.round(o))), m.setAttribute("height", String(Math.round(n))), z(), C();
  }), document.addEventListener("mouseup", () => {
    H && (H = !1, A = null, document.body.style.userSelect = "", document.body.style.cursor = "");
  }), window.addEventListener("scroll", z), window.addEventListener("resize", z);
};
typeof window != "undefined" && !window.__mediaManagerInitialized && (window.__mediaManagerInitialized = !0, O());
const te = () => ({
  name: "image",
  initialize: (t) => {
    const e = t == null ? void 0 : t.editorElement;
    O(e);
  },
  toolbar: [
    {
      label: "Image",
      command: "insertImage",
      icon: '<svg width="24px" height="24px" viewBox="0 0 32 32" enable-background="new 0 0 32 32"><g><rect fill="none" height="22" stroke="#000000" stroke-linejoin="round" stroke-miterlimit="10" stroke-width="2" width="30" x="1" y="5"></rect><polygon fill="none" points="31,27 21,17 11,27" stroke="#000000" stroke-linejoin="round" stroke-miterlimit="10" stroke-width="2"></polygon><polygon fill="none" points="18,20 9,11 1,19 1,27 11,27" stroke="#000000" stroke-linejoin="round" stroke-miterlimit="10" stroke-width="2"></polygon><circle cx="19" cy="11" fill="none" r="2" stroke="#000000" stroke-linejoin="round" stroke-miterlimit="10" stroke-width="2"></circle></g></svg>'
    },
    {
      label: "Video",
      command: "insertVideo",
      icon: '<svg width="24" height="24" focusable="false"><path d="M4 3h16c.6 0 1 .4 1 1v16c0 .6-.4 1-1 1H4a1 1 0 0 1-1-1V4c0-.6.4-1 1-1Zm1 2v14h14V5H5Zm4.8 2.6 5.6 4a.5.5 0 0 1 0 .8l-5.6 4A.5.5 0 0 1 9 16V8a.5.5 0 0 1 .8-.4Z" fill-rule="nonzero"></path></svg>'
    }
  ],
  commands: {
    insertImage: (t, e) => {
      const r = (e == null ? void 0 : e.contentElement) instanceof HTMLElement ? e.contentElement : void 0;
      return W("image", r), !0;
    },
    insertVideo: (t, e) => {
      const r = (e == null ? void 0 : e.contentElement) instanceof HTMLElement ? e.contentElement : void 0;
      return W("video", r), !0;
    }
  },
  keymap: {
    "Mod-Shift-i": "insertImage"
  }
});
export {
  te as MediaManagerPlugin
};
//# sourceMappingURL=MediaManagerPlugin.native-eOsTJFpw.mjs.map
