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