import{generateWeave,generateWeaveProject}from'./weave-generators.mjs';let state,revision,request=1300000;const emit=(n,d)=>window.dispatchEvent(new CustomEvent(n,{detail:d}));const dialog=document.createElement('dialog');dialog.id='weaveGeneratorDialog';dialog.innerHTML=`

Weave construction

Single-cloth construction only. Black=warp up. Applying replaces the weave and updates its repeat; current yarn colours/counts, densities and warp/weft plans stay unchanged.

`;document.body.append(dialog);const $=id=>document.getElementById(id),options=()=>({type:$('wgType').value,a:+$('wgA').value,b:+$('wgB').value,step:+$('wgStep').value,face:$('wgFace').value});function preview(){try{const g=generateWeave(options()),w=g[0].length,h=g.length,c=$('wgCanvas'),ctx=c.getContext('2d'),s=Math.min(c.width/w,c.height/h);ctx.fillStyle='#aaa';ctx.fillRect(0,0,c.width,c.height);for(let y=0;y{if(id==='wgType'&&$('wgType').value==='satin'){$('wgA').value=5;$('wgStep').value=2;}preview();};$('wgApply').onclick=()=>{try{if(!state||state.raster||state.revision!==revision)throw Error('Design changed or is in saved preview. Reopen weave construction to apply.');emit('cad:edit',{id:++request,type:'replace-project',revision,project:generateWeaveProject(state.project,options())});}catch(e){$('wgError').textContent=e.message;}};$('wgClose').onclick=()=>dialog.close();window.addEventListener('cad:document',e=>state=e.detail);window.addEventListener('cad:edit-result',e=>{if(e.detail.id>1300000&&e.detail.id<=request){if(e.detail.ok)dialog.close();else $('wgError').textContent=e.detail.error;}});window.addEventListener('cad:weave-generator',()=>{revision=state?.revision;preview();dialog.showModal();});emit('cad:request-state');