import {activities,result,target,matches} from './studio-data.mjs'; const key='ft-studio-completed-v15';let done=new Set();try{const raw=JSON.parse(sessionStorage.getItem(key)||'[]');if(Array.isArray(raw))done=new Set(raw.filter(x=>activities.some(a=>a.id===x)));}catch{} const save=()=>{try{sessionStorage.setItem(key,JSON.stringify([...done]));}catch{}}; const $=s=>document.querySelector(s);function paintProgress(){if($('#st-count'))$('#st-count').textContent=done.size;if($('#st-progress'))$('#st-progress').value=done.size;document.querySelectorAll('[data-completed]').forEach(x=>{const v=done.has(x.dataset.completed);x.textContent=v?'★ Completed':'Ready to explore';x.classList.toggle('is-done',v);});}paintProgress();$('#st-clear')?.addEventListener('click',()=>{done.clear();save();paintProgress();}); const shell=$('[data-mission]');if(shell){const a=activities.find(x=>x.id===shell.dataset.mission);const inputs=[...shell.querySelectorAll('[data-input]')];const values=()=>inputs.map(x=>Number(x.value));const message=$('#st-feedback');const fmt=(c,v)=>c.options?c.options[v]:`${v}${c.unit?' '+c.unit:''}`; function svgNode(tag,attrs,text){const n=document.createElementNS('http://www.w3.org/2000/svg',tag);for(const [k,v] of Object.entries(attrs))n.setAttribute(k,v);if(text!==undefined)n.textContent=text;return n;} function chart(vals){const c=a.controls[0],box=$('#st-graph'),g=$('#st-plot');if(c.options){box.hidden=true;return;}let pts=Array.from({length:41},(_,i)=>{let x=c.min+(c.max-c.min)*i/40;let snap=Math.min(c.max,c.min+Math.round((x-c.min)/c.step)*c.step);return [snap,result(a,[snap,...vals.slice(1)])[0]];});if(pts.some(p=>typeof p[1]!=='number'||!Number.isFinite(p[1]))){box.hidden=true;return;}box.hidden=false;g.replaceChildren();let min=Math.min(...pts.map(p=>p[1])),max=Math.max(...pts.map(p=>p[1]));if(min===max){min-=1;max+=1;}const X=x=>40+(x-c.min)/(c.max-c.min)*255,Y=y=>125-(y-min)/(max-min)*105;for(let j=0;j<=4;j++){let y=min+(max-min)*j/4;g.append(svgNode('line',{x1:40,x2:295,y1:Y(y),y2:Y(y),class:'st-gridline'}),svgNode('text',{x:34,y:Y(y)+4,'text-anchor':'end'},Number(y.toFixed(1))));}g.append(svgNode('path',{d:'M40 20 V125 H295',class:'st-axis'}),svgNode('polyline',{points:pts.map(([x,y])=>`${X(x)},${Y(y)}`).join(' '),class:'st-curve'}),svgNode('circle',{cx:X(vals[0]),cy:Y(result(a,vals)[0]),r:5,class:'st-dot'}),svgNode('text',{x:40,y:143},c.min),svgNode('text',{x:295,y:143,'text-anchor':'end'},c.max));$('#st-graph-caption').textContent=`${a.output[0]} vs ${c.label}; other inputs held fixed. Samples follow the allowed input steps.`;} function update(){const vals=values();vals.forEach((v,i)=>$('#st-val-'+i).textContent=fmt(a.controls[i],v));result(a,vals).forEach((v,i)=>$('#st-out-'+i).textContent=v);chart(vals);message.textContent=done.has(a.id)?'★ Completed earlier in this tab. Try another solution.':'Explore, then check your answer.';message.className='';} inputs.forEach(x=>{x.addEventListener('input',update);x.addEventListener('change',update);});$('#st-reset').addEventListener('click',()=>{inputs.forEach((x,i)=>x.value=a.controls[i].start);update();});$('#st-check').addEventListener('click',()=>{if(matches(a,values())){done.add(a.id);save();message.textContent='★ Mission accomplished! You reached the target.';message.className='st-success';}else{message.textContent='Not quite yet. Compare your results with the target and try again.';message.className='st-retry';}});update();}