import{n as e,t}from"./supabase-public-B-SdT9fc.js";import{r as n,s as r}from"./catalog-D09J2EDp.js";import{a as i,i as a,o,s,t as c}from"./types-B5Q2tyc0.js";import{t as l}from"./client-hkSfcnbu.js";import{t as u}from"./preload-helper-Czpn1I53.js";async function d(e,t){let n=await e(0,t-1,!0),r=[...n.rows];if(n.rows.lengthi+n*t).filter(e=>ee(n,n+t-1,!1)));for(let e of o)r.push(...e.rows)}return r}var f=new Set([`a`,`an`,`and`,`for`,`in`,`kenya`,`nairobi`,`of`,`on`,`the`,`to`,`with`,`price`,`buy`,`shop`,`new`,`original`,`genuine`]),p=.55,m=.08;function h(e){return e?e.toLowerCase().replace(/&/g,` and `).replace(/[^a-z0-9]+/g,` `).split(` `).filter(e=>e.length>1&&!f.has(e)):[]}function g(e){let t=new Set(h(e)),n=new Set;for(let e of t)for(let t of e.match(/\d{3,}/g)??[])n.add(t);return{words:t,parts:n}}function _(e,t){if(e.size===0||t.size===0)return 0;let n=0;for(let r of e)t.has(r)&&(n+=1);return n/(e.size+t.size-n)}function v(e,t){if(e.size===0)return 0;let n=0;for(let r of e)t.has(r)&&(n+=1);return n/e.size}function y(e,t){let n=g(e),r=g(t.slug),i=g(t.title),a=[...n.parts],o=new Set([...r.parts,...i.parts]);if(a.length>0&&o.size>0&&a.filter(e=>o.has(e)).length===0||[...o].filter(e=>!n.parts.has(e)).length>0&&v(n.words,r.words)<1)return 0;let s=a.filter(e=>!o.has(e)),c=n.words.size>0&&v(n.words,r.words)===1;if(s.length>0&&!c)return 0;let l=new Set([...n.words].flatMap(e=>e.match(/\d+/g)??[])),u=new Set([...r.words,...i.words].flatMap(e=>e.match(/\d+/g)??[])),d=[...l].some(e=>!u.has(e))||[...u].some(e=>!l.has(e));if(l.size>0&&u.size>0&&d&&!c)return 0;let f=v(n.words,r.words),p=_(n.words,r.words),m=_(n.words,i.words),h=(t.brand??``).toLowerCase(),y=h.length>1&&n.words.has(h.split(/\s+/)[0])&&!r.words.has(h.split(/\s+/)[0]),b=f*.5+p*.3+m*.2;return y?b*.5:b}function b(e,t){let n=e.trim().toLowerCase();if(!n)return null;let r=null,i=0;for(let e of t){if(!e.slug||e.slug===n)continue;let t=y(n,e);!r||t>r.score?(r&&(i=r.score),r={slug:e.slug,score:t}):t>i&&(i=t)}return!r||r.score{let r=t(),i=()=>r.from(`product_cards`).select(w).eq(`is_active`,!0),[a,o,s,c]=await Promise.all([i().order(`created_at`,{ascending:!1}).order(`id`).limit(C),i().eq(`is_featured`,!0).order(`created_at`).order(`id`).limit(C),i().not(`compare_at_price`,`is`,null).order(`created_at`).order(`id`).limit(C),e(r)]),l=Object.entries(c).sort((e,t)=>t[1].count-e[1].count||t[1].average-e[1].average).slice(0,C).map(([e])=>e),u=l.length?await i().in(`id`,l).limit(C):{data:[],error:null},d=[];for(let e of[a,o,s,u]){if(e.error)throw Error(e.error.message);d.push(...e.data??[])}return{products:D(d).map(e=>n(e,c[e.id]??{average:0,count:0}))}});function E(e){return new Date(e.updated_at??e.created_at??0).getTime()||0}function D(e){let t=new Map;for(let n of e){let e=t.get(n.slug);(!e||E(n)>E(e))&&t.set(n.slug,n)}return[...t.values()].sort((e,t)=>E(e)-E(t))}async function O(e){return D(await d(async(t,n,r)=>{let{data:i,error:a,count:o}=await e.from(`product_cards`).select(w,r?{count:`exact`}:{}).eq(`is_active`,!0).order(`created_at`).order(`id`).range(t,n);if(a)throw Error(a.message);return{rows:i??[],count:o}},S))}async function k(e){let t=[];for(let n=0;;n+=S){let{data:r,error:i}=await e.from(`product_cards`).select(`slug`).eq(`is_active`,!0).order(`slug`).range(n,n+S-1);if(i)throw Error(i.message);if(t.push(...(r??[]).map(e=>e.slug)),!r||r.length{let i=t(),[a,o,{data:s},{data:c}]=await Promise.all([O(i),e(i),i.from(`banners`).select(`*`).eq(`active`,!0).order(`position`),i.from(`testimonials`).select(`*`).eq(`active`,!0)]);return{products:a.map(e=>n(e,o[e.id]??{average:0,count:0})),banners:(s??[]).map(e=>({...e,image:r(e.image_key)})),testimonials:c??[]}});async function j(e,t){let[{data:r},{data:i}]=await Promise.all([e.from(`reviews`).select(`id,author,rating,title,body,verified,created_at`).eq(`product_id`,t.id).eq(`status`,`approved`).order(`created_at`,{ascending:!1}),t.related_slugs&&t.related_slugs.length>0?e.from(`product_cards`).select(w).in(`slug`,t.related_slugs).eq(`is_active`,!0).limit(4):e.from(`product_cards`).select(w).eq(`category`,t.category).eq(`is_active`,!0).neq(`slug`,t.slug).limit(4)]),a=r??[],o=a.length,s=o?a.reduce((e,t)=>e+t.rating,0)/o:0;return{product:n(t,{average:s,count:o}),reviews:a,related:(i??[]).map(e=>n(e))}}var M=s({method:`GET`}).inputValidator(e=>i({slug:o().max(120)}).parse(e)).handler(async({data:e})=>{let n=t(),{data:r}=await n.from(`products`).select(x).eq(`slug`,e.slug).eq(`is_active`,!0).order(`updated_at`,{ascending:!1}).limit(1).maybeSingle();return r?j(n,r):null}),N=s({method:`GET`}).inputValidator(e=>i({slug:o().max(120)}).parse(e)).handler(async({data:e})=>{let n=t(),{data:r,error:i}=await n.from(`product_cards`).select(w).eq(`is_active`,!0).limit(2e3);if(i)throw Error(i.message);let a=b(e.slug,r??[]);if(!a)return null;let{data:o}=await n.from(`products`).select(x).eq(`slug`,a).eq(`is_active`,!0).order(`updated_at`,{ascending:!1}).limit(1).maybeSingle();return o?j(n,o):null}),P=s({method:`GET`}).handler(async()=>{let e=t(),[{data:n},{data:r}]=await Promise.all([e.from(`faqs`).select(`*`).eq(`active`,!0).order(`position`),e.from(`testimonials`).select(`*`).eq(`active`,!0)]);return{faqs:n??[],testimonials:r??[]}});s({method:`GET`}).handler(async()=>k(t()));var F=s({method:`POST`}).inputValidator(e=>i({code:o().min(1).max(32),subtotal:a().int().min(0)}).parse(e)).handler(async({data:e})=>{let t=e.code.trim().toUpperCase().replace(/\s+/g,``),{data:n}=await l.rpc(`lookup_coupon`,{_code:t}),r=n?.[0]??null;if(!r)return{ok:!1,message:`We couldn't find the code "${t}". Check the spelling and try again.`};if(!r.active)return{ok:!1,message:`${t} is no longer available.`};if(r.expires_at&&new Date(r.expires_at).getTime()=r.usage_limit)return{ok:!1,message:`${t} has reached its usage limit.`};if(e.subtotali({customerName:o().trim().min(2).max(80),email:o().trim().email().max(255),phone:o().trim().min(7).max(20),address:o().trim().min(5).max(200),city:o().trim().min(2).max(80),notes:o().trim().max(500).optional(),couponCode:o().max(32).nullable().optional(),items:c(i({productId:o().uuid(),title:o().max(200),unitPrice:a().int().min(0),qty:a().int().min(1).max(99)})).min(1).max(50)}).parse(e)).handler(async({data:e})=>{let{calculateTotals:t}=await u(async()=>{let{calculateTotals:e}=await import(`./pricing-DESEZYmu.js`);return{calculateTotals:e}},[]),n=e.items.map(e=>e.productId),{data:r}=await l.from(`products`).select(`id,title,price,stock`).in(`id`,n),i=e.items.map(e=>{let t=(r??[]).find(t=>t.id===e.productId);if(!t)throw Error(`One of the products is no longer available.`);return{...e,unitPrice:t.price,title:t.title}}),a=i.reduce((e,t)=>e+t.unitPrice*t.qty,0),o=null;if(e.couponCode){let{data:t}=await l.rpc(`lookup_coupon`,{_code:e.couponCode}),n=t?.[0]??null;n&&n.active&&a>=n.min_subtotal&&(!n.expires_at||new Date(n.expires_at).getTime()>Date.now())&&(o={kind:n.kind,value:n.value})}let s=t(a,o),c=crypto.randomUUID(),d=`ACE-${crypto.randomUUID().replaceAll(`-`,``).slice(0,8).toUpperCase()}`,{error:f}=await l.from(`orders`).insert({id:c,order_number:d,customer_name:e.customerName,email:e.email,phone:e.phone,address:e.address,city:e.city,notes:e.notes??null,coupon_code:e.couponCode??null,subtotal:a,discount:s.discount,vat:s.vat,delivery_fee:s.delivery,total:s.total});if(f)throw Error(`We couldn't place your order. Please try again.`);let{error:p}=await l.from(`order_items`).insert(i.map(e=>({order_id:c,product_id:e.productId,title:e.title,unit_price:e.unitPrice,qty:e.qty})));if(p)throw Error(`We couldn't save your order items. Please try again.`);return e.couponCode&&o&&await l.rpc(`consume_coupon`,{_code:e.couponCode.toUpperCase()}),{orderNumber:d,couponCode:e.couponCode?.toUpperCase()??null,items:i.map(e=>({title:e.title,unitPrice:e.unitPrice,qty:e.qty})),subtotal:a,discount:s.discount,delivery:s.delivery,vat:s.vat,total:s.total}});export{A as a,T as i,M as n,I as o,N as r,F as s,P as t};