document.addEventListener("DOMContentLoaded",function(){const o=document.getElementById("editorContent"),c=document.getElementById("editorHtml"),g=document.querySelectorAll(".toolbar-btn[data-command]"),v=document.getElementById("formatBlock"),p=document.querySelectorAll(".mode-btn");g.forEach(e=>{e.addEventListener("click",function(){const t=this.getAttribute("data-command");document.execCommand(t,!1,null),o.focus(),u()})}),v&&v.addEventListener("change",function(){document.execCommand("formatBlock",!1,this.value),o.focus()});function u(){g.forEach(e=>{const t=e.getAttribute("data-command");document.queryCommandState(t)?e.classList.add("active"):e.classList.remove("active")})}o&&(o.addEventListener("keyup",u),o.addEventListener("mouseup",u)),p.forEach(e=>{e.addEventListener("click",function(){const t=this.getAttribute("data-mode");p.forEach(n=>n.classList.remove("active")),this.classList.add("active"),t==="html"?(c.value=o.innerHTML,o.style.display="none",c.style.display="block"):(o.innerHTML=c.value,c.style.display="none",o.style.display="block")})});const y=document.getElementById("insertLink");y&&y.addEventListener("click",function(){const e=prompt("Enter URL:","https://");e&&(document.execCommand("createLink",!1,e),o.focus())});const E=document.getElementById("insertImage");E&&E.addEventListener("click",function(){const e=prompt("Enter image URL:","https://");e&&(document.execCommand("insertImage",!1,e),o.focus())});const i=document.getElementById("featuredImageUpload"),a=document.getElementById("featuredImageInput"),h=document.getElementById("uploadPlaceholder"),L=document.getElementById("uploadPreview"),I=document.getElementById("previewImage"),l=document.getElementById("removeImage");i&&a&&(i.addEventListener("click",function(e){e.target!==l&&!l.contains(e.target)&&a.click()}),a.addEventListener("change",function(){const e=this.files[0];if(e){const t=new FileReader;t.onload=function(n){I.src=n.target.result,h.style.display="none",L.style.display="block"},t.readAsDataURL(e)}}),l&&l.addEventListener("click",function(e){e.stopPropagation(),I.src="",a.value="",L.style.display="none",h.style.display="block"}),i.addEventListener("dragover",function(e){e.preventDefault(),this.classList.add("dragover")}),i.addEventListener("dragleave",function(){this.classList.remove("dragover")}),i.addEventListener("drop",function(e){e.preventDefault(),this.classList.remove("dragover");const t=e.dataTransfer.files[0];if(t&&t.type.startsWith("image/")){a.files=e.dataTransfer.files;const n=new Event("change");a.dispatchEvent(n)}}));const d=document.getElementById("documentUploadZone"),m=document.getElementById("documentInput");d&&m&&(d.addEventListener("click",function(){m.click()}),d.addEventListener("dragover",function(e){e.preventDefault(),this.classList.add("dragover")}),d.addEventListener("dragleave",function(){this.classList.remove("dragover")}),d.addEventListener("drop",function(e){e.preventDefault(),this.classList.remove("dragover");const t=e.dataTransfer.files;B(t)}),m.addEventListener("change",function(){B(this.files)}));function B(e){const t=document.getElementById("uploadQueue"),n=document.getElementById("queueList");e.length>0&&t&&n&&(t.style.display="block",Array.from(e).forEach(s=>{const r=document.createElement("div");r.className="upload-queue-item",r.innerHTML=`
                    <div class="queue-item-info">
                        <i class="fas fa-file"></i>
                        <span>${s.name}</span>
                        <small>${F(s.size)}</small>
                    </div>
                    <div class="queue-item-progress">
                        <div class="progress-bar" style="width: 0%"></div>
                    </div>
                `,n.appendChild(r),A(r.querySelector(".progress-bar"))}))}function A(e){let t=0;const n=setInterval(function(){t+=Math.random()*30,t>=100&&(t=100,clearInterval(n),e.style.background="var(--success)"),e.style.width=t+"%"},200)}function F(e){if(e===0)return"0 Bytes";const t=1024,n=["Bytes","KB","MB","GB"],s=Math.floor(Math.log(e)/Math.log(t));return parseFloat((e/Math.pow(t,s)).toFixed(2))+" "+n[s]}const k=document.getElementById("pageDescription"),b=document.getElementById("descCount");k&&b&&k.addEventListener("input",function(){b.textContent=this.value.length});const T=document.getElementById("metaTitle"),D=document.getElementById("metaTitleCount"),C=document.getElementById("googleTitle");T&&D&&T.addEventListener("input",function(){D.textContent=this.value.length,C&&(C.textContent=this.value)});const M=document.getElementById("metaDescription"),S=document.getElementById("metaDescCount"),H=document.getElementById("googleDescription");M&&S&&M.addEventListener("input",function(){S.textContent=this.value.length,H&&(H.textContent=this.value)});const q=document.getElementById("pageStatus"),f=document.getElementById("scheduledDateGroup");q&&f&&q.addEventListener("change",function(){this.value==="scheduled"?f.style.display="block":f.style.display="none"});const w=document.getElementById("pageEditorForm");w&&w.addEventListener("submit",function(e){e.preventDefault(),new FormData(this).append("content",o.innerHTML);const n=this.querySelector('button[type="submit"]'),s=n.innerHTML;n.innerHTML='<i class="fas fa-spinner fa-spin"></i> Saving...',n.disabled=!0,setTimeout(function(){n.innerHTML='<i class="fas fa-check"></i> Saved!',n.classList.add("btn-success"),setTimeout(function(){n.innerHTML=s,n.classList.remove("btn-success"),n.disabled=!1},2e3)},1500)});const x=document.getElementById("saveDraft");x&&x.addEventListener("click",function(){const e=this.innerHTML;this.innerHTML='<i class="fas fa-spinner fa-spin"></i> Saving...',this.disabled=!0,setTimeout(()=>{this.innerHTML='<i class="fas fa-check"></i> Saved!',setTimeout(()=>{this.innerHTML=e,this.disabled=!1},2e3)},1e3)}),document.addEventListener("keydown",function(e){if((e.ctrlKey||e.metaKey)&&e.key==="s"){e.preventDefault();const t=document.querySelector('button[type="submit"]');t&&t.click()}});const U=document.querySelector(".gallery-upload");U&&U.addEventListener("click",function(){alert("Open media library or file picker")}),document.querySelectorAll(".btn-danger-glow, .gallery-btn.danger, .doc-btn.danger, .comment-btn.danger").forEach(e=>{e.addEventListener("click",function(t){confirm("Are you sure you want to delete this item?")||t.preventDefault()})})});
