document.addEventListener("DOMContentLoaded",function(){function e(e){const t=e.querySelector(".fb-search-block-wrapper-inner-input-field"),n=e.querySelector(".fb-search-block-wrapper-overlay"),r=e.querySelector(".fb-search-ajax-results"),s=e.querySelector(".fb-search-container.button-and-search .fb-search-button-wrapper");t&&t.classList.remove("active"),n&&n.classList.remove("active"),r&&r.classList.remove("active"),s&&(s.style.visibility="visible")}document.querySelectorAll(".fb-search-block").forEach(function(t){const n=t.querySelector(".fb-search-container.button-and-search .fb-search-button-wrapper"),r=t.querySelector(".fb-search-block-wrapper-overlay"),s=t.querySelector(".fb-search-block-wrapper-inner-input-field"),a=t.querySelector(".wp-block-search__input");if(n&&n.addEventListener("click",function(){s.classList.add("active"),r.classList.add("active"),s.classList.contains("active")?n.style.visibility="hidden":n.style.visibility="visible",a&&setTimeout(()=>a.focus(),50)}),r&&r.addEventListener("click",function(){e(t)}),document.addEventListener("keydown",function(n){"Escape"===n.key&&e(t)}),!a)return;const c=document.createElement("div");c.className="fb-search-ajax-results",c.setAttribute("aria-live","polite"),c.setAttribute("role","listbox"),s.appendChild(c);let i=null,o=null,l=-1;function u(e,t){e.forEach(function(e,n){e.classList.toggle("selected",n===t),n===t&&e.scrollIntoView({block:"nearest"})})}function d(e){const t=document.createElement("div");return t.appendChild(document.createTextNode(e)),t.innerHTML}a.addEventListener("input",function(){const e=a.value.trim();if(clearTimeout(i),e.length<2)return c.classList.remove("active"),void(l=-1);var t;(t=c).innerHTML='\n                    <div class="fb-search-loading">\n                        <span class="fb-search-spinner"></span>\n                        <span>Searching...</span>\n                    </div>',t.classList.add("active"),i=setTimeout(function(){!function(e,t){o&&(o.abort(),o=null);const n=new AbortController;o=n;const r=`${FrontisBlocksData.site_url}/wp-json/frontis-blocks/v1/search?query=${encodeURIComponent(e)}`;fetch(r,{method:"GET",headers:{Accept:"application/json"},signal:n.signal}).then(function(e){if(!e.ok)throw new Error("Network error");return e.json()}).then(function(n){o=null,l=-1,function(e,t,n){if(t.innerHTML="",0===e.length)return t.innerHTML=`<div class="fb-search-no-results">No results found for "<strong>${d(n)}</strong>"</div>`,void t.classList.add("active");const r=document.createDocumentFragment();e.forEach(function(e,t){const s=document.createElement("div");s.className="fb-search-result-item",s.setAttribute("role","option"),s.setAttribute("data-index",t);const a=function(e,t){const n=d(e),r=t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");return n.replace(new RegExp(`(${r})`,"gi"),'<mark class="fb-search-highlight">$1</mark>')}(e.title,n);var c;s.innerHTML=`\n                        <a href="${e.permalink}" class="fb-search-result-link">\n                            <div class="fb-search-result-content">\n                                <span class="fb-search-result-title">${a}</span>\n                                ${e.excerpt?`<span class="fb-search-result-excerpt">${d(e.excerpt)}</span>`:""}\n                                <span class="fb-search-result-meta">\n                                    ${c=e.type,c.charAt(0).toUpperCase()+c.slice(1)}\n                                    <span class="meta-divider"></span>\n                                    ${e.date}\n                                </span>\n                            </div>\n                        </a>\n                    `,r.appendChild(s)});const s=document.createElement("div");s.className="fb-search-results-footer",s.innerHTML=`<a href="${FrontisBlocksData.site_url}/?s=${encodeURIComponent(n)}" class="fb-search-view-all">View all results for "<strong>${d(n)}</strong>" →</a>`,r.appendChild(s),t.appendChild(r),t.classList.add("active")}(n.results,t,e)}).catch(function(e){"AbortError"!==e.name&&(o=null,function(e){e.innerHTML='<div class="fb-search-error">Something went wrong. Please try again.</div>',e.classList.add("active")}(t))})}(e,c)},350)}),a.addEventListener("keydown",function(e){const t=c.querySelectorAll(".fb-search-result-item");t.length&&("ArrowDown"===e.key?(e.preventDefault(),l=Math.min(l+1,t.length-1),u(t,l)):"ArrowUp"===e.key?(e.preventDefault(),l=Math.max(l-1,-1),u(t,l)):"Enter"===e.key&&l>=0&&(e.preventDefault(),t[l].querySelector("a").click()))}),document.addEventListener("click",function(n){t.contains(n.target)||e(t)})})});