import*as e from"@wordpress/interactivity";var t,r,n={d:function(e,t){for(var r in t)n.o(t,r)&&!n.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},o:function(e,t){return Object.prototype.hasOwnProperty.call(e,t)}},o=(t={getContext:function(){return e.getContext},getElement:function(){return e.getElement},store:function(){return e.store},withSyncEvent:function(){return e.withSyncEvent}},r={},n.d(r,t),r);const i=e=>e&&e instanceof window.HTMLAnchorElement&&e.href&&(!e.target||"_self"===e.target)&&e.origin===window.location.origin;let c=null;const{state:a}=(0,o.store)("surecart/product-list",{state:{loading:!1,searching:!1},actions:{navigate:(0,o.withSyncEvent)((function*(e){const{ref:t}=(0,o.getElement)(),r=(0,o.getContext)(),n=r?.history,c=t.closest("[data-wp-router-region]");if(i(t)&&(e=>!(0!==e.button||e.metaKey||e.ctrlKey||e.altKey||e.shiftKey||e.defaultPrevented))(e)&&c){e.preventDefault();const{actions:r}=yield import("@wordpress/interactivity-router");a.loading=!0,yield r.navigate(t.href,{replace:!1===n}),a.loading=!1;const o=c.querySelector(".sc-product-item a[href]");o?.focus()}})),*prefetch(){const{ref:e}=(0,o.getElement)();if(i(e)){const{actions:t}=yield import("@wordpress/interactivity-router");yield t.prefetch(e.href)}},onSearchInput:(0,o.withSyncEvent)((function*(e){e.preventDefault();const{value:t}=e.target,r=(0,o.getContext)();if(!r)return;if(t===r.search)return;r.search=t,a.searching=!0,c?.();const{promise:n,resolve:i,reject:s}=Promise.withResolvers(),l=setTimeout(i,500);c=()=>{clearTimeout(l),s()};try{yield n}catch{return}const u=new URL(window.location.href);u.searchParams.delete(`${r.urlPrefix}-page`),t?u.searchParams.set(`${r.urlPrefix}-search`,t):u.searchParams.delete(`${r.urlPrefix}-search`),a.loading=!0,a.searching=!0;const{actions:d}=yield import("@wordpress/interactivity-router");d.navigate(u.href);const h=(0,o.getContext)(),f=new CustomEvent("scSearched",{detail:{searchString:t,searchResultCount:h?.products?.length,searchResultIds:h?.products?.map((e=>e.id))},bubbles:!0});document.dispatchEvent(f),a.loading=!1,a.searching=!1})),clearSearch:(0,o.withSyncEvent)((function*(e){if("keydown"===e?.type&&!["Enter","Space"].includes(e.key))return!0;e.preventDefault();const t=(0,o.getContext)();if(!t)return;t.search="",a.loading=!0,a.searching=!0;const r=new URL(window.location.href);r.searchParams.delete(`${t.urlPrefix}-page`),r.searchParams.delete(`${t.urlPrefix}-search`);const{actions:n}=yield import("@wordpress/interactivity-router");n.navigate(r.href),a.loading=!1,a.searching=!1}))},callbacks:{*onChangeProducts(){const e=(0,o.getContext)();e?.products&&((window?.dataLayer||window?.gtag)&&(yield import("@surecart/google-events")),window?.fbq&&(yield import("@surecart/facebook-events")),document.dispatchEvent(new CustomEvent("scProductsViewed",{detail:{products:e.products,pageTitle:document.title},bubbles:!0})))},*prefetch(){const e=(0,o.getContext)(),{ref:t}=(0,o.getElement)();if(e?.url&&i(t)){const{actions:e}=yield import("@wordpress/interactivity-router");yield e.prefetch(t.href)}}}});