(function (){
'use strict';
var ajaxUrl=(window.zdSearch&&window.zdSearch.ajaxUrl)||'/wp-admin/admin-ajax.php';
var overlay=document.getElementById('search-overlay');
var input=document.getElementById('search-input');
var listEl=document.getElementById('search-list');
var emptyEl=document.getElementById('search-empty');
var noneEl=document.getElementById('search-none');
var loadingEl=document.getElementById('search-loading');
var pluginWrap=document.getElementById('zds-plugin-wrap');
var pluginBtn=document.getElementById('zds-plugin-btn');
var pluginDrop=document.getElementById('zds-plugin-dropdown');
var pluginLbl=document.getElementById('zds-plugin-label');
var chevron=document.getElementById('zds-plugin-chevron');
var closeBtn=document.getElementById('search-close');
var openBtn=document.getElementById('search-btn');
var heroBtn=document.getElementById('hero-search-trigger');
if(!overlay||!input) return;
var currentDocId=0;
var activeIdx=-1;
var debounceT=null;
var lastXhr=null;
var lastFocus=null;
function openOverlay(){
lastFocus=document.activeElement;
overlay.style.display='flex';
overlay.setAttribute('aria-hidden', 'false');
if(openBtn) openBtn.setAttribute('aria-expanded', 'true');
setTimeout(function (){ input.focus(); }, 50);
if(input.value.trim().length >=2) doSearch(input.value.trim());
}
function closeOverlay(){
overlay.style.display='none';
overlay.setAttribute('aria-hidden', 'true');
if(openBtn) openBtn.setAttribute('aria-expanded', 'false');
closeDrop();
if(lastFocus&&typeof lastFocus.focus==='function'){
lastFocus.focus();
}}
function openDrop(){
pluginDrop.style.display='block';
chevron.style.transform='rotate(180deg)';
pluginBtn.setAttribute('aria-expanded', 'true');
}
function closeDrop(){
pluginDrop.style.display='none';
chevron.style.transform='';
pluginBtn.setAttribute('aria-expanded', 'false');
}
if(openBtn) openBtn.addEventListener('click', openOverlay);
if(heroBtn) heroBtn.addEventListener('click', openOverlay);
if(closeBtn) closeBtn.addEventListener('click', closeOverlay);
overlay.addEventListener('mousedown', function (e){
if(e.target===overlay) closeOverlay();
});
document.addEventListener('keydown', function (e){
if(e.key==='Escape'){
if(overlay.style.display==='flex'){
if(pluginDrop.style.display!=='none') closeDrop();
else closeOverlay();
}
return;
}
if((e.metaKey||e.ctrlKey)&&e.key==='k'){
e.preventDefault();
openOverlay();
return;
}
if(overlay.style.display==='flex'){
handleArrows(e);
trapFocus(e);
}});
function trapFocus(e){
if(e.key!=='Tab') return;
var focusables=overlay.querySelectorAll('button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
);
var visible=[];
for (var i=0; i < focusables.length; i++){
if(focusables[i].offsetParent!==null) visible.push(focusables[i]);
}
if(!visible.length) return;
var first=visible[0];
var last=visible[visible.length - 1];
if(e.shiftKey&&document.activeElement===first){
e.preventDefault();
last.focus();
}else if(!e.shiftKey&&document.activeElement===last){
e.preventDefault();
first.focus();
}}
if(pluginBtn){
pluginBtn.addEventListener('click', function (e){
e.stopPropagation();
if(pluginDrop.style.display==='none') openDrop();
else closeDrop();
});
}
document.addEventListener('click', function (e){
if(pluginWrap&&!pluginWrap.contains(e.target)) closeDrop();
});
document.querySelectorAll('.zds-plugin-opt').forEach(function (opt){
opt.addEventListener('mouseenter', function (){
this.style.background='#eff6ff';
this.style.color='#2563eb';
});
opt.addEventListener('mouseleave', function (){
this.style.background='';
var isSelected=this.dataset.docId==currentDocId ||
(this.dataset.docId=='0'&&currentDocId==0);
this.style.color=isSelected ? '#2563eb':'#374151';
});
opt.addEventListener('click', function (){
currentDocId=parseInt(this.dataset.docId, 10);
pluginLbl.textContent=currentDocId===0 ? 'All Docs':this.textContent.trim();
document.querySelectorAll('.zds-plugin-opt').forEach(function (o){
o.style.fontWeight='400';
o.style.color='#374151';
});
this.style.fontWeight='600';
this.style.color='#2563eb';
closeDrop();
if(input.value.trim().length >=2) doSearch(input.value.trim());
});
});
input.addEventListener('input', function (){
clearTimeout(debounceT);
var q=this.value.trim();
if(q.length < 2){ showState('empty'); return; }
showState('loading');
debounceT=setTimeout(function (){ doSearch(q); }, 280);
});
function doSearch(q){
if(lastXhr) lastXhr.abort();
showState('loading');
var url=ajaxUrl + '?action=zd_search&q=' + encodeURIComponent(q) +
(currentDocId ? '&doc_id=' + currentDocId:'');
lastXhr=new XMLHttpRequest();
lastXhr.open('GET', url);
lastXhr.onload=function (){
try {
var d=JSON.parse(lastXhr.responseText);
if(d.success) renderResults(d.data, q);
else showState('none');
} catch (e){ showState('none'); }};
lastXhr.onerror=function (){ showState('none'); };
lastXhr.send();
}
var depthIcons=[
'<svg style="width:15px;height:15px;" fill="none" stroke="currentColor" stroke-width="1.8" viewBox="0 0 24 24" aria-hidden="true"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.75V12A2.25 2.25 0 014.5 9.75h15A2.25 2.25 0 0121.75 12v.75m-8.69-6.44l-2.12-2.12a1.5 1.5 0 00-1.061-.44H4.5A2.25 2.25 0 002.25 6v12a2.25 2.25 0 002.25 2.25h15A2.25 2.25 0 0021.75 18V9a2.25 2.25 0 00-2.25-2.25h-5.379a1.5 1.5 0 01-1.06-.44z"/></svg>',
'<svg style="width:15px;height:15px;" fill="none" stroke="currentColor" stroke-width="1.8" viewBox="0 0 24 24" aria-hidden="true"><path stroke-linecap="round" stroke-linejoin="round" d="M3.75 12h16.5m-16.5 3.75h16.5M3.75 19.5h16.5M5.625 4.5h12.75a1.875 1.875 0 010 3.75H5.625a1.875 1.875 0 010-3.75z"/></svg>',
'<svg style="width:15px;height:15px;" fill="none" stroke="currentColor" stroke-width="1.8" viewBox="0 0 24 24" aria-hidden="true"><path stroke-linecap="round" stroke-linejoin="round" d="M19.5 14.25v-2.625a3.375 3.375 0 00-3.375-3.375h-1.5A1.125 1.125 0 0113.5 7.125v-1.5a3.375 3.375 0 00-3.375-3.375H8.25m0 12.75h7.5m-7.5 3H12M10.5 2.25H5.625c-.621 0-1.125.504-1.125 1.125v17.25c0 .621.504 1.125 1.125 1.125h12.75c.621 0 1.125-.504 1.125-1.125V11.25a9 9 0 00-9-9z"/></svg>'
];
function hl(t, q){
return t.replace(new RegExp('(' + q.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + ')', 'gi'),
'<mark style="background:#fef9c3;color:#92400e;border-radius:2px;font-style:normal;">$1</mark>'
);
}
function renderResults(results, q){
if(!results||!results.length){ showState('none'); return; }
activeIdx=-1;
var html='';
results.forEach(function (r, i){
var icon=depthIcons[Math.min(r.depth, 2)];
var crumb=r.breadcrumb&&r.breadcrumb.length
? '<span style="font-size:11px;color:#94a3b8;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">' + r.breadcrumb.join(' › ') + '</span>'
: '';
html +='<a href="' + r.url + '" class="zd-sr-item" data-idx="' + i + '" style="display:flex;align-items:center;gap:12px;padding:11px 16px;text-decoration:none;color:#1e293b;border-bottom:1px solid #f1f5f9;transition:background .1s;">' +
'<span style="flex-shrink:0;display:flex;align-items:center;justify-content:center;width:30px;height:30px;border-radius:7px;background:#f1f5f9;color:#64748b;">' + icon + '</span>' +
'<span style="flex:1;min-width:0;display:flex;flex-direction:column;gap:2px;"><span style="font-size:14px;font-weight:500;color:#0f172a;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">' + hl(r.title, q) + '</span>' + crumb + '</span>' +
'<svg style="width:13px;height:13px;color:#cbd5e1;flex-shrink:0;" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24" aria-hidden="true"><path stroke-linecap="round" stroke-linejoin="round" d="M13.5 4.5L21 12m0 0l-7.5 7.5M21 12H3"/></svg>' +
'</a>';
});
listEl.innerHTML=html;
listEl.querySelectorAll('.zd-sr-item').forEach(function (a){
a.addEventListener('mouseenter', function (){ this.style.background='#eff6ff'; });
a.addEventListener('mouseleave', function (){
if(!this.classList.contains('zd-sr-active')) this.style.background='';
});
});
showState('list');
}
function handleArrows(e){
var items=listEl.querySelectorAll('.zd-sr-item');
if(!items.length) return;
if(e.key==='ArrowDown'){ e.preventDefault(); setActive(activeIdx + 1, items); }
if(e.key==='ArrowUp'){ e.preventDefault(); setActive(activeIdx - 1, items); }
if(e.key==='Enter'&&activeIdx >=0){ e.preventDefault(); items[activeIdx].click(); }}
function setActive(idx, items){
if(items[activeIdx]){
items[activeIdx].classList.remove('zd-sr-active');
items[activeIdx].style.background='';
}
activeIdx=Math.max(0, Math.min(idx, items.length - 1));
items[activeIdx].classList.add('zd-sr-active');
items[activeIdx].style.background='#eff6ff';
items[activeIdx].scrollIntoView({ block: 'nearest' });
}
function showState(s){
emptyEl.style.display=s==='empty'   ? 'block':'none';
loadingEl.style.display=s==='loading' ? 'block':'none';
noneEl.style.display=s==='none'    ? 'block':'none';
listEl.style.display=s==='list'    ? 'block':'none';
}})();