import{j as t}from"./jsx-runtime.D_zvdyIk.js";import{c as E}from"./config.MP0qMYzO.js";import{s as K,d as I}from"./js.cookie.C0BS3qSL.js";import{r as s}from"./index.2yJIXLcc.js";import{B as F}from"./index.D9qG4eP_.js";import{AddToCart as L}from"./AddToCart.CVJQz0Jd.js";import"./iconBase.Blz05Vck.js";import"./cartStore.DQm8CpS2.js";const G=({initialProducts:N,initialPageInfo:l,sortKey:u,reverse:S,searchValue:f})=>{const{currencySymbol:h}=E.shopify,[n,x]=s.useState(N),[i,p]=s.useState(l),[g,v]=s.useState(!1),[d,P]=s.useState(u),[m,R]=s.useState(S),[j,w]=s.useState(!1),o=s.useRef(null),C=e=>{const r=K.find(a=>a.slug===e)||I;return{sortKey:r.sortKey,reverse:r.reverse}},y=async()=>{if(!(g||!i.hasNextPage)){v(!0);try{const e=await fetch(`/api/products.json?cursor=${i.endCursor||""}&sortKey=${d}&reverse=${m}`);if(!e.ok)throw new Error("Failed to fetch");const{products:r,pageInfo:a}=await e.json();x(c=>[...c,...r]),p(a),w(!1)}catch(e){console.error("Error loading more products:",e)}finally{v(!1)}}},b=()=>{const r=new URLSearchParams(window.location.search).get("sortKey")||u,{sortKey:a,reverse:c}=C(r);(a!==d||c!==m)&&(P(a),R(c),x([]),p(l),w(!0))};s.useEffect(()=>(window.addEventListener("popstate",b),()=>{window.removeEventListener("popstate",b)}),[l]),s.useEffect(()=>{if(j)y();else{const e=new IntersectionObserver(r=>{r[0].isIntersecting&&y()},{threshold:1});return o.current&&e.observe(o.current),()=>{o.current&&e.unobserve(o.current)}}},[i?.endCursor,d,m,j]);const k=n.length>1?"results":"result";return t.jsxs("div",{children:[t.jsxs("div",{className:"row mx-auto",children:[f?t.jsxs("p",{className:"mb-4",children:[n.length===0?"There are no products that match ":`Showing ${n.length} ${k} for `,t.jsxs("span",{className:"font-bold",children:['"',f,'"']})]}):null,n?.length===0&&t.jsxs("div",{className:"mx-auto pt-5 text-center",children:[t.jsx("img",{className:"mx-auto mb-6",src:"/images/no-search-found.png",alt:"no-search-found",width:211,height:184}),t.jsx("h1",{className:"h2 mb-4",children:"No Product Found!"}),t.jsx("p",{children:"We couldn't find what you filtered for. Try filtering again."})]}),n.map((e,r)=>{const a=e?.variants.length>0?e?.variants[0].id:void 0;return t.jsxs("div",{className:"text-center col-12 sm:col-6 md:col-4 group relative",children:[t.jsxs("div",{className:"md:relative overflow-hidden",children:[t.jsx("img",{src:e.featuredImage?.url||"/images/product_image404.jpg",width:312,height:269,alt:e.featuredImage?.altText||"fallback image",className:"w-full h-[200px] sm:w-[312px] md:h-[269px] object-cover rounded-md border mx-auto"}),t.jsx(L,{variants:e?.variants,availableForSale:e?.availableForSale,handle:e?.handle,defaultVariantId:a,stylesClass:"btn btn-primary max-md:btn-sm z-10 absolute bottom-24 md:bottom-0 left-1/2 transform -translate-x-1/2 md:translate-y-full md:group-hover:-translate-y-6 duration-300 ease-in-out whitespace-nowrap drop-shadow-md"})]}),t.jsxs("div",{className:"py-2 md:py-4 text-center z-20",children:[t.jsx("h2",{className:"font-medium text-base md:text-xl",children:t.jsx("a",{className:"after:absolute after:inset-0",href:`/products/${e?.handle}`,children:e?.title})}),t.jsxs("div",{className:"flex flex-wrap justify-center items-center gap-x-2 mt-2 md:mt-4",children:[t.jsxs("span",{className:"text-base md:text-xl font-bold text-dark dark:text-darkmode-dark",children:[h," ",e?.priceRange?.minVariantPrice?.amount," ",e?.priceRange?.minVariantPrice?.currencyCode]}),parseFloat(e?.compareAtPriceRange?.maxVariantPrice?.amount)>0?t.jsxs("s",{className:"text-light dark:text-darkmode-light text-xs md:text-base font-medium",children:[h," ",e?.compareAtPriceRange?.maxVariantPrice?.amount," ",e?.compareAtPriceRange?.maxVariantPrice?.currencyCode]}):""]})]})]},r)})]}),i?.hasNextPage&&t.jsx("div",{ref:o,className:"text-center py-4 flex justify-center",children:g?t.jsx(F,{className:"animate-spin",size:30}):"Scroll for more"})]})};export{G as default};
