(()=>{"use strict";const e={baseURL:window.ABlocksGlobal?.rest_url,async request(e,{method:t="GET",params:s={},data:a=null,headers:r={}}={}){let c=`${this.baseURL}${e}`;Object.keys(s).length&&(c+=`?${new URLSearchParams(s).toString()}`);const o={method:t,headers:{"X-WP-Nonce":window.ABlocksGlobal?.nonce,...r},credentials:"same-origin"};"GET"!==t&&a&&(a instanceof FormData?o.body=a:(o.headers["Content-Type"]="application/json",o.body=JSON.stringify(a)));const l=await fetch(c,o);if(!l.ok)throw new Error(`HTTP error: ${l.status}`);return{data:await l.json()}},get(e,t={},s={}){return this.request(e,{method:"GET",params:t,...s})},post(e,t={},s={}){return this.request(e,{method:"POST",data:t,...s})},delete(e,t={},s={}){return this.request(e,{method:"DELETE",data:t,...s})},put(e,t={},s={}){return this.request(e,{method:"PUT",data:t,...s})},patch(e,t={},s={}){return this.request(e,{method:"PATCH",data:t,...s})}};document.addEventListener("DOMContentLoaded",()=>{async function t(t,a,r,c,o){const l=a.value.trim(),n=t.querySelector(".ablocks-block--search-source")?.value,i=t.querySelector(".ablocks-block--search__current-post-id")?.value;if(""===l)return r.classList.add("ablocks-block--search-empty-result"),void s(!1,c,o);s(!0,c,o);try{const t=await e.get(window.ABlocksGlobal?.namespace+"search",{searchQuery:l,source:n,current_page_id:i});t.data.html?(r.innerHTML=t.data.html,r.classList.remove("ablocks-block--search-empty-result")):(r.innerHTML='<p class="ablocks-block--search-result__list-no-data">No results available for your search</p>',r.classList.remove("ablocks-block--search-empty-result"))}catch(e){r.innerHTML='<p class="ablocks-block--search-result__list-no-data">An error occurred during search. Please try again.</p>',r.classList.remove("ablocks-block--search-empty-result")}finally{s(!1,c,o)}}function s(e,t,s){e?(t.style.display="none",s.style.display="flex"):(t.style.display="flex",s.style.display="none")}document.querySelectorAll(".ablocks-block--search-form").forEach(e=>function(e){const s=e.querySelector(".ablocks-block--search-button"),a=e.querySelector(".ablocks-block--search-input"),r=s?.querySelector(".button-content"),c=s?.querySelector(".loading-spinner");s.addEventListener("click",e=>{a.classList.contains("searchbar-input-open")?a.classList.remove("searchbar-input-open"):a.classList.add("searchbar-input-open")});const o=e.closest(".ablocks-block--search-bar")?.querySelector(".ablocks-block--search-result");let l;o&&o.classList.add("ablocks-block--search-empty-result"),e.addEventListener("submit",s=>{s.preventDefault(),t(e,a,o,r,c)}),s.addEventListener("click",s=>{s.preventDefault(),t(e,a,o,r,c)}),a.addEventListener("input",()=>{clearTimeout(l),l=setTimeout(()=>{t(e,a,o,r,c)},1e3)})}(e)),document.addEventListener("click",e=>{document.querySelectorAll(".ablocks-block--search-bar").forEach(t=>{const s=t.querySelector(".ablocks-block--search-result");!t.contains(e.target)&&s&&s.classList.add("ablocks-block--search-empty-result")})})})})();