import {createPuzzle,puzzleRng,puzzleText,createTickets,createCaller,checkTicket,ticketCSV,callCSV} from './puzzle-engine.v1.mjs'; const $=id=>document.getElementById(id); const el=(tag,text)=>{const n=document.createElement(tag);if(text!==undefined)n.textContent=text;return n;}; function download(name,text,type){const url=URL.createObjectURL(new Blob([text],{type})),a=el('a');a.href=url;a.download=name;document.body.append(a);a.click();a.remove();setTimeout(()=>URL.revokeObjectURL(url),1000);} function table(rows,caption,marked=new Set()){const t=el('table');t.className='puzzle-grid';t.append(el('caption',caption));const b=el('tbody');rows.forEach((row,r)=>{const tr=el('tr');row.forEach((v,c)=>{const d=el('td',v??'');if(marked.has(`${r},${c}`))d.className='puzzle-marked';tr.append(d);});b.append(tr);});t.append(b);return t;} const word=$('word-puzzle'); if(word){let puzzle=null;const status=$('word-status'),out=$('word-output'),key=$('word-key'),answer=$('word-show');const say=s=>status.textContent=s; function clear(){puzzle=null;out.replaceChildren();key.replaceChildren();key.hidden=true;answer.checked=false;for(const id of ['word-show','word-download','word-answer-download','word-print'])$(id).disabled=true;} function render(){out.replaceChildren(table(puzzle.grid,'Word search puzzle'),el('p','Find: '+puzzle.words.join(', ')),el('p',`Seed ${puzzle.seed} · ${puzzle.size} × ${puzzle.size} · ${puzzle.mode==='all'?'All eight directions':'Across and down'}`));key.replaceChildren();key.hidden=!answer.checked;if(answer.checked){const marks=new Set(puzzle.answers.flatMap(a=>a.cells.map(p=>p.join(','))));key.append(table(puzzle.grid,'Answer key — highlighted letters',marks));const list=el('ul');for(const a of puzzle.answers){const [r,c]=a.cells[0],[y,x]=a.cells.at(-1);list.append(el('li',`${a.word}: row ${r+1}, column ${c+1} → row ${y+1}, column ${x+1}`));}key.append(list);}} $('word-generate').addEventListener('click',()=>{clear();try{const seed=$('word-seed').value.trim()||String(crypto.getRandomValues(new Uint32Array(1))[0]);puzzle=createPuzzle($('word-list').value,$('word-size').value,$('word-directions').value,puzzleRng(seed));puzzle.seed=seed;$('word-seed').value=seed;render();for(const id of ['word-show','word-download','word-answer-download','word-print'])$(id).disabled=false;say(`${puzzle.words.length} words placed with a checked answer key.`);}catch(e){say(e.message);}}); for(const id of ['word-list','word-size','word-directions','word-seed'])$(id).addEventListener('input',()=>{clear();say('Inputs changed. Generate a new puzzle.');});answer.addEventListener('change',()=>{if(puzzle)render();});$('word-download').addEventListener('click',()=>{if(puzzle)download('word-search.txt',puzzleText(puzzle),'text/plain;charset=utf-8');});$('word-answer-download').addEventListener('click',()=>{if(puzzle)download('word-search-answer.txt',puzzleText(puzzle,true),'text/plain;charset=utf-8');});$('word-print').addEventListener('click',()=>{if(puzzle)window.print();});word.hidden=false;$('word-fallback').hidden=true;clear();} const tambola=$('tambola-tool'); if(tambola){let strips=null;const caller=createCaller(),status=$('tambola-status'),calls=$('tambola-calls'),tickets=$('tambola-tickets');const say=s=>status.textContent=s; function renderTickets(){tickets.replaceChildren();if(!strips)return;const history=caller.state().history;strips.forEach((strip,i)=>{const group=el('section');group.className='tambola-strip';group.append(el('h3',`Strip ${i+1}`));strip.forEach((t,j)=>{const card=el('article');card.className='tambola-ticket';card.append(table(t,`Strip ${i+1} · Ticket ${j+1}`));const result=checkTicket(t,history);const p=el('p',`${result.lines} completed row(s)${result.full?' · Full ticket matched':''}`);p.className='tambola-match';card.append(p);group.append(card);});tickets.append(group);});} function renderCalls(){const s=caller.state();$('tambola-current').textContent=s.last??'—';$('tambola-remaining').textContent=`${s.remaining} of 90 remaining`;calls.textContent=s.history.join(', ')||'No calls yet';$('tambola-next').disabled=!s.remaining;$('tambola-undo').disabled=!s.history.length;$('tambola-call-download').disabled=!s.history.length;renderTickets();} $('tambola-generate').addEventListener('click',()=>{try{const n=createTickets($('tambola-count').value);if((strips||caller.state().history.length)&&!window.confirm('Replace the tickets and reset the current calling round?'))return;strips=n;caller.reset();renderCalls();$('tambola-download').disabled=$('tambola-print').disabled=false;say(`${strips.length*6} tickets generated. Each strip contains all 90 numbers exactly once.`);}catch(e){say(e.message);}}); $('tambola-next').addEventListener('click',()=>{try{const n=caller.next();renderCalls();say(`Called ${n}.`);}catch(e){say(e.message);}});$('tambola-undo').addEventListener('click',()=>{const n=caller.undo();renderCalls();say(n===null?'No call to undo.':`Removed call ${n}.`);});$('tambola-reset').addEventListener('click',()=>{if(caller.state().history.length&&!window.confirm('Reset the call history? Tickets are kept.'))return;caller.reset();renderCalls();say('Calling round reset. Tickets retained.');});$('tambola-download').addEventListener('click',()=>{if(strips)download('tambola-tickets.csv',ticketCSV(strips),'text/csv;charset=utf-8');});$('tambola-call-download').addEventListener('click',()=>download('tambola-calls.csv',callCSV(caller.state().history),'text/csv;charset=utf-8'));$('tambola-print').addEventListener('click',()=>{if(strips)window.print();});tambola.hidden=false;$('tambola-fallback').hidden=true;renderCalls();}