// @ts-check
(()=>{
  'use strict';
  const App=window.IUS_TEMPLATE,{utils,state}=App;
  const projects=App.data.projects||[];
  const q=s=>utils.$(s);
  function renderTabs(){const host=q('[data-project-tabs]');if(!host)return;host.replaceChildren();projects.forEach((p,i)=>{const b=document.createElement('button');b.type='button';b.role='tab';b.dataset.projectIndex=String(i);b.setAttribute('aria-controls','projects-panel');b.innerHTML=`<strong>${utils.escapeHtml(p.code)}</strong><small>${utils.escapeHtml(utils.local(p.name))} · ${utils.escapeHtml(utils.local(p.category))}</small>`;b.addEventListener('click',()=>select(i,true));host.append(b)})}
  function select(index,focus=false){const p=projects[index];if(!p)return;state.activeProject=index;const stage=q('.project-stage');stage?.classList.add('is-transitioning');const update=()=>{
    q('[data-project-category]').textContent=utils.local(p.category);q('[data-project-year]').textContent=p.year;q('[data-project-code]').textContent=p.code;q('[data-project-name]').textContent=utils.local(p.name);q('[data-project-summary]').textContent=utils.local(p.summary);
    const meta=q('[data-project-meta]');meta.replaceChildren();p.meta.forEach(item=>{const div=document.createElement('div'),dt=document.createElement('dt'),dd=document.createElement('dd');dt.textContent=utils.local(item.term);dd.textContent=utils.local(item.value);div.append(dt,dd);meta.append(div)});
    const points=q('[data-project-points]');points.replaceChildren();utils.local(p.points).forEach(text=>{const li=document.createElement('li');li.textContent=text;points.append(li)});
    const img=q('[data-project-image]');img.src=p.image;img.alt=utils.local(p.caption);q('[data-project-caption]').textContent=utils.local(p.caption);q('[data-image-label]').textContent=p.code;
    utils.$$('[data-project-index]').forEach((btn,i)=>{btn.setAttribute('aria-selected',String(i===index));btn.tabIndex=i===index?0:-1});if(focus)q(`[data-project-index="${index}"]`)?.focus({preventScroll:true});
    requestAnimationFrame(()=>stage?.classList.remove('is-transitioning'));
  };
    if(document.startViewTransition&&!matchMedia('(prefers-reduced-motion: reduce)').matches)document.startViewTransition(update);else setTimeout(update,stage?80:0);
  }
  function openDetails(trigger){const p=projects[state.activeProject],d=q('[data-details-dialog]');if(!p||!d)return;q('[data-dialog-title]').textContent=utils.local(p.name);q('[data-dialog-overview]').textContent=utils.local(p.overview);const scope=q('[data-dialog-scope]');scope.replaceChildren();utils.local(p.scope).forEach(text=>{const li=document.createElement('li');li.textContent=text;scope.append(li)});q('[data-dialog-outcome]').textContent=utils.local(p.outcome);q('[data-dialog-note]').textContent=utils.local(p.note);utils.openDialog(d,trigger)}
  function init(){renderTabs();select(state.activeProject);q('[data-project-details]')?.addEventListener('click',e=>openDetails(e.currentTarget));q('[data-project-lightbox]')?.addEventListener('click',e=>{const p=projects[state.activeProject];App.modules.lightbox.open(p.image,utils.local(p.caption),e.currentTarget)});q('[data-project-tabs]')?.addEventListener('keydown',event=>{const tabs=utils.$$('[data-project-index]'),current=tabs.indexOf(document.activeElement);if(current<0)return;let next=current;if(['ArrowRight','ArrowDown'].includes(event.key))next=(current+1)%tabs.length;else if(['ArrowLeft','ArrowUp'].includes(event.key))next=(current-1+tabs.length)%tabs.length;else if(event.key==='Home')next=0;else if(event.key==='End')next=tabs.length-1;else return;event.preventDefault();select(next,true);tabs[next].scrollIntoView({block:'nearest',inline:'nearest'})});document.addEventListener('ius:localechange',()=>{renderTabs();select(state.activeProject)})}
  App.modules.projects={init,select};
})();
