const app=document.querySelector('#app');
let catalog=[],cart={items:[],addons:[],plan:{servings:2,meals:3,preference:'All'},selected_meals:0,total:0},filters={q:'',category:'All',calories:0};
const money=n=>'$'+Number(n||0).toFixed(2);
const esc=s=>String(s??'').replace(/[&<>"']/g,c=>({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]));
function route(path){history.pushState({},'',path);boot()}
window.onpopstate=boot;
function shell(content,{simple=false}={}){document.body.className=simple?'simple':'';app.innerHTML=`Blue Apron${content}`}
function img(item){return item.image?``:`
Blue Apron
`}
function card(item,weekly=false){return `${img(item)}
`}
async function getCart(){cart=await(await fetch('/api/cart')).json();return cart}
async function getCatalog(params=''){let result=await(await fetch('/api/recipes'+params)).json();catalog=result.recipes;return result}
function privacyBanner(){return ``}
function home(){let picks=catalog.slice(0,4);shell(`
NO SUBSCRIPTION NEEDED
Dinner's Back on the Calendar
Your week has enough on its plate. Let us handle what's on yours.
More Ways to Blue Apron
${[['20 MIN OR LESS','Fast, flavorful recipes ready in 20 minutes or less.'],['ASSEMBLE & BAKE','One-pan meals. Minimal effort, maximum reward.'],['MEAL KITS','Easy recipes. Pre-portioned ingredients.'],['FAMILY STYLE','Mix & match, family-sized mains & sides.']].map((x,i)=>`${x[0]}
`}`)}
async function searchRecipes(e){e.preventDefault();filters.q=recipeSearch.value.trim();let x=await getCatalog('?q='+encodeURIComponent(filters.q));cookbook()}
async function bookFilter(cat){filters.category=cat;await getCatalog(cat==='All'?'':'?category='+encodeURIComponent(cat));cookbook()}
async function clearSearch(){filters={q:'',category:'All',calories:0};await getCatalog();cookbook()}
function detail(item){let ingredients=(item.ingredients||[]).map(x=>`
`)}
async function qty(id,n){cart=await(await fetch('/api/cart/'+id,{method:'PATCH',headers:{'Content-Type':'application/json'},body:JSON.stringify({qty:n})})).json();cartPage()}
async function addAddon(id){let r=await fetch('/api/addons',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({addon_id:id})});let x=await r.json();if(!r.ok)return alert(x.error);cart=x;cartPage()}
async function addonQty(id,qty){let r=await fetch('/api/addons/'+id,{method:'PATCH',headers:{'Content-Type':'application/json'},body:JSON.stringify({qty})});let x=await r.json();if(!r.ok)return alert(x.error);cart=x;cartPage()}
function checkout(){let d=cart.delivery||{};shell(`
Delivery & payment
`)}
async function saveDelivery(e){e.preventDefault();let value=id=>document.getElementById(id).value;let data={name:value('name'),address:value('address'),city:value('city'),state:value('state'),zip:value('zip'),phone:value('phone'),date:value('date')};let r=await fetch('/api/delivery',{method:'PUT',headers:{'Content-Type':'application/json'},body:JSON.stringify(data)});let x=await r.json();if(!r.ok){deliveryError.textContent=x.error;return}cart=x;deliveryError.className='success';deliveryError.textContent='Delivery information saved.'}
async function reviewOrder(){let r=await fetch('/api/checkout/review',{method:'POST'});let x=await r.json();reviewMessage.className=r.ok?'success':'error';reviewMessage.textContent=r.ok?'Ready for final review. No purchase has been submitted.':x.error}
function authPage(kind){let signin=kind==='signin';shell(`
${signin?'Welcome back!':'Create an account'}
${signin?`Don’t have an account? Get started`:`Have an account? Login`}
`,{simple:true})}
async function resetRequest(e){e.preventDefault();let r=await fetch('/api/reset-request',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({email:resetEmail.value})});let x=await r.json();resetMessage.className=r.ok?'notice':'error';resetMessage.textContent=x.message||x.error}
async function accountPage(){let s=await(await fetch('/api/auth/session')).json();if(!s.authenticated){authPage('signin');authMessage.textContent='Sign in to manage your deliveries.';return}let r=await fetch('/api/subscription');let sub=await r.json();shell(`
${cart.plan.servings} people · ${cart.plan.meals} meals
Order history
September 14, 2026Delivered · ${cart.plan.meals} meals · ${money(cart.total||78.42)}View recipes
`)}
async function subAction(action){let r=await fetch('/api/subscription',{method:'PATCH',headers:{'Content-Type':'application/json'},body:JSON.stringify({action})});let x=await r.json();if(!r.ok)return alert(x.error);accountPage()}
async function editSub(kind){let value=prompt(kind==='date'?'Enter a new delivery date':'Enter a new delivery address');if(!value)return;let r=await fetch('/api/subscription',{method:'PATCH',headers:{'Content-Type':'application/json'},body:JSON.stringify({action:kind,[kind]:value})});let x=await r.json();if(!r.ok)return alert(x.error);accountPage()}
function confirmCancel(){if(confirm('Cancel your local offline subscription? This has no effect on Blue Apron.'))subAction('cancel')}
async function logout(){await fetch('/api/auth/signout',{method:'POST'});route('/')}
function infoPage(kind){if(kind==='gifts'){shell(`
Give a Blue Apron gift card
A gift that never expires. Customized to their favorite Blue Apron meal.
${['A delicious week','Meals made easy','Fresh ingredients','Cook together'].map(x=>`Blue Apron${x}`).join('')}
FAQs
For more information on gift cards, view our full list of FAQs here.
But with everything life throws at you during the week, getting great meals on the table is not always easy.
You need a helping hand in the kitchen. That’s where Blue Apron comes in.
${privacyBanner()}`);return}let content={help:['Help Center & FAQ','How can we help?','Find guidance for orders, deliveries, account access, failed actions, and subscription changes without exposing private account data.']}[kind];shell(`