"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const C=new WeakMap;let x=null,S=!1,k=0;const E="User";function v(t){return k+=1,`${t}-${Date.now()}-${k}`}function y(t){if(!t)return null;const e=t instanceof Element?t:t.parentElement;return e?e.closest("[data-editora-editor]")||e.closest(".rte-editor")||e.closest(".editora-editor"):null}function A(t){const e=t?.editorElement||t?.contentElement||null;if(!(e instanceof HTMLElement))return;const r=e.closest("[data-editora-editor], .rte-editor, .editora-editor, editora-editor")||(e.matches("[data-editora-editor], .rte-editor, .editora-editor, editora-editor")?e:null);r&&(x=r)}function w(t,e){return e.contains(t.commonAncestorContainer)}function B(){if(typeof window<"u"){const o=window.__editoraCommandEditorRoot;if(o instanceof HTMLElement){const a=o.closest("[data-editora-editor], .rte-editor, .editora-editor, editora-editor")||(o.matches("[data-editora-editor], .rte-editor, .editora-editor, editora-editor")?o:null);if(a)return x=a,window.__editoraCommandEditorRoot=null,a}}const t=document.activeElement,e=t?y(t):null;if(e)return e;const r=window.getSelection();if(r&&r.rangeCount>0){const o=y(r.getRangeAt(0).commonAncestorContainer);if(o)return o}return x}function H(t){const e=C.get(t);if(e)return e;const r={root:t,comments:new Map,panelVisible:!1,panelElement:null,expandedComments:new Set,replyTexts:{},savedSelection:null,newCommentText:"",selectionChangeListener:null};return C.set(t,r),r}function u(){const t=B();return t?(x=t,H(t)):null}function R(t){const e=window.getSelection();if(!e||e.rangeCount===0||e.isCollapsed)return null;const r=e.getRangeAt(0);return w(r,t)?r.cloneRange():null}function I(t,e){if(!e.anchorId)return;const r=t.root.querySelector(`#${e.anchorId}`);r&&r.classList.toggle("rte-comment-anchor-resolved",e.resolved)}function h(t,e,r){const o=t.comments.get(e);if(!o||!o.anchorId)return;const a=t.root.querySelector(`#${o.anchorId}`);a&&a.classList.toggle("highlighted",r)}function $(t,e,r){e.onclick=o=>{o.preventDefault(),o.stopPropagation(),x=t.root,t.expandedComments.add(r),g(t,!0),m(t)}}function q(t){const e=t.parentNode;if(e){for(;t.firstChild;)e.insertBefore(t.firstChild,t);t.remove()}}function g(t,e){if(t.panelVisible=e,e){Z(),N(t),t.root.setAttribute("data-rte-comments-open","true"),t.panelElement&&(t.panelElement.classList.add("is-open"),t.panelElement.setAttribute("aria-hidden","false")),z(t);return}t.root.removeAttribute("data-rte-comments-open"),V(t),t.panelElement&&(t.panelElement.remove(),t.panelElement=null)}function N(t){if(t.panelElement)return;const e=document.createElement("aside");e.className="rte-comments-panel",e.setAttribute("role","complementary"),e.setAttribute("aria-label","Comments"),e.setAttribute("aria-hidden","true"),window.getComputedStyle(t.root).position==="static"&&(t.root.style.position="relative"),t.root.appendChild(e),t.panelElement=e}function z(t){t.selectionChangeListener||(t.selectionChangeListener=()=>{const e=window.getSelection();if(!e||e.rangeCount===0||e.isCollapsed)return;const r=e.getRangeAt(0);w(r,t.root)&&(t.savedSelection=r.cloneRange(),x=t.root)},document.addEventListener("selectionchange",t.selectionChangeListener))}function V(t){t.selectionChangeListener&&(document.removeEventListener("selectionchange",t.selectionChangeListener),t.selectionChangeListener=null)}function M(t){return Array.from(t.comments.values()).sort((e,r)=>r.createdAt.localeCompare(e.createdAt))}function L(t){return new Date(t).toLocaleString()}function D(t,e){const r=t.expandedComments.has(e.id),o=document.createElement("article");o.className=`rte-comment-item${e.resolved?" resolved":""}`,o.innerHTML=`
    <header class="rte-comment-header">
      <div class="rte-comment-meta">
        <strong class="rte-comment-author">${e.author}</strong>
        <time class="rte-comment-date">${L(e.createdAt)}</time>
      </div>
      <button class="rte-comment-expand" type="button" aria-label="Toggle details">
        ${r?"▾":"▸"}
      </button>
    </header>
    <div class="rte-comment-text"></div>
    ${e.selectedText?`<blockquote class="rte-comment-selection">${e.selectedText}</blockquote>`:""}
    <section class="rte-comment-expanded${r?" show":""}"></section>
  `;const a=o.querySelector(".rte-comment-text");a&&(a.textContent=e.text),o.querySelector(".rte-comment-expand")?.addEventListener("click",()=>{t.expandedComments.has(e.id)?t.expandedComments.delete(e.id):t.expandedComments.add(e.id),m(t)});const p=o.querySelector(".rte-comment-expanded");if(p&&r){if(e.replies.length>0){const n=document.createElement("div");n.className="rte-comment-replies",e.replies.forEach(s=>{const l=document.createElement("div");l.className="rte-comment-reply",l.innerHTML=`
          <div class="rte-comment-reply-header">
            <strong>${s.author}</strong>
            <time>${L(s.createdAt)}</time>
          </div>
          <div class="rte-comment-reply-text"></div>
        `;const f=l.querySelector(".rte-comment-reply-text");f&&(f.textContent=s.text),n.appendChild(l)}),p.appendChild(n)}if(!e.resolved){const n=document.createElement("div");n.className="rte-comment-reply-composer",n.innerHTML=`
        <textarea class="rte-comment-reply-textarea" rows="2" placeholder="Reply..."></textarea>
        <button type="button" class="rte-comment-btn primary">Reply</button>
      `;const s=n.querySelector(".rte-comment-reply-textarea"),l=n.querySelector(".rte-comment-btn.primary");if(s&&l){s.value=t.replyTexts[e.id]||"";const f=()=>{const b=!!s.value.trim();l.disabled=!b};f(),s.addEventListener("input",()=>{t.replyTexts[e.id]=s.value,f()}),l.addEventListener("click",()=>{const b=s.value.trim();b&&(F(e.id,E,b),t.replyTexts[e.id]="",m(t))})}p.appendChild(n)}const c=document.createElement("div");if(c.className="rte-comment-actions",e.anchorId){const n=document.createElement("button");n.type="button",n.className="rte-comment-btn ghost",n.textContent="Jump to text",n.onclick=()=>{const s=t.root.querySelector(`#${e.anchorId}`);s&&(s.scrollIntoView({behavior:"smooth",block:"center",inline:"nearest"}),h(t,e.id,!0),window.setTimeout(()=>h(t,e.id,!1),1200))},c.appendChild(n)}if(e.resolved){const n=document.createElement("button");n.type="button",n.className="rte-comment-btn ghost",n.textContent="Reopen",n.onclick=()=>_(e.id),c.appendChild(n)}else{const n=document.createElement("button");n.type="button",n.className="rte-comment-btn success",n.textContent="Resolve",n.onclick=()=>P(e.id,E),c.appendChild(n)}const i=document.createElement("button");i.type="button",i.className="rte-comment-btn danger",i.textContent="Delete",i.onclick=()=>j(e.id),c.appendChild(i),p.appendChild(c)}return o.addEventListener("mouseenter",()=>h(t,e.id,!0)),o.addEventListener("mouseleave",()=>h(t,e.id,!1)),o}function m(t){const e=t||u();if(!e?.panelElement)return;const r=M(e);e.panelElement.innerHTML=`
    <div class="rte-comments-header">
      <div>
        <h3>Comments (${r.length})</h3>
        <p>Select text and add comments, or add a general note.</p>
      </div>
      <button class="rte-comments-close" type="button" aria-label="Close comments panel">✕</button>
    </div>
    <div class="rte-comments-composer">
      <textarea class="new-comment-textarea" rows="3" placeholder="Add a comment..."></textarea>
      <div class="rte-comments-composer-actions">
        <button class="rte-comment-btn primary add-comment-btn" type="button">Add Comment</button>
      </div>
    </div>
    ${r.length===0?'<div class="rte-comments-empty">No comments yet.</div>':'<div class="rte-comments-list"></div>'}
  `,e.panelElement.querySelector(".rte-comments-close")?.addEventListener("click",()=>{g(e,!1)});const a=e.panelElement.querySelector(".new-comment-textarea"),d=e.panelElement.querySelector(".add-comment-btn");if(a&&d){a.value=e.newCommentText;const c=()=>{const i=!!a.value.trim();d.disabled=!i};c(),a.addEventListener("input",()=>{e.newCommentText=a.value,c()}),a.addEventListener("keydown",i=>{(i.ctrlKey||i.metaKey)&&i.key==="Enter"&&(i.preventDefault(),d.click())}),d.addEventListener("click",()=>{const i=a.value.trim();if(!i)return;const n=!!e.savedSelection;O(E,i,!n),e.newCommentText="",m(e)})}const p=e.panelElement.querySelector(".rte-comments-list");p&&r.forEach(c=>{p.appendChild(D(e,c))})}function T(){S||(S=!0,document.addEventListener("focusin",t=>{const e=y(t.target);e&&(x=e)}),document.addEventListener("selectionchange",()=>{const t=window.getSelection();if(!t||t.rangeCount===0)return;const e=t.getRangeAt(0),r=y(e.commonAncestorContainer);if(!r)return;x=r;const o=C.get(r);!o||t.isCollapsed||w(e,r)&&(o.savedSelection=e.cloneRange())}),document.addEventListener("keydown",t=>{if(t.key!=="Escape")return;const e=u();!e||!e.panelVisible||(t.preventDefault(),t.stopPropagation(),g(e,!1))},!0))}function O(t,e,r=!1){const o=u();if(!o)return"";const a=e.trim();if(!a)return"";if(r){const l=v("comment");return o.comments.set(l,{id:l,anchorId:"",selectedText:"",author:t,text:a,createdAt:new Date().toISOString(),resolved:!1,replies:[]}),m(o),l}const d=o.savedSelection||R(o.root);if(!d||!w(d,o.root))return"";const p=d.toString().trim();if(!p)return"";const c=v("comment"),i=v("comment-anchor"),n=document.createElement("span");n.id=i,n.className="rte-comment-anchor",n.setAttribute("data-comment-id",c),n.setAttribute("title","Commented text");try{const l=d.cloneRange(),f=l.extractContents();if(!f.textContent?.trim())return"";n.appendChild(f),l.insertNode(n)}catch{return""}return $(o,n,c),o.comments.set(c,{id:c,anchorId:i,selectedText:p,author:t,text:a,createdAt:new Date().toISOString(),resolved:!1,replies:[]}),o.savedSelection=null,window.getSelection()?.removeAllRanges(),m(o),c}function P(t,e){const r=u();if(!r)return;const o=r.comments.get(t);o&&(o.resolved=!0,o.resolvedBy=e,o.resolvedAt=new Date().toISOString(),I(r,o),m(r))}function _(t){const e=u();if(!e)return;const r=e.comments.get(t);r&&(r.resolved=!1,r.resolvedBy=void 0,r.resolvedAt=void 0,I(e,r),m(e))}function j(t){const e=u();if(!e)return;const r=e.comments.get(t);if(r){if(r.anchorId){const o=e.root.querySelector(`#${r.anchorId}`);o&&q(o)}e.comments.delete(t),e.expandedComments.delete(t),delete e.replyTexts[t],m(e)}}function F(t,e,r){const o=u();if(!o)return;const a=o.comments.get(t);if(!a)return;const d=r.trim();d&&(a.replies.push({id:v("reply"),author:e,text:d,createdAt:new Date().toISOString()}),m(o))}function Z(){if(document.getElementById("rte-comments-panel-styles"))return;const t=document.createElement("style");t.id="rte-comments-panel-styles",t.textContent=`
    .rte-comments-panel {
      --rte-comments-panel-width: min(360px, 42vw);
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      width: var(--rte-comments-panel-width);
      display: flex;
      flex-direction: column;
      background: var(--rte-color-bg-primary, #ffffff);
      color: var(--rte-color-text-primary, #111827);
      border-left: 1px solid var(--rte-color-border, #d1d5db);
      box-shadow: -12px 0 28px rgba(15, 23, 42, 0.2);
      transform: translateX(100%);
      opacity: 0;
      pointer-events: none;
      transition: transform 180ms ease, opacity 180ms ease;
      z-index: 55;
    }

    .rte-comments-panel.is-open {
      transform: translateX(0);
      opacity: 1;
      pointer-events: auto;
    }

    [data-rte-comments-open="true"] :is(.rte-toolbar, .editora-toolbar, .rte-content, .editora-content, .editora-statusbar, .editora-statusbar-container) {
      margin-right: var(--rte-comments-panel-width);
      transition: margin-right 180ms ease;
    }

    .rte-comments-header {
      display: flex;
      justify-content: space-between;
      align-items: flex-start;
      gap: 12px;
      padding: 14px 14px 12px;
      border-bottom: 1px solid var(--rte-color-border-light, #e5e7eb);
      background: var(--rte-color-bg-secondary, #f8fafc);
    }

    .rte-comments-header h3 {
      margin: 0;
      font-size: 15px;
      line-height: 1.2;
    }

    .rte-comments-header p {
      margin: 6px 0 0;
      font-size: 12px;
      color: var(--rte-color-text-muted, #64748b);
    }

    .rte-comments-close {
      border: 1px solid var(--rte-color-border, #d1d5db);
      background: transparent;
      color: var(--rte-color-text-secondary, #475569);
      border-radius: 6px;
      width: 30px;
      height: 30px;
      cursor: pointer;
      font-size: 16px;
      line-height: 1;
    }

    .rte-comments-close:hover {
      background: var(--rte-color-bg-hover, #f1f5f9);
      color: var(--rte-color-text-primary, #0f172a);
    }

    .rte-comments-composer {
      display: flex;
      flex-direction: column;
      gap: 8px;
      padding: 12px 14px;
      border-bottom: 1px solid var(--rte-color-border-light, #e5e7eb);
      background: var(--rte-color-bg-secondary, #f8fafc);
    }

    .new-comment-textarea,
    .rte-comment-reply-textarea {
      width: 100%;
      resize: vertical;
      min-height: 62px;
      font-family: inherit;
      font-size: 13px;
      line-height: 1.35;
      border: 1px solid var(--rte-color-border, #d1d5db);
      border-radius: 8px;
      background: var(--rte-color-bg-primary, #ffffff);
      color: var(--rte-color-text-primary, #0f172a);
      padding: 8px 10px;
      outline: none;
    }

    .new-comment-textarea:focus,
    .rte-comment-reply-textarea:focus {
      border-color: var(--rte-color-border-focus, #2563eb);
      box-shadow: 0 0 0 2px color-mix(in srgb, var(--rte-color-border-focus, #2563eb) 20%, transparent);
    }

    .rte-comments-composer-actions {
      display: flex;
      justify-content: flex-end;
    }

    .rte-comments-list {
      flex: 1;
      overflow: auto;
      padding: 12px;
      display: flex;
      flex-direction: column;
      gap: 10px;
    }

    .rte-comments-empty {
      padding: 18px 14px;
      color: var(--rte-color-text-muted, #64748b);
      font-size: 13px;
    }

    .rte-comment-item {
      border: 1px solid var(--rte-color-border-light, #e5e7eb);
      border-radius: 10px;
      background: var(--rte-color-bg-primary, #ffffff);
      padding: 10px;
    }

    .rte-comment-item.resolved {
      opacity: 0.74;
    }

    .rte-comment-header {
      display: flex;
      align-items: flex-start;
      justify-content: space-between;
      gap: 8px;
    }

    .rte-comment-meta {
      display: flex;
      flex-direction: column;
      gap: 2px;
    }

    .rte-comment-author {
      font-size: 13px;
      color: var(--rte-color-text-primary, #0f172a);
    }

    .rte-comment-date {
      font-size: 11px;
      color: var(--rte-color-text-muted, #64748b);
    }

    .rte-comment-expand {
      border: none;
      background: transparent;
      cursor: pointer;
      color: var(--rte-color-text-secondary, #475569);
      font-size: 16px;
      line-height: 1;
      padding: 0;
    }

    .rte-comment-text {
      margin-top: 8px;
      font-size: 13px;
      line-height: 1.45;
      color: var(--rte-color-text-primary, #0f172a);
      white-space: pre-wrap;
      word-break: break-word;
    }

    .rte-comment-selection {
      margin: 8px 0 0;
      border-left: 3px solid var(--rte-color-primary, #2563eb);
      background: var(--rte-color-bg-secondary, #f8fafc);
      color: var(--rte-color-text-secondary, #475569);
      padding: 6px 8px;
      border-radius: 6px;
      font-size: 12px;
      font-style: italic;
      white-space: pre-wrap;
      word-break: break-word;
    }

    .rte-comment-expanded {
      display: none;
      margin-top: 10px;
      padding-top: 10px;
      border-top: 1px solid var(--rte-color-border-light, #e5e7eb);
    }

    .rte-comment-expanded.show {
      display: block;
    }

    .rte-comment-replies {
      display: flex;
      flex-direction: column;
      gap: 8px;
      margin-bottom: 10px;
    }

    .rte-comment-reply {
      border: 1px solid var(--rte-color-border-light, #e5e7eb);
      border-radius: 8px;
      padding: 8px;
      background: var(--rte-color-bg-secondary, #f8fafc);
    }

    .rte-comment-reply-header {
      display: flex;
      justify-content: space-between;
      gap: 8px;
      margin-bottom: 4px;
      font-size: 11px;
      color: var(--rte-color-text-muted, #64748b);
    }

    .rte-comment-reply-text {
      font-size: 12px;
      color: var(--rte-color-text-secondary, #334155);
      white-space: pre-wrap;
      word-break: break-word;
    }

    .rte-comment-reply-composer {
      display: flex;
      flex-direction: column;
      gap: 8px;
      margin-bottom: 10px;
    }

    .rte-comment-actions {
      display: flex;
      justify-content: flex-end;
      flex-wrap: wrap;
      gap: 6px;
    }

    .rte-comment-btn {
      border: 1px solid var(--rte-color-border, #d1d5db);
      border-radius: 7px;
      padding: 5px 9px;
      font-size: 12px;
      cursor: pointer;
      background: var(--rte-color-bg-primary, #ffffff);
      color: var(--rte-color-text-secondary, #334155);
    }

    .rte-comment-btn:hover {
      background: var(--rte-color-bg-hover, #f1f5f9);
      color: var(--rte-color-text-primary, #0f172a);
    }

    .rte-comment-btn:disabled {
      opacity: 0.5;
      cursor: not-allowed;
    }

    .rte-comment-btn.primary {
      border-color: var(--rte-color-primary, #2563eb);
      background: var(--rte-color-primary, #2563eb);
      color: var(--rte-color-text-inverse, #ffffff);
    }

    .rte-comment-btn.primary:hover {
      background: var(--rte-color-primary-hover, #1d4ed8);
      border-color: var(--rte-color-primary-hover, #1d4ed8);
      color: var(--rte-color-text-inverse, #ffffff);
    }

    .rte-comment-btn.success {
      border-color: color-mix(in srgb, var(--rte-color-success, #15803d) 60%, transparent);
      color: var(--rte-color-success, #15803d);
    }

    .rte-comment-btn.danger {
      border-color: color-mix(in srgb, var(--rte-color-danger, #b91c1c) 60%, transparent);
      color: var(--rte-color-danger, #b91c1c);
    }

    .rte-comment-anchor {
      background: rgba(250, 204, 21, 0.34);
      border-bottom: 2px solid rgba(217, 119, 6, 0.75);
      border-radius: 2px;
      cursor: pointer;
      transition: outline-color 120ms ease, box-shadow 120ms ease;
    }

    .rte-comment-anchor.rte-comment-anchor-resolved {
      background: rgba(148, 163, 184, 0.2);
      border-bottom-color: rgba(100, 116, 139, 0.6);
    }

    .rte-comment-anchor.highlighted {
      outline: 2px solid var(--rte-color-primary, #2563eb);
      outline-offset: 2px;
    }

    :is([theme="dark"], [data-theme="dark"], .dark, .editora-theme-dark) .rte-comments-panel {
      box-shadow: -14px 0 30px rgba(0, 0, 0, 0.45);
    }

    @media (max-width: 900px) {
      .rte-comments-panel {
        --rte-comments-panel-width: min(420px, 100%);
      }

      [data-rte-comments-open="true"] :is(.rte-toolbar, .editora-toolbar, .rte-content, .editora-content, .editora-statusbar, .editora-statusbar-container) {
        margin-right: 0;
      }
    }
  `,document.head.appendChild(t)}const K=()=>({name:"comments",toolbar:[{label:"Add Comment",command:"addComment",type:"button",icon:'<svg fill="#000000" width="24px" height="24px" viewBox="0 0 32 32" id="icon" xmlns="http://www.w3.org/2000/svg"><defs><style>.cls-1{fill:none;}</style></defs><title>add-comment</title><path d="M17.74,30,16,29l4-7h6a2,2,0,0,0,2-2V8a2,2,0,0,0-2-2H6A2,2,0,0,0,4,8V20a2,2,0,0,0,2,2h9v2H6a4,4,0,0,1-4-4V8A4,4,0,0,1,6,4H26a4,4,0,0,1,4,4V20a4,4,0,0,1-4,4H21.16Z"></path><polygon points="17 9 15 9 15 13 11 13 11 15 15 15 15 19 17 19 17 15 21 15 21 13 17 13 17 9"></polygon><rect class="cls-1" width="32" height="32"></rect></svg>'},{label:"Show / Hide Comments",command:"toggleComments",type:"button",icon:'<svg width="24px" height="24px" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M16 1H4V11H8L10 13L12 11H16V1Z" fill="#000000"></path><path d="M2 5V13H7.17157L8.70711 14.5355L7.29289 15.9497L6.34315 15H0V5H2Z" fill="#000000"></path></svg>'}],commands:{addComment:(t,e)=>{A(e),T();const r=u();return r?(r.savedSelection=R(r.root),r.savedSelection&&window.getSelection()?.removeAllRanges(),g(r,!0),m(r),r.panelElement?.querySelector(".new-comment-textarea")?.focus({preventScroll:!0}),!0):!1},toggleComments:(t,e)=>{A(e),T();const r=u();return r?(g(r,!r.panelVisible),r.panelVisible&&m(r),!0):!1}},keymap:{}});exports.CommentsPlugin=K;
