const $=id=>document.getElementById(id),data=await(await fetch('/embed/ads/qr/catalog.json')).json(),apps=(await(await fetch('/embed/catalog.json')).json()).apps;for(const id of ['creative-app','creative-a','creative-b'])$(id).replaceChildren(...data.map(x=>new Option(x.title+' · '+x.edition,x.id)));if(data.length>1)$('creative-b').selectedIndex=1;const get=id=>data.find(x=>x.id===$(id).value);let seq=0;function wrap(ctx,s,max){let lines=[],line='';for(const w of s.split(/\s+/)){const n=line?line+' '+w:w;if(ctx.measureText(n).width>max&&line){lines.push(line);line=w}else line=n}if(line)lines.push(line);return lines.slice(0,3)}async function render(){const turn=++seq,a=get('creative-app'),[w,h]=$('creative-size').value.split('x').map(Number),c=$('creative-canvas'),ctx=c.getContext('2d'),img=new Image();$('creative-download').disabled=true;$('creative-status').textContent='Preparando diseño…';img.src=a.qr;try{await img.decode();if(turn!==seq)return;c.width=w;c.height=h;const pad=Math.round(w*.08),qr=Math.round(w*.19);ctx.fillStyle=$('creative-color').value;ctx.fillRect(0,0,w,h);ctx.fillStyle='rgba(0,0,0,.46)';ctx.fillRect(0,0,w,h);ctx.fillStyle='#8de3cf';ctx.font=`bold ${Math.round(w*.026)}px system-ui`;ctx.fillText('APPINITDEV / ANDROID',pad,pad);ctx.fillStyle='#fff';ctx.font=`bold ${Math.round(w*.057)}px system-ui`;const titleLines=wrap(ctx,a.title,w-2*pad);titleLines.forEach((l,i)=>ctx.fillText(l,pad,pad+w*.095+i*w*.066));ctx.font=`${Math.round(w*.027)}px system-ui`;const messageY=Math.max(pad+w*.095+titleLines.length*w*.066,h-pad-qr+w*.045);wrap(ctx,$('creative-text').value.trim()||'Descubre la aplicación',w-3*pad-qr).slice(0,2).forEach((l,i)=>ctx.fillText(l,pad,Math.min(messageY,h-pad-w*.09)+i*w*.035));ctx.fillStyle='#fff';ctx.fillRect(w-pad-qr,h-pad-qr,qr,qr);ctx.drawImage(img,w-pad-qr,h-pad-qr,qr,qr);ctx.font=`bold ${Math.round(w*.026)}px system-ui`;ctx.fillText(a.edition||'Android',pad,h-pad);$('creative-qr').src=a.qr;$('creative-link').href=a.url;$('creative-link').textContent=a.url;$('creative-qr-download').href=a.qr;$('creative-download').disabled=false;$('creative-status').textContent='Diseño listo · '+w+' × '+h}catch(e){$('creative-status').textContent='No se pudo cargar el QR'}}function compare(){const a=get('creative-a'),b=get('creative-b'),rows=[['Nombre',a.title,b.title],['Edición',a.edition,b.edition],['Categoría',a.category,b.category],['Enlace en revisión',a.review?'Sí':'No',b.review?'Sí':'No'],['Descripción',apps.find(x=>x.id===a.id)?.summary,apps.find(x=>x.id===b.id)?.summary]];$('creative-comparison').replaceChildren(...rows.map(row=>{const tr=document.createElement('tr');row.forEach((v,i)=>{const td=document.createElement(i?'td':'th');td.textContent=v||'No declarado';tr.append(td)});return tr}))}$('creative-render').onclick=render;$('creative-app').onchange=render;$('creative-a').onchange=$('creative-b').onchange=compare;$('creative-download').onclick=()=>$('creative-canvas').toBlob(b=>{if(!b)return;const u=URL.createObjectURL(b),a=document.createElement('a');a.href=u;a.download='appinitdev-promocion.png';a.click();setTimeout(()=>URL.revokeObjectURL(u),10000)},'image/png');compare();render();