/* ---------- architect runs (prototype): the card, the panels, the link picker, the results and the list rows ---------- */ const arDarkNow=c=>!!((UI.arShow&&UI.arShow.dark.includes(c))||(UI.arFail&&UI.arFail.dark.includes(c))); const arHide=c=>arOn()&&!arKind(c); function arRender(el){const A=S.ar,run=arRun(),b=arBrief(),e=arEval(),sh=UI.arShow,f=UI.arFail,n=run.briefs.length,nm=c=>esc(CITY[c].name); let h=`
๐Ÿ—๏ธ ${esc(run.n)}

${tr('architect.brief_n_of_m',{n:A.bi+1,m:n})}

${btn('โ˜ฐ','puzzleMenu',{},'ghost sm')}
`; h+=`
${run.briefs.map((x,i)=>``).join('')}
`; h+=`

${esc(b.t)}

`; h+=`
${e.sites.map(s=>`${s.hq?'๐Ÿ ':s.gold?'๐Ÿšจ':s.ok?'โœ“':'โ—‹'} ${nm(s.c)}${s.hq?'':` ${s.ok&&!s.full?arG(s.got)+'/':''}${arG(s.dem)}`}`).join('')}
`; if(A.gold.length)h+=`
๐Ÿšจ ${tr('architect.gold_must_survive',{list:A.gold.map(nm).join(', ')})}
`; for(const r of e.rings)h+=`
๐Ÿ’ ${tr('architect.ring_list',{list:r.cs.map(nm).join(' ยท ')})} ${r.ok?'โœ“':''}
`; if(e.lat)h+=`
โฑ ${nm(e.lat.a)} โ‡„ ${nm(e.lat.b)}: ${e.lat.ms==null?tr('common.no_path'):e.lat.ms.toFixed(1)+' ms'} / ${e.lat.cap} ms
`; for(const c of S.pz.avoid)h+=`
๐Ÿšซ ${tr('architect.closed_city',{city:nm(c)})}
`; const T=e.tubes;if(T.max!=null)h+=`
${tr('architect.tubes_this_brief')}${'โ—'.repeat(Math.min(T.u,T.max))}${'โ—‹'.repeat(Math.max(0,T.max-T.u))}${T.over?` ${tr('architect.n_over_plan',{n:T.over})}`:''}
`; h+=`
${tr('architect.spent_x_of_y',{x:fmt$(e.spend),y:fmt$(e.budget)})}${e.cheap?'โญ':'โ˜†'}
${A.sunk?`
${tr('architect.x_paid_for_and_gone',{x:fmt$(A.sunk)})}
`:''}
`; if(sh)h+=`
๐Ÿšœ ${tr('architect.cut_n_of_m',{n:sh.i+1,m:sh.units.length})}${sh.dark.length?`
${tr('architect.dark_list',{list:sh.dark.map(nm).join(', ')})}`:''}
`; else if(f)h+=`
๐Ÿ’ฅ ${f.gold.length?tr('architect.cut_on_x_cuts_off_y',{x:pzUnitName(f.u),y:f.gold.map(nm).join(', ')}):tr('architect.cut_on_x_no_room',{x:pzUnitName(f.u),y:f.short.map(nm).join(', ')})}
${tr('architect.fix_it_and_send_it_again')}
`; else if(e.passed)h+=`
โœ… ${tr('architect.review_passed')}
`; else if(!e.ready)h+=`
${esc(e.why)}
`; else h+=`
${A.gold.length?tr('architect.ready_send_the_backhoe'):tr('architect.ready_no_gold_yet')}
`; const main=sh?btn('โฉ '+tr('architect.skip'),'pzArSkip',{},'big'):e.passed?btn(arLast()?tr('architect.finish_the_run')+' ๐ŸŽ‰':tr('architect.next_brief')+' โ†’','pzArNext',{},'pink big'):btn('โ–ถ '+tr('architect.send_the_backhoe'),'pzArGo',{},'pink big',!e.ready); h+=`
${main}${btn('โ†ถ '+tr('puzzles.undo'),'pzUndo',{},'ghost sm',!!sh||!(UI.pzHist&&UI.pzHist.length))}${btn(tr('architect.reset_brief'),'pzReset',{},'ghost sm',!!sh)}${btn(UI.arAll?tr('architect.hide_other_cities'):tr('architect.show_all_cities'),'pzArAll',{},'ghost sm')}
`; if(h!==UI._pzh){UI._pzh=h;morph(el,h);}} function arPanel(sel){const A=S.ar,head=(eb,name,facts)=>`
${eb}

${name}

${facts?`
${facts}
`:''}
`; const old=x=>(x.br|0)${tr('architect.built_in_an_earlier_brief')}

`:''; if(sel.type==='city'){const c=sel.id,k=pzKind(c),p=S.pops[c];if(!k)return ''; const tg=arTags(c),eb=c===A.hq?'๐Ÿ  '+tr('architect.hq'):A.gold.includes(c)?'๐Ÿšจ '+tr('architect.gold_site'):k==='goal'?'๐ŸŽฏ '+tr('architect.site'):k==='via'&&tg.length?'๐Ÿ”œ '+tr('architect.joins_in_brief_n',{n:tg[0].n}):k==='via'?'๐Ÿชจ '+tr('puzzles.stepping_stone_optional'):k==='avoid'?'๐Ÿšซ '+tr('architect.closed'):'๐Ÿ“ '+tr('puzzles.not_in_the_plan'); let h=head(eb,esc(CITY[c].name),p?`${tr('common.your_pop')}${tr('puzzles.price_per_year',{price:fmt$(arPopCost(c))})}`:'')+'
'; if(c===A.hq)h+=`

${tr('architect.hq_lead')}

`;else if(A.gold.includes(c))h+=`

${tr('architect.gold_lead')}

`; if(tg.length)h+=`
${tr('architect.coming_up')}
${tg.map(t=>`
${t.n} ${esc(arSum(arRun().briefs[t.n-1]))}
`).join('')}
`; if(k==='avoid'&&!p)return h+`

${tr('puzzles.not_this_one_find_a_way_around')}

`; if(!p){const pc=pzPopCost(c)+pzRtCost(0,1);return h+`
${btn(tr('puzzles.open_a_pop_cost',{cost:fmt$(pc)}),'pzPop',{city:c},'pink big')}

${tr('architect.or_drag_a_tube_here')}

`;} if(k==='avoid')h+=`

${tr('architect.closed_lead')}

`; {const G=RG[p.rgen||0],cap=nodeCap(c),ld=R.nodeLoad[c]||0;h+=`
${tr('architect.router_x',{x:esc(G.n)})}${pzRtCost(p.rgen||0,1)?tr('puzzles.price_per_year',{price:fmt$(pzRtCost(p.rgen||0,1))}):tr('puzzles.free')}
${tr('architect.router_follows')} ยท ${fmtG(ld)} / ${fmtG(cap)}
`;} const ls=S.links.filter(l=>l.a===c||l.b===c); h+=`

${tr('common.links')}

`; if(!ls.length)h+=`

${tr('puzzles.drag_from_this_pop_onto_another')}

`; for(const l of ls){const o=l.a===c?l.b:l.a;h+=`
โ†’ ${esc(CITY[o].name)}
${pzTrench(l)} ยท ${linkLat(l).toFixed(1)} ms ยท ${tr('puzzles.price_per_year',{price:fmt$(arLinkCost(l))})}
${btn(tr('common.open'),'selLink',{id:l.id},'ghost sm')}
`;} if(k!=='avoid'){const others=[...S.pz.cities,...S.pz.via].filter(x=>x!==c&&!S.pz.avoid.includes(x));if(others.length)h+=`

${tr('architect.lay_a_tube_to')}

${others.map(x=>btn(esc(CITY[x].name),'pzLinkTo',{a:c,b:x},S.pops[x]?'':'ghost')).join('')}
`;} if(c!==A.hq)h+=old(p)+`
${btn(tr('puzzles.remove_this_pop'),'pzRmPop',{city:c},'red sm')}
`; return h+'';} const l=S.links.find(x=>x.id===sel.id);if(!l)return ''; let h=head(l.cable?esc(CAB[l.cable].name):tr('common.overland'),`${esc(CITY[l.a].name)} โ‡„ ${esc(CITY[l.b].name)}`,`${l.km.toLocaleString()} km${linkLat(l).toFixed(1)} ms${tr('puzzles.price_per_year',{price:fmt$(arLinkCost(l))})}`)+'
'; h+=`

${pzTrench(l)}${l.cable?' ยท '+esc(CAB[l.cable].blurb):''}

`; {const u=capOf(l)?(R.load[l.id]||0)/capOf(l):0;h+=`
${tr('puzzles.this_link')}${pzLoadTxt(l)}
`;} h+=`

${tr('architect.size')}

${AR_TIERS.map(i=>{const r={km:l.km,sub:l.sub,f:l.mult||1},why=subWaveWhy(i,l.km,l.sub);return btn(`${LT[i].n} ${why?esc(why):tr('puzzles.price_per_year',{price:fmt$(pzMo(r,i,false)*PZ_YEAR)})}`,'pzTier',{id:l.id,tier:i},l.tier===i?'pink':'ghost',!!why||l.tier===i);}).join('')}
${arAge(l)${tr('architect.changing_an_old_tube')}

`:''}`; h+=`

${tr('puzzles.add_another_link_here')}

${btn(l.cable?tr('puzzles.another_wave_on_this_cable'):tr('puzzles.same_trench_cheaper'),'pzAddPar',{id:l.id,div:0},'sm')}${btn(l.cable?tr('puzzles.a_different_route'):tr('puzzles.a_diverse_route'),'pzAddPar',{id:l.id,div:1},'pink sm')}
`; return h+old(l)+`
${btn(tr('puzzles.remove_this_link'),'pzRmLink',{id:l.id},'red sm')}
`;} // The tube picker: overland or one of the real cables, a diverse trench when there is one already, and one price. function arLinkModal(a,b,keep){const kb=pzKind(b); if(!kb||kb==='avoid'||!pzKind(a)){toast(kb==='avoid'?tr('puzzles.not_this_one_find_a_way_around'):tr('puzzles.not_a_city_you_can_build_in'),'bad');sfx('err');return;} if(!keep){UI.pzVia=null;UI.pzDiv=true;UI.pzTrench=null;}UI.pending={a,b};const rs=pzRoutes(a,b),cost1=x=>pzLinkCost(x,AR_TIER,false); const head=`
${kmBetween(a,b).toLocaleString()} km

${esc(CITY[a].name)} โ†’ ${esc(CITY[b].name)}

`; if(!rs.length){openModal(head+`

${tr('puzzles.no_cable_lands_in_both_try',{a:esc(CITY[a].name),b:esc(CITY[b].name)})}

`);return;} const r=rs.find(x=>x.via===UI.pzVia)||rs.slice().sort((x,y)=>cost1(x)-cost1(y))[0];UI.pzVia=r.via; const pl=pairLinks(a,b),land=!r.sub&&pl.some(l=>!l.cable),div=land&&UI.pzDiv!==false,pop=S.pops[b]?0:pzPopCost(b)+pzRtCost(0,1); let h=head; if(pl.length)h+=`

${tr('puzzles.already_here_list',{list:[...new Set(pl.map(pzTrench))].join(', ')})}

`; h+=`

${tr('common.route')}

${rs.map(x=>btn(`${x.cab?esc(CAB[x.cab].name):tr('common.overland')} ${x.ms.toFixed(1)} ms ยท ${tr('architect.from_x',{x:fmt$(pzLinkCost(x,0,false))})}${pl.some(l=>(l.cable||'land')===x.via)?' ยท โœ“':''}`,'pzVia',{v:x.via},x.via===r.via?'pink':'ghost')).join('')}
`; if(r.cab)h+=`

${esc(CAB[r.cab].blurb)}${pl.some(l=>l.cable===r.cab)?' '+tr('puzzles.same_cable_same_anchor'):''}

`; if(land)h+=`
${btn(tr('puzzles.diverse_route_40_survives_a_backhoe'),'pzDiv',{v:1},div?'pink':'ghost')}${btn(tr('puzzles.same_trench_cheaper_one_backhoe_cuts'),'pzDiv',{v:0},div?'ghost':'pink')}
`; if(pop)h+=`

${tr('puzzles.includes_a_new_pop_in_city',{city:esc(CITY[b].name),price:fmt$(pop)})}

`; const T=arTubes();if(T.max!=null&&T.u>=T.max)h+=`

${tr('architect.this_tube_is_over_the_plan')}

`; h+=`

${tr('architect.size')}

${AR_TIERS.map(i=>{const why=subWaveWhy(i,r.km,r.sub),rt=arRouterDelta(a,i)+(S.pops[b]?arRouterDelta(b,i):arRouterDelta(b,i)),c=pzLinkCost(r,i,div); return ``;}).join('')}
`; openModal(h);} // ---------- results ---------- const arLine=st=>st>=3?tr('architect.line_3'):st===2?tr('architect.line_2'):tr('architect.line_1'); function arResults(done){const A=S.ar,run=arRun(),sp=arSpend(),br=done?run.briefs.length:A.bi,st=done?arStars(sp):0,sh=arShape();arRecord(st,br,sp); const nx=AR_RUNS[AR_RUNS.indexOf(run)+1]||null,stars=[0,1,2].map(i=>`โญ`).join(''); openModal(`
${st>=3?pzConfetti():''}
๐Ÿ—๏ธ ${done?tr('architect.run_complete'):tr('architect.run_over')}

${esc(run.n)}

${stars}

${done?arLine(st):tr('architect.the_cfo_pulled_the_plug')}

${done?'โญ':'โ˜†'} ${tr('architect.briefs_done')}${br} / ${run.briefs.length}
${arOver()?'โ˜†':'โญ'} ${tr('architect.tubes_over_plan')}${arOver()}
${sp<=run.budget+1e-6?'โญ':'โ˜†'} ${tr('architect.spent')}${fmt$(sp)} / ${fmt$(run.budget)}
${sh?`
${tr('architect.your_shape')}${sh.i} ${esc(sh.n)}
`:''}
${nx&&done?btn(tr('architect.next_run')+' โ†’','pzArStart',{id:nx.id},'pink big'):btn(tr('puzzles.more_puzzles'),'puzzleMenu',{},'pink big')}${btn(tr('architect.try_again'),'pzRetry',{},'')}${done?btn(tr('common.share_2cd9'),'pzShare',{},'ghost'):btn(tr('architect.keep_going'),'closeModal',{},'ghost')}
`); for(let i=0;i{sfx('star');haptic(15);},i*450+150);if(st>=3)setTimeout(()=>sfx('fanfare'),3*450+250);} // ---------- the list ---------- function arMenuRows(){const p=arLoad();let h=`

๐Ÿ—๏ธ ${tr('architect.architect_runs')} ${tr('architect.new')}

${tr('architect.runs_lead')}

`; for(const r of AR_RUNS){const x=p.runs[r.id]; h+=`
${esc(r.n)} ${pzStarStr(x?x.st:0)}
${esc(r.j)}
${tr('architect.n_briefs',{n:r.briefs.length})} ยท ๐Ÿ  ${esc(CITY[r.hq].name)}${' ยท '+tr('puzzles.budget_x',{x:fmt$(r.budget)})}${x&&x.sp!=null?' ยท '+tr('puzzles.best_x',{x:fmt$(x.sp)}):x&&x.br?' ยท '+tr('architect.best_n_briefs',{n:x.br}):''}
${btn(tr('common.play'),'pzArStart',{id:r.id},x&&x.st>=3?'ghost':'pink')}
`;} return h;}