// Teleports and satellite circuits on screen (#83): a POP's section, the order sheet, a circuit's panel. const satLogo=P=>``; // Round trip over bird id between a and b, in whole ms. const satRtt=(id,a,b)=>Math.round(2*satLat({bird:id,km:satKm(id,a,b)})); const satModalHead=(eye,title)=>`
🛰️ ${eye}

${title}

`; // In a city's facts: its own users' traffic rides a satellite today (R.satU), and they count part of the service as bad (sfOf, R.latF). const satFact=c=>R&&R.latF&&R.latF[c]<1&&R.satU&&R.satU[c]>1e-3?`🛰️ ${tr('satellites.users_ride_a_satellite')}`:''; // The birds a dish at c sees, in words. const satInView=c=>{const ids=teleSees(c);return ids.length?tr('satellites.in_view_list',{list:ids.map(id=>`${esc(SATS[id].n)} (${tr(SATS[id].sea)})`).join(', ')}):tr('satellites.no_bird_sees_this_city');}; // In a POP's Infra tab: its teleport (build it, what it sees, its circuits), and the teleports that can reach it. function teleHtml(c){if(pzOn())return '';const t=teleOf(c),from=satFrom(c),open=lvl()>=SAT_ORB.geo.lv;if(!t&&!open&&!from.length)return ''; let h=`

🛰️ ${tr('satellites.satellites')}

`; if(t){const out=S.links.filter(l=>l.kind==='sat'&&l.a===c),cf=UI.confirm==='tele:'+c,meo=teleSees(c).some(id=>SATS[id].orb==='meo'); h+=`
${tr('satellites.teleport')}${t>1?tr('satellites.geo_dishes_and_meo_tracking'):tr('satellites.geo_dishes')} · ${tr('common.price_mo',{price:fmt$(TPORT_MO+(t>1?TRK_MO:0))})}
${satInView(c)}
`; for(const l of out){const u=capOf(l)?(R.load[l.id]||0)/capOf(l):0; h+=`
→ ${CITY[l.b].name}${l.standby?' '+tr('common.standby')+'':''}
${capName(l)} · ${esc(satName(l))}${l.ready>S.day?' · '+tr('city_panel.ready_in_n_d',{n:l.ready-S.day}):''} · ${tr('common.price_mo',{price:fmt$(l.mo)})}
${btn(tr('common.open'),'selLink',{id:l.id},'ghost sm')}
`;} h+=`
${btn(tr('satellites.rent_satellite_capacity'),'satOpen',{a:c},'pink sm')}${t===1&&lvl()>=SAT_ORB.meo.lv&&meo?btn(tr('satellites.add_tracking_antennas_cost_mo',{cost:fmt$(TRK_COST),mo:fmt$(TRK_MO)}),'teleTrk',{city:c},'sm',S.cash ${t===1&&lvl()${tr('satellites.meo_comes_at_level',{lv:SAT_ORB.meo.lv})}

`:''}`;} // Before the build: what its dishes would see and reach at your level, and no button to a teleport that reaches nothing. else if(open){const why=teleWhy(c),n=why?0:satTargets(c).filter(x=>satBirds(c,x).some(id=>lvl()>=SAT_ORB[SATS[id].orb].lv)).length; h+=`

${tr('satellites.a_teleport_is_a_satellite_gateway')}

${satInView(c)}${why?'':' '+trn('satellites.n_places_in_reach',n)}
${why&&teleSees(c).length?`
${esc(why)}
`:''}
${btn(tr('satellites.build_a_teleport_cost_mo',{cost:fmt$(TPORT_COST),mo:fmt$(TPORT_MO)}),'teleBuild',{city:c},'sm',S.cash`;} if(from.length)h+=satReach(c,from); return h+`
`;} // Teleports that reach city c: a button each, to the order sheet. const satReach=(c,from)=>`

${tr('satellites.reachable_from_your_teleports',{city:CITY[c].name})}

${from.slice(0,4).map(a=>btn(tr('fiber_modals.from_city',{city:CITY[a].name}),'satOpen',{a,b:c},'ghost sm')).join('')}
`; // Whether a satellite can get to city c: a teleport of yours reaches it, or (once teleports are on sale) a bird you may // rent from sees it. Never in a puzzle, as the rest of the satellites. const satCan=c=>!pzOn()&&(satFrom(c).length>0||lvl()>=SAT_ORB.geo.lv&&satRemote(c)&&teleSees(c).some(id=>lvl()>=SAT_ORB[SATS[id].orb].lv)); // Under the link window's "no subsea cable lands here" (#321): the teleports that reach c, a button each to the order // sheet, or where to build one. const satNoCable=c=>{const from=satFrom(c);return from.length?satReach(c,from):`

${tr('satellites.no_teleport_reaches_city',{city:CITY[c].name})}

`;}; // A city without a POP of yours, that a teleport of yours reaches: a POP and a circuit in one go. function satNoPop(c){if(pzOn()||S.pops[c])return '';const from=satFrom(c);if(!from.length)return ''; return `

🛰️ ${tr('satellites.by_satellite')}

${tr('satellites.no_cable_needed_a_pop_here',{city:CITY[c].name})}

${satReach(c,from)}
`;} // The order sheet: from teleport a to b. With one end missing it lists the choices first. function satModal(a,b){a=isCity(a)?a:null;b=isCity(b)?b:null; if(!a&&b){const fr=satFrom(b);if(fr.length===1)a=fr[0]; else{openModal(satModalHead(tr('satellites.satellite_capacity'),tr('fiber_modals.to_city',{city:CITY[b].name}))+(fr.length?fr.map(x=>`
${CITY[x].name}
${tr('satellites.teleport')} · ${trn('satellites.n_birds_see_both',satBirds(x,b).length)}
${btn(tr('satellites.choose'),'satOpen',{a:x,b},'sm')}
`).join(''):`

${tr('satellites.no_teleport_reaches_city',{city:CITY[b].name})}

`));return;}} if(!a||!teleOf(a))return; if(!b){const ts=satTargets(a),mine=ts.filter(x=>S.pops[x]),more=ts.filter(x=>!S.pops[x]).slice(0,12); const row=x=>`
${CITY[x].name}
${S.pops[x]?tr('common.your_pop'):tr('satellites.new_pop_cost',{cost:fmt$(newPopCost(x))})} · ${kmBetween(a,x).toLocaleString()} km · ${trn('satellites.n_birds_see_both',satBirds(a,x).length)}
${btn(tr('satellites.choose'),'satOpen',{a,b:x},'sm')}
`; openModal(satModalHead(tr('satellites.satellite_capacity'),tr('fiber_modals.from_city',{city:CITY[a].name}))+`

${tr('satellites.who_a_satellite_serves')}

` +(mine.length?`

${tr('satellites.your_pops_in_view')}

`+mine.map(row).join(''):'')+(more.length?`

${tr('satellites.more_islands_and_towns')}

`+more.map(row).join(''):'')+(ts.length?'':`

${tr('satellites.nothing_in_view_from_here')}

`));return;} const pop=S.pops[b]?0:newPopCost(b),sib=pairLinks(a,b).some(l=>l.kind!=='sat'),birds=satBirds(a,b); const rows=birds.map(id=>{const B=SATS[id],O=SAT_ORB[B.orb],P=SAT_PROV[B.p],why=satWhy(a,b,id); return `
${satLogo(P)}
${esc(B.n)} ${O.n}
${esc(P.n)} · ${tr(B.sea)} · ${tr('satellites.ms_round_trip',{ms:satRtt(id,a,b)})} · ${trn('satellites.up_in_n_days',leadDays(O.days))}
${esc(P.blurb)}
${why?`
${esc(why)}
`:''}
${O.g.map(g=>btn(`${fmtCap(g)} ${tr('common.price_mo',{price:fmt$(satMo(id,g))})}`,'buySat',{a,b,bird:id,g},'sm',!!why||S.cash
`;}).join(''); openModal(satModalHead(tr('satellites.satellite_capacity'),`${CITY[a].name} → ${CITY[b].name}`)+`

${tr('satellites.users_notice_geo_meo')}

` +(pop?`

${tr('satellites.no_pop_there_yet',{city:CITY[b].name,cost:fmt$(pop)})}

`:'')+(sib?`

${tr('satellites.it_waits_on_standby')}

`:'') +(rows||`

${tr('satellites.no_bird_sees_both',{a:CITY[a].name,b:CITY[b].name})}

`));} // A circuit's panel: what it carries, its latency and rent, a size up or down, standby and the metric, cancel. function satPanel(l){const B=SATS[l.bird],O=SAT_ORB[B.orb],P=SAT_PROV[B.p],load=R.load[l.id]||0,cap=capOf(l),u=cap?load/cap:0,ms=satLat(l); let h=`
🛰️ ${esc(B.n)} · ${O.n} · ${esc(P.n)}

${lkEnds(l)}

`; // A closed border takes a circuit dark like any link between the two countries (linkCut): say so, as the others do (#329). h+=borderSection(l); if(l.ready>S.day){const tot=Math.max(1,l.ready-l.day),done=clamp((S.day-l.day)/tot,0,1); h+=`

${tr('satellites.being_commissioned')}

${tr('satellites.pointing_dishes_and_testing')}${tr('common.n_days_left',{n:l.ready-S.day})}
`;} h+=`

${tr('common.capacity')}

${capName(l)}${l.standby?' · '+tr('common.standby'):''}${fmtG(load)} / ${fmtG(cap)}
${tr('common.latency')}${tr('satellites.ms_one_way_rtt',{ms:Math.round(ms),rtt:Math.round(2*ms)})}
${tr('satellites.radio_path')}${l.km.toLocaleString()} km
${tr('satellites.rent')}${tr('common.price_mo',{price:fmt$(l.mo)})}

${B.orb==='geo'?tr('satellites.geo_users_notice',{p:pct(O.loss)}):tr('satellites.meo_users_notice',{p:pct(O.loss)})} ${esc(P.blurb)}

${O.g.filter(g=>g!==l.g).map(g=>btn(`${g>l.g?tr('satellites.grow_to',{cap:fmtCap(g)}):tr('satellites.shrink_to',{cap:fmtCap(g)})} ${tr('common.price_mo',{price:fmt$(satMo(l.bird,g))})}`,'satSize',{id:l.id,g},'sm',g>l.g&&S.cash
${trafficSection(l)}${metricSection(l)}
${btn(UI.confirm===l.id?tr('common.really_cancel'):tr('common.cancel_circuit'),'rmLink',{id:l.id},'ghost sm')}
`; return h;}