export function selectEvents(events,now=new Date()){ return events.filter(e=>e.slug&&/^[a-z0-9-]+$/.test(e.slug)&&Number.isFinite(Date.parse(e.start))&&Number.isFinite(Date.parse(e.end))&&Date.parse(e.end)>=now.getTime()).sort((a,b)=>Date.parse(a.start)-Date.parse(b.start)).slice(0,2); } export function eventState(e){return e.status==='full'?'GRUPĖ PILNA':e.status==='open'?'VYKSTA REGISTRACIJA':'REGISTRACIJA BAIGTA'} export function makeEvent(e,index=0){ const article=document.createElement('article');article.className='event-row'; const info=document.createElement('div');info.className='event-info'; const meta=document.createElement('div');meta.className='event-meta'; const date=document.createElement('p');date.className='event-date'; const formatter=new Intl.DateTimeFormat('lt-LT',{timeZone:'Europe/Vilnius',year:'numeric',month:'long',day:'numeric'}); date.textContent=e.date_label||`${formatter.format(new Date(e.start))} – ${formatter.format(new Date(e.end))}`; const badge=document.createElement('span');badge.className='tag';badge.textContent=eventState(e);meta.append(date,badge); const title=document.createElement('h3');title.textContent=e.title; const schedule=document.createElement('p');schedule.className='event-schedule';schedule.textContent=`${e.sessions??e.dates?.length??8} susitikimai · ${e.format} · ${e.schedule}`; info.append(meta,title,schedule); const actions=document.createElement('div');actions.className='event-actions'; const detail=document.createElement('a');detail.className='text-link';detail.href=e.slug+'.html';detail.textContent='Programos detalės'; const action=document.createElement('a');action.className=index===0?'button':'text-link';action.href='#kontaktai';action.textContent=e.status==='open'?'Registruotis':'Pranešti apie kitą grupę'; actions.append(detail,action);article.append(info,actions);return article; } if(typeof document!=='undefined'){ const list=document.querySelector('#event-list'); if(list)fetch('events.json',{cache:'no-cache'}).then(r=>{if(!r.ok)throw Error();return r.json()}).then(data=>{ const visible=selectEvents(data);if(visible.length)list.replaceChildren(...visible.map(makeEvent)); else{const text=document.createElement('p');text.textContent='Šiuo metu nėra vykstančių ar artėjančių mokymų.';list.replaceChildren(text)} }).catch(()=>{/* Retain the initial course information on a network failure. */}); }