import{$,api,state,escape,toast}from'./common.js'; let view='outer',selected='occipital',active=[]; const colors={teal:'#4e927b',amber:'#d8a451',purple:'#9b86bb',blue:'#7e9fbb',coral:'#d87860',pink:'#bc8299'}; const positions={cortex:[308,57],frontal:[136,145],parietal:[379,111],occipital:[496,196],temporal:[291,247],motor:[266,113],sensory:[303,111],association:[381,171],broca:[179,207],wernicke:[388,231],angular:[445,163],hippocampus:[345,251],amygdala:[262,267],limbic:[297,91],thalamus:[323,201],basal:[236,174],callosum:[324,126],cerebellum:[467,321],brainstem:[341,318],medulla:[368,371],ras:[343,276],pons:[376,313],hypothalamus:[299,235],pituitary:[271,299],pineal:[371,198],chiasm:[252,239],ventricles:[282,165]}; const cortexOutline='M83 266C52 255 35 231 42 200C24 180 37 137 64 123C67 91 94 69 128 69C155 42 190 45 216 48C247 27 281 32 311 40C350 25 394 44 411 62C449 53 485 78 496 99C532 111 552 144 547 174C568 204 549 238 525 250C505 274 472 286 439 279C413 295 376 285 352 275C313 293 270 295 238 282C198 296 165 283 142 275C119 279 100 276 83 266Z'; const folds=['M77 127C108 110 112 147 139 135S170 101 183 123S168 172 196 171','M67 183C89 169 91 203 117 194S141 158 159 177S145 225 175 216','M99 238C132 223 128 250 158 247S185 236 204 253','M144 84C154 109 187 75 205 100S220 131 238 108','M204 60C207 84 246 70 250 96S221 143 241 158','M274 57C284 74 282 91 269 111S258 145 276 163','M312 62C333 83 309 95 326 117S353 112 359 139','M365 71C378 92 406 72 419 99S397 125 426 143','M449 109C444 137 478 126 490 152S468 180 496 197','M504 221C487 207 465 232 443 217S429 187 402 198','M329 156C320 177 355 180 355 204S331 230 362 243','M222 214C239 197 248 225 271 214S302 197 304 225','M270 260C295 244 302 272 326 261','M199 197C229 182 244 194 258 181','M369 253C388 244 407 264 428 251','M113 97C126 115 136 95 149 116']; function illustration(v='outer',interactive=true){const prefix=interactive?'diagram':'hero';const picked=id=>selected===id||active.includes(id);const fill=(id,base)=>interactive&&picked(id)?'#d2ddc2':base;const lobes=v==='outer'?``:``;const parts=state.anatomy.filter(p=>p.view===v);return `${[0,1,2,3,4,5].map(i=>``).join('')}${lobes}${folds.map(d=>``).join('')}${interactive?parts.map(p=>{const on=state.modules[p.id]!==false,[x,y]=positions[p.id],isSelected=selected===p.id;return `${p.number}`}).join(''):''}${interactive?'FRONTBACK':''}`} function map(){$('#brain-map').innerHTML=illustration(view);const hero=$('#hero-brain');if(hero&&!hero.firstChild)hero.innerHTML=illustration('outer',false);document.querySelectorAll('#brain-map .brain-point').forEach(e=>{const choose=()=>{selected=e.dataset.part;detail();map()};e.onclick=choose;e.onkeydown=ev=>{if(ev.key==='Enter'||ev.key===' '){ev.preventDefault();choose()}}})} function detail(){const p=state.anatomy.find(p=>p.id===selected);if(!p)return;$('#brain-detail').innerHTML=`${p.number.toString().padStart(2,'0')} ยท ${escape(p.name)}

${escape(p.biology)}

In this lab: ${escape(p.analogy)}

`} const labels={occipital:['Vision','Analyze images and sketches'],hippocampus:['Learning & memories','Update new memories and neural weights'],motor:['Movement','Issue motion and drawing commands'],wernicke:['Understanding','Interpret spoken and written hints'],basal:['Action selection','Allow an action to proceed'],frontal:['Planning','Choose and organize goals'],association:['Integration','Combine memories and hints'],temporal:['Audio input','Recognize recorded speech'],broca:['Language output','Speak an answer aloud'],cerebellum:['Coordination','Smooth paddle commands'],pineal:['Night signal','Reserved for a sleep exhibit'],hypothalamus:['Internal needs','Reserved for a homeostasis exhibit'],amygdala:['Threat response','Reserved for a threat-learning exhibit'],ras:['Alertness','Let the racer and arcade act'],thalamus:['Sensory routing','Route image input to recognition'],callosum:['Communication','Reserved for a split-brain exhibit']}; function switches(){const render=ids=>ids.map(id=>{const p=state.anatomy.find(p=>p.id===id);return `
${labels[id][0]}${labels[id][1]}
`}).join('');$('#switches').innerHTML=render(['occipital','hippocampus','motor','wernicke','basal']);$('#extra-switches').innerHTML=render(Object.keys(labels).filter(k=>!['occipital','hippocampus','motor','wernicke','basal'].includes(k)));document.querySelectorAll('[data-switch]').forEach(el=>el.onchange=async()=>{const id=el.dataset.switch;try{state.modules=await api('modules',{modules:{[id]:el.checked}});selected=id;const p=state.anatomy.find(p=>p.id===id);if(p.view!==view)setView(p.view);else{map();detail()}document.dispatchEvent(new Event('modules-changed'));toast(`${p.name} analogue ${el.checked?'on':'off'}.`)}catch(e){el.checked=!el.checked;toast(e.message,true)}})} function setView(v){view=v;if(state.anatomy.find(p=>p.id===selected)?.view!==v)selected=v==='inner'?'hippocampus':'occipital';$('#outer-view').classList.toggle('active',v==='outer');$('#inner-view').classList.toggle('active',v==='inner');map();detail()} export function initBrain(){map();detail();switches();$('#outer-view').onclick=()=>setView('outer');$('#inner-view').onclick=()=>setView('inner');$('#restore').onclick=async()=>{state.modules=await api('reset/modules',{});switches();map();document.dispatchEvent(new Event('modules-changed'));toast('All modules restored.')};document.addEventListener('brain-activity',e=>{active=e.detail;map();setTimeout(()=>{active=[];map()},900)})}