const{registerPlugin:k}=wp.plugins,{PluginSidebar:q}=wp.editPost,{useSelect:w}=wp.data,{createElement:h}=wp.element,C="notifal-tags-sidebar";function A(o){if(!o)return!1;const e=o.attributes||o;if(!e||typeof e!="object")return!1;const t=["content","text","title","label","caption","alt","placeholder","value","description","excerpt","body","message","citation","anchor","linkText","buttonText","heading","subheading"];for(const a of t)if(e[a]){const n=e[a];if(typeof n=="string"&&n.trim().length>0||typeof n=="object"&&n.length>0)return!0}for(const[a,n]of Object.entries(e))if(typeof n=="string"&&n.trim().length>0&&!["url","href","src","id","className","style","width","height","size","color","backgroundColor","align","position","margin","padding","border"].includes(a))return!0;return!1}function I(){if(sessionStorage.getItem("notifal-tags-tooltip-closed")==="true")return;const o=document.getElementById("notifal-tags-tooltip");o&&o.remove();const e=b();if(!e)return;const t=document.createElement("div");t.id="notifal-tags-tooltip",t.innerHTML=`
        <div class="tooltip-arrow"></div>
        <span>Notifal Tags</span>
        <button id="notifal-tooltip-close" style="
            background: transparent;
            border: none;
            color: #ffffff;
            cursor: pointer;
            padding: 2px;
            font-size: 14px;
            line-height: 1;
            margin-left: 4px;
        " aria-label="Close tooltip"><span class="notifal-icon notifal-icon-x-circle size-16"></span></button>
    `,Object.assign(t.style,{position:"fixed",backgroundColor:"#1e1e1e",color:"#ffffff",padding:"8px 12px",borderRadius:"6px",fontSize:"12px",fontFamily:'-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',boxShadow:"0 2px 8px rgba(0, 0, 0, 0.3)",display:"flex",alignItems:"center",gap:"8px",maxWidth:"200px",zIndex:"1000000",pointerEvents:"auto"});const a=t.querySelector(".tooltip-arrow");Object.assign(a.style,{position:"absolute",top:"-6px",left:"50%",transform:"translateX(-50%)",width:"0",height:"0",borderLeft:"6px solid transparent",borderRight:"6px solid transparent",borderBottom:"6px solid #1e1e1e"});const n=e.getBoundingClientRect();t.style.top=n.bottom+8+"px",t.style.left=n.left+n.width/2+"px",t.style.transform="translateX(-50%)",document.body.appendChild(t),t.querySelector("#notifal-tooltip-close").addEventListener("click",()=>{t.remove(),sessionStorage.setItem("notifal-tags-tooltip-closed","true")})}function b(){const o=['button[aria-controls="notifal-tags-sidebar"]','button[aria-label*="Notifal Tags"]','button[aria-label*="Notifal"]','.components-button[aria-controls="notifal-tags-sidebar"]','.edit-post-sidebar button[aria-controls*="notifal"]'];for(const e of o){const t=document.querySelector(e);if(t)return t}return null}function x(){const o=document.getElementById("notifal-tags-tooltip");o&&(b()||o.remove())}function E(){let o=!1;document.addEventListener("click",()=>{o||(o=!0,setTimeout(()=>{b()?I():x(),o=!1},500))}),setInterval(()=>{x()},2e3)}function B(o){if(!o)return;const e=o.querySelector(".notifal-search-input");e&&(e.removeEventListener("input",T),e.addEventListener("input",T)),o.querySelectorAll(".notifal-category-tab").forEach(n=>{n.removeEventListener("click",S),n.addEventListener("click",S)}),o.querySelectorAll(".notifal-tag").forEach(n=>{n.removeEventListener("click",L),n.addEventListener("click",L)})}function T(o){const e=o.target,t=e.value.toLowerCase().trim(),a=e.closest("#notifal-tags-panel-content");if(!a)return;const n=a.querySelector("[data-search-input]");if(!n)return;const u=n.querySelectorAll(".notifal-tag"),g=n.querySelectorAll(".notifal-tags-category"),f=n.querySelector(".notifal-no-results");let i=0;u.forEach(s=>{const d=s.dataset.tagText||"",l=s.dataset.label||"",r=!t||d.toLowerCase().includes(t)||l.toLowerCase().includes(t);s.style.display=r?"inline-block":"none",r&&i++}),g.forEach(s=>{const d=s.querySelectorAll(".notifal-tag"),l=Array.from(d).some(r=>r.style.display!=="none");s.style.display=l?"block":"none"}),f&&(f.style.display=i===0&&t?"block":"none"),R(n,t)}function S(o){const e=o.target.closest(".notifal-category-tab");if(!e)return;const t=e.closest("#notifal-tags-panel-content");if(!t)return;const a=t.querySelector("[data-search-input]");if(!a)return;const n=e.dataset.category;t.querySelectorAll(".notifal-category-tab").forEach(s=>s.classList.remove("active")),e.classList.add("active");const g=a.querySelectorAll(".notifal-tags-category"),f=t.querySelector(".notifal-search-input"),i=f?f.value.toLowerCase().trim():"";g.forEach(s=>{const d=s.dataset.category;let l=!1;if(n==="all"){const r=s.querySelectorAll(".notifal-tag");l=Array.from(r).some(c=>{const p=c.dataset.tagText||"",y=c.dataset.label||"";return!i||p.toLowerCase().includes(i)||y.toLowerCase().includes(i)})}else if(l=d===n,l&&i){const r=s.querySelectorAll(".notifal-tag");r.forEach(c=>{const p=c.dataset.tagText||"",y=c.dataset.label||"",m=p.toLowerCase().includes(i)||y.toLowerCase().includes(i);c.style.display=m?"inline-block":"none"}),l=Array.from(r).some(c=>c.style.display!=="none")}s.style.display=l?"block":"none"}),M(a,i)}function L(o){const e=o.target.closest(".notifal-tag");if(!e)return;const t=e.querySelector(".notifal-tag-text");if(!t)return;const a=t.dataset.originalText||t.textContent.replace(/ - Copied$/,"").trim();t.dataset.originalText||(t.dataset.originalText=a),e.classList.contains("notifal-tag-dynamic")?(o.preventDefault(),o.stopPropagation(),typeof createDynamicKeysPopup=="function"&&createDynamicKeysPopup(a,e)):(N(a),e.classList.add("copied"),t.textContent=a+" - Copied",setTimeout(()=>{e.classList.remove("copied"),t.textContent=a},1500))}function N(o){navigator.clipboard&&navigator.clipboard.writeText?navigator.clipboard.writeText(o).catch(()=>{v(o)}):v(o)}function v(o){const e=document.createElement("textarea");e.value=o,e.style.position="fixed",e.style.opacity="0",document.body.appendChild(e),e.focus(),e.select();try{document.execCommand("copy")}catch(t){console.error("Fallback copy exception:",t)}document.body.removeChild(e)}function R(o,e){const t=o.closest("#notifal-tags-panel-content");if(!t)return;t.querySelectorAll(".notifal-category-tab").forEach(n=>{const u=n.dataset.category;let g=0;if(u==="all")g=o.querySelectorAll('.notifal-tag[style*="display: inline-block"], .notifal-tag:not([style*="display"])').length;else{const i=o.querySelector(`.notifal-tags-category[data-category="${u}"]`);i&&i.querySelectorAll(".notifal-tag").forEach(d=>{const l=d.dataset.tagText||"",r=d.dataset.label||"";(!e||l.toLowerCase().includes(e)||r.toLowerCase().includes(e))&&g++})}const f=n.querySelector(".notifal-category-count");f&&(f.textContent=`(${g})`)})}function M(o,e){const t=o.querySelector(".notifal-no-results");if(!t)return;const n=o.querySelectorAll('.notifal-tag[style*="display: inline-block"], .notifal-tag:not([style*="display"])').length>0;t.style.display=!n&&e?"block":"none"}function z(){const o=w(t=>t("core/block-editor").getSelectedBlock(),[]);return o&&A(o)?h(q,{name:C,title:"Notifal Tags",icon:"tag"},h("div",{id:"notifal-tags-panel-content",dangerouslySetInnerHTML:{__html:document.getElementById("notifal-tags-panel-hidden")?.innerHTML||"<p>Loading tags...</p>"},ref:t=>{t&&t.innerHTML&&t.innerHTML!=="<p>Loading tags...</p>"&&setTimeout(()=>{B(t)},0)}})):null}k(C,{render:z,icon:"tag"});document.addEventListener("DOMContentLoaded",()=>{E()});document.readyState==="loading"||E();
