1import{r as o,j as e,L as E,z as M,w as I,y as k,x as L,T as S}from"./vendor-Dm1e2k69.js";import{u as F,d as $}from"./index-CxY3JD6w.js";import{L as P}from"./main-ClgP_npZ.js";const A=({id:x,src:w,text:C,title:j,label:y,redemptionInfo:v,nextAvailableDate:i,isReusable:N,reuseFrequency:_})=>{const[l,c]=o.useState("available"),[g,b]=o.useState(0),[a,d]=o.useState("");o.useEffect(()=>{if(!v){c("available");return}const s=new Date,D=v.expiresAt.toDate();let p;if(D>s){c("active");const r=()=>{const h=Math.max(0,Math.floor((D.getTime()-new Date().getTime())/1e3));b(h),h<=0&&(i&&i>new Date?c("onCooldown"):c("redeemed"),p&&clearInterval(p))};r(),p=setInterval(r,1e3)}else if(i&&i>s){c("onCooldown");const r=()=>{const h=Math.max(0,i.getTime()-new Date().getTime()),R=Math.floor(h/(1e3*60*60*24)),T=Math.floor(h%(1e3*60*60*24)/(1e3*60*60));R>0?d(`Redeem again in ${R} day(s)`):T>0?d(`Redeem again in ${T} hour(s)`):d("Redeem again soon")};r(),p=setInterval(r,3600*1e3)}else c("redeemed");return()=>{p&&clearInterval(p)}},[v,i]);const u=()=>g>60?`${Math.ceil(g/60)} minutes remaining`:`${g} seconds remaining`,f=l==="active"?`/redeemed-coupon/${x}`:l==="available"?`/coupon/${x}`:"#",m=l==="redeemed"||l==="onCooldown",t=()=>{switch(l){case"active":return e.jsx("h2",{className:"overlay-text timer-text",children:u()});case"onCooldown":return e.jsx("h2",{className:"overlay-text expired-text",children:a});case"redeemed":return e.jsx("h2",{className:"overlay-text expired-text",children:"Redeemed!"});default:return null}},n=()=>{switch(_){case"daily":return"Available every day!";case"weekly":return"Available every week!";case"monthly":return"Available every month!";default:return null}};return e.jsx("li",{className:`cards__item ${l!=="available"?"expired-card":""}`,children:e.jsxs(E,{className:"cards__item__link",to:f,onClick:s=>{m&&s.preventDefault()},children:[e.jsxs("figure",{className:"cards__item__pic-wrap","data-category":y,children:[e.jsx("img",{className:"cards__item__img",alt:j,src:w}),l!=="available"&&e.jsx("div",{className:"card-overlay",children:t()})]}),e.jsxs("div",{className:"cards__item__info",children:[e.jsx("h5",{className:"cards__item__title",children:j}),e.jsx("p",{className:"cards__item__text",children:C})]}),N&&e.jsx("div",{className:"reusable-banner",children:e.jsx("p",{className:"reusable-banner-text",children:n()})})]})})},G=()=>{const{currentUser:x,loading:w}=F(),[C,j]=o.useState(!0),[y,v]=o.useState(null),[i,N]=o.useState([]),[_,l]=o.useState(new Map);o.useEffect(()=>{if(w||!x)return;let a=!1;return(async()=>{j(!0),v(null);try{await new Promise(n=>setTimeout(n,0));const[u,f]=await Promise.all([M(I(k($,"redeemedCoupons"),L("userId","==",x.uid))),M(k($,"coupons"))]);if(a)return;const m=new Map;u.forEach(n=>{const s=n.data();m.set(s.couponId,{redeemedAt:s.redeemedAt,expiresAt:s.expiresAt})});const t=f.docs.map(n=>{const s=n.data();return s.expiration&&typeof s.expiration.toDate!="function"&&(s.expiration.seconds?s.expiration=new S(s.expiration.seconds,s.expiration.nanoseconds):s.expiration=S.fromDate(new Date(s.expiration))),{...s,id:n.id}});l(m),N(t)}catch(u){a||(console.error(u),v("Sorry, we couldn't load coupons right now. Please try again later."))}finally{a||j(!1)}})(),()=>{a=!0}},[w,x]);const{activeCoupons:c,availableCoupons:g,allRedeemedAndCooldown:b}=o.useMemo(()=>{const a=[],d=[],u=[],f=[],m=new Date;for(const t of i){const n=t.expiration&&t.expiration.toDate()<m,s=_.get(t.id);if(s)if(s.expiresAt.toDate()>m&&!n)a.push(t);else if(t.isReusable&&!n){const p=s.redeemedAt.toDate(),r=new Date(p);let h=!0;switch(t.reuseFrequency){case"daily":r.setDate(r.getDate()+1);break;case"weekly":r.setDate(r.getDate()+7);break;case"monthly":r.setMonth(r.getMonth()+1);break;default:h=!1,f.push(t);break}h&&(m>=r?d.push(t):u.push({...t,nextAvailableDate:r}))}else f.push(t);else n||d.push(t)}return d.sort((t,n)=>t.isReusable&&!n.isReusable?-1:!t.isReusable&&n.isReusable?1:0),{activeCoupons:a,availableCoupons:d,allRedeemedAndCooldown:[...u,...f]}},[i,_]);return w||C?e.jsx(P,{}):y?e.jsx("div",{className:"coupons-container",children:e.jsx("p",{children:y})}):e.jsxs("div",{className:"coupons-container",children:[c.length>0&&e.jsxs(e.Fragment,{children:[e.jsx("h1",{className:"coupons-heading",children:"Active Coupons"}),e.jsx("div",{className:"cards__container",children:e.jsx("div",{className:"cards__wrapper",children:e.jsx("ul",{className:"cards__items",children:c.map(a=>e.jsx(A,{...a,redemptionInfo:_.get(a.id)},`active-${a.id}`))})})})]}),e.jsx("h1",{className:"coupons-heading",style:{marginTop:"40px"},children:"Available Coupons"}),e.jsx("div",{className:"cards__container",children:e.jsxs("div",{className:"cards__wrapper",children:[e.jsx("ul",{className:"cards__items",children:g.map(a=>e.jsx(A,{...a},`available-${a.id}`))}),g.length===0&&x&&e.jsx("p",{children:"No new coupons available right now. Check back soon!"})]})}),b.length>0&&e.jsxs(e.Fragment,{children:[e.jsx("h1",{className:"coupons-heading",style:{marginTop:"40px"},children:"Redeemed Coupons"}),e.jsx("div",{className:"cards__container",children:e.jsx("div",{className:"cards__wrapper",children:e.jsx("ul",{className:"cards__items",children:b.map(a=>e.jsx(A,{...a,redemptionInfo:_.get(a.id),nextAvailableDate:a.nextAvailableDate},`redeemed-${a.id}`))})})})]})]})};export{G as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.