// Pie Control: a plain modal (a bottom sheet on phones) for the rocket company. Reached from the NOC tab (pieCard()).
let PIE_CCN=null;
function pieLicCountry(cc){try{PIE_CCN=PIE_CCN&&PIE_CCN.l===LANG?PIE_CCN:{l:LANG,d:new Intl.DisplayNames([LANG],{type:'region'})};return PIE_CCN.d.of(cc)||cc;}catch(e){return cc;}}
function pieCard(){if(!pieOn())return '';const P=S.pie;
return `
🥧 ${tr('pie.pie_in_the_sky')}
${P&&P.co?esc(pieCoName())+' · '+tr('pie.card_live',{n:Object.keys(CRUSTS).reduce((a,id)=>a+pieLive(id),0),q:P.q.length}):tr('pie.card_pitch')}
${pieSpatCard()}
${btn(tr('pie.open_pie_control'),'pieOpen',{},P&&P.co?'sm':'pink sm')}
`;}
const PIE_TABS=['traffic','co','fly','dish','gov','book'];
const pieTag=(t,on)=>`${t} `;
function pieModal(){if(!S||!pieOn())return;const P=S.pie||pieBlank(S.day),can=pieCanBuild(),T=PIE_TABS.includes(UI.pieTab)?UI.pieTab:'traffic';
let h=`${tr('pie.pie_in_the_sky')}
${P.co?esc(pieCoName()):tr('pie.pie_control')} × `;
if(!can)h+=`${tr('pie.why_not_t1')}
`;
if(pieSand())h+=`${tr('pie.sandbox_note',{amt:fmt$(P.book.wouldHave)})}
`;
if(!P.co){h+=`${tr('pie.found_pitch')}
${btn(tr('pie.found_price',{price:fmt$(piePrice(PIE_CO)),mo:fmt$(PIE_CO_MO)}),'pieFound',{},'pink',!can)}
`;openModal(h,'pie');return;}
const tab=(k,l)=>`${l} `;
h+=pieGlobeHtml()+pieSpatCard();
h+=`${tab('traffic',tr('pie_traffic.tab'))}${tab('co',tr('pie.tab_company'))}${tab('fly',tr('pie.tab_launch'))}${tab('dish',tr('pie.tab_dishes'))}${tab('gov',tr('pie_gov.tab'))}${tab('book',tr('pie.tab_founder'))}
`;
const cities=Object.keys(S.pops).sort((a,b)=>CITY[a].name.localeCompare(CITY[b].name)),c=S.pops[UI.pieCity]?UI.pieCity:cities[0];
const sel=`${tr('common.your_pop')}${cities.map(x=>`${esc(CITY[x].name)} `).join('')}
`;
if(T==='traffic')h+=pieTrafficHtml();
else if(T==='co'){
h+=`${tr('pie.running_costs')} ${tr('common.price_mo',{price:fmt$(pieMo())})}
`;
h+=`🏭 ${tr('pie.factory')}
${P.fac?tr('pie.factory_stock',{n:Math.floor(P.stock),m:PIE_FAC_MONTH}):tr('pie.factory_pitch',{m:PIE_FAC_MONTH,mo:fmt$(PIE_FAC_MO)})}
${P.fac?'':`
${btn(tr('pie.build_price',{price:fmt$(piePrice(PIE_FAC))}),'pieFactory',{},'pink sm',!can)}
`}
`;
h+=`🧪 ${tr('pie.rd')}
${Object.keys(PIE_RD).map(k=>`
${tr(PIE_RD[k].n)} ${P.rd[k]?`✓ `:btn(fmt$(piePrice(PIE_RD[k].cost)),'pieRd',{k},'ghost sm',!can)}
`).join('')}
${P.rd.heavy?`
${tr('pie.heavy_tests',{n:P.rd.heavyTests,max:PIE_HEAVY_TESTS})}
`:''}
`;
h+=`🚀 ${tr('pie.pads')}
${P.pads.map(p=>`
${esc(CITY[p.city].name)} · −${pct(pieEq(p.city))} ${p.next>S.day?tr('pie.pad_busy',{d:dateStr(p.next)}):tr('pie.pad_free')}${p.boosters.length?' · '+trn('pie.n_boosters',p.boosters.length):''}
`).join('')||`
${tr('pie.no_pads')}
`}
${sel}
${btn(tr('pie.build_pad_price',{city:esc(CITY[c].name),price:fmt$(piePrice(PIE_PAD))}),'piePad',{city:c},'ghost sm',!can||!pieCoast(c)||P.pads.some(p=>p.city===c)||P.pads.length>=PIE_PAD_MAX)}
${pieCoast(c)?'':`
${tr('pie.why_pad_city')}
`}
`;
h+=`🏷️ ${tr('pie.name')}
${btn(tr('pie.next_name'),'pieName',{},'ghost sm')}
${btn(tr('slots.rename'),'pieRename',{},'ghost sm')}
`;
h+=`${btn(P.safe?tr('pie.safe_on'):tr('pie.safe_off'),'pieSafe',{},P.safe?'red sm':'ghost sm')}
${tr('pie.safe_note')}
`;
}else if(T==='fly'){
let nudge=false;
h+=Object.keys(CRUSTS).map(id=>{const C=CRUSTS[id],cv=pieCov(id);return `${tr(C.n)} ${pieTag(pieBaked(id)?tr('pie.baked'):tr('pie.half_baked'),pieBaked(id))}
${tr('pie.crust_line',{alt:C.alt,n:pieLive(id),full:C.full,q:P.crusts[id].queued,p:pct(cv)})}
${(()=>{const f=pieLive(id)+P.crusts[id].queued>0&&pieLive(id)
${t}`;
if(!nudge&&f.pad&&f.days>PIE_PACE_SLOW&&P.pads.length${tr('pie.pace_pad',{city:esc(CITY[c].name),p:pct(pieEq(c))})}${btn(tr('pie.build_pad_price',{city:esc(CITY[c].name),price:fmt$(piePrice(PIE_PAD))}),'piePad',{city:c},'ghost sm',!can)}
`
:`${tr('pie.pace_pad_none')}
`;}
return x;})()}
${Object.keys(ROCKETS).filter(pieRkOk).map(rk=>{const q=pieQuote(rk,id,{ins:!!UI.pieIns,las:!!UI.pieLas});return btn(`${tr(ROCKETS[rk].n)} ${q.ok?(q.test?tr('pie.test_flight')+' · ':'')+fmt$(q.cost):esc(q.why)} `,'pieLaunch',{rk,cr:id},'ghost sm',!q.ok);}).join('')}
`;}).join('');
h+=`${btn(UI.pieIns?tr('pie.ins_on'):tr('pie.ins_off'),'pieIns',{},UI.pieIns?'green sm':'ghost sm')}${btn(UI.pieLas?tr('pie.las_on'):tr('pie.las_off'),'pieLas',{},UI.pieLas?'green sm':'ghost sm')}
`;
h+=`${tr('pie.stock_line',{n:Math.floor(P.stock)})}
`;
if(P.q.length)h+=`${tr('pie.queue')} `+P.q.slice().sort((a,b)=>a.day-b.day).map(L=>`${esc(tr(ROCKETS[L.rk].n))} → ${esc(tr(CRUSTS[L.cr].n))} ${L.test?tr('pie.test_flight'):L.n} · ${esc(CITY[L.pad].name)} · ${dateStr(L.day)}
`).join('');
}else if(T==='dish'){
h+=`${tr('pie.dish_note')}
`;
// A gentle hint while the sky is nearly empty: dishes cost money every month and earn nothing yet. Building stays open.
{const top=Math.max(...Object.keys(CRUSTS).map(pieCov));if(top${tr('pie.dish_wait',{min:pct(PIE_RAW),p:pct(top),mo:fmt$(PIE_DISH_MO),lo:pct(PIE_PATCHY_LO)})}`;}
h+=P.dishes.map(x=>{const cc=CITY[x.city].cc,ok=pieLicOk(cc);return `📡 ${esc(CITY[x.city].name)} · ${trn('pie.n_antennas',x.ant)}${ok?'':' · '+tr('pie.no_rights')} ${btn(tr('pie.add_antenna',{price:fmt$(piePrice(PIE_ANT))}),'pieAnt',{id:x.id},'ghost sm',!can||x.ant>=PIE_ANT_MAX)}
`;}).join('');
h+=sel+`${btn(tr('pie.build_dish_price',{city:esc(CITY[c].name),price:fmt$(piePrice(PIE_DISH+PIE_ANT))}),'pieDish',{city:c},'ghost sm',!can||P.dishes.some(x=>x.city===c))}
`;
const ccs=[...new Set([...P.dishes.map(x=>CITY[x.city].cc),CITY[c].cc,...Object.keys(P.lic)])];
h+=`${tr('pie.landing_rights')} `+ccs.map(cc=>{const l=P.lic[cc],ok=pieLicOk(cc);
return `${esc(pieLicCountry(cc))}${ok?' · '+tr('pie.lic_until',{d:dateStr(l.until)}):l&&l.state!=='ok'&&l.until>S.day?' · '+tr('pie.lic_refused'):''} ${btn((ok?tr('pie.renew'):tr('pie.buy'))+' '+fmt$(pieLicPrice(cc)),'pieLic',{cc},'ghost sm')}
`;}).join('');
}else if(T==='gov')h+=pieGovHtml();
else{
const F=P.founder;h+=`${tr('pie.promises')} ${tr('pie.promises_line',{n:F.promises,b:F.broken})}
`;
h+=`${tr('pie.spent')} ${fmt$(P.book.spent)}
${tr('pie.earned')} ${fmt$(P.book.earned)}
`;
if(R&&R.pie)h+=`${tr('pie.earned_mo',{amt:fmt$(R.pie.rev),n:R.pie.isl.length})}
`;
if(pieSand())h+=`${tr('pie.would_have')} ${fmt$(P.book.wouldHave)}
`;
h+=`${tr('pie.founder_feed')} `+(F.posts.length?F.posts.map(p=>`${esc(dateStr(p.d))}
${esc(pieFpText(p))}
`).join(''):`${tr('pie.feed_empty')}
`);
}
openModal(h,'pie');{const cv=document.querySelector('#modal .pgcv');if(cv)pieGlobeOpen(cv,pieGlobeData);}}