P)){e.highlightedSlot=S,e.highlightedAtom=T;break}}}else{if(!_.isSquare||!_.expanded)continue;let{x:A,y:I}=tx(_.pickerPad),B=A,L=A+_.pickerPad.width,G=I,N=I+_.pickerPad.height;if(c>L||gN)continue;let U=["red","green","blue"].filter(e=>void 0===_[e]);if(0===U.length)continue;let{x:Y,y:H}=tx(_);for(let V of U){let j=tV[V],W=Y+_.size+2*tX+j*(tD.size+tH),q=H+tX,F=Math.hypot(h-W,s-q);F0)for(let s=0;s<10;s++){let c=e.options[s];if(e.parent.isSquare)h.values[9-s]=!0,s>0&&(h.values[9-s+1]=!1);else for(let u of d)u.values[9-s]=!0,s>0&&(u.values[9-s+1]=!1);let g=Z({channels:d}),$=M(g);c.colours=$,c.colourTicker=1/0,c!==e&&(c.needsColoursUpdateCountdown=s,c.needsColoursUpdate=!1)}},getCenterId(e){let t,l;for(let i=0;i[e,t+.13397460000000005/2*o]);let r=[];for(let n=0;n<6;n++){let d=wrap(n+1,0,5),h=a[n],s=a[d],c=[0,1].map(e=>(h[e]+s[e])/2);r.push(c)}let u=[t+i/2,l+o/2],g=a.map((e,t)=>{let l=wrap(t+1,0,5),i=a[wrap(t+1+1,0,5)],o=wrap(t+1+1,0,5),n=r[o],d=r[l];return[u,d,i,n]}),[$,...p]=a,f=new Path2D;for(let v of(f.moveTo(...$),p))f.lineTo(...v);if(f.closePath(),eJ.fillStyle=e.colour,eJ.fill(f),void 0!==e.ons)for(let m=0;m<6;m++){if(!e.ons[m])continue;let[_,...w]=g[m],b=new Path2D;for(let S of(b.moveTo(..._),w))b.lineTo(...S);b.closePath(),eJ.fillStyle=Colour.Silver,eJ.fill(b),eJ.lineWidth=1/C,eJ.strokeStyle=Colour.Silver,eJ.stroke(b)}e.hasBorder&&(eJ.lineWidth=1.5*tR,eJ.strokeStyle=e.borderColour,eJ.stroke(f),e.parent.isSquare&&lP.drawY(e,e.size-8,4))},getValue(e){let t=0;for(let l of e.ons)l&&t++;return t},click(e){e.expanded?e.unexpand(e):e.expand(e)},unexpand(e){for(let t of(e.expanded=!1,e.handles))tb(e,t);for(let l of e.buttons)tb(e,l);e.handles=[],e.buttons=[]},expand(e){e.expanded=!0,e.handles=[],e.buttons=[];let{width:t,height:l}=e,i=.22373758200000007*t,o=[[t,l/2-tM.height/2],[t-i,l-tM.height/2],[i,l-tM.height/2],[0,l/2-tM.height/2],[i,0-tM.height/2],[t-i,0-tM.height/2],],a=[[t,l/2],[t-i,l],[i,l],[0,l/2],[i,0],[t-i,0],];a=a.map(([t,l],i)=>{let[o,a]=[t-e.width/2,l-e.height/2],[r,n]=[];return i%3==0?[r,n]=[2.2*o,2.2*a]:[r,n]=[2*o,2*a],[r+e.width/2,n+e.height/2]});for(let r=0;r<6;r++){let n=tw(e,tM);n.rotation=r,n.x=o[r][0]-tM.width/2,n.y=o[r][1],e.handles.push(n);let d=tw(e,tF);d.x=a[r][0]-tF.size/2,d.y=a[r][1]-tF.size/2,e.buttons.push(d),d.id=r,e.ons[r]&&(d.inner.selected=!0,d.inner.colour=Colour.Silver)}},construct(e){e.ons=[!1,!1,!1,!1,!1,!1]},updateValue(e){let t=tV[e.variable],l=!e.ons[1]&&!e.ons[0]&&!e.ons[5],i=!e.ons[2]&&!e.ons[3]&&!e.ons[4],o=!l&&!i,a=[l||o,e.ons[1],e.ons[0],e.ons[5],!1,!1,!1,!1,!1,!1],r=[i||o,e.ons[2],e.ons[3],e.ons[4],!1,!1,!1,!1,!1,!1],n=X({values:a}),d=X({values:r}),h=X({channel:t,variable:e.variable,add:n,subtract:d});e.value=h},hover(e){let{x:t,y:l}=tx(e),i=t,o=l,a=t+e.width,r=l+e.height;for(let n of paddles){let{x:d,y:h}=tx(n),s=d+n.width,c=h,u=h+n.height;if(void 0===n.chance&&n.expanded&&i<=s&&a>=s&&(oc||r>c&&r{let[a,r]=[e-l,t-i],n=Math.sqrt(a**2+r**2),d=Math.atan2(r,a),[h,s]=[n*Math.cos(o+d),n*Math.sin(o+d),];return[l+h,i+s]},tF={size:tD.size,offscreen:tk.offscreen,overlaps:tk.overlaps,colour:Colour.Grey,grab:e=>e.parent,behindChildren:!0,draw(e){tk.draw(e)},construct(e){e.inner=tw(e,t4,{bottom:!1}),e.inner.x=e.width/2-e.inner.width/2,e.inner.y=e.height/2-e.inner.height/2},click(e){e.inner.selected?(e.inner.selected=!1,e.inner.colour=Colour.Grey):(e.inner.selected=!0,e.inner.colour=Colour.Silver);let t=e.parent;if(t.ons[e.id]=e.inner.selected,t.parent.isPaddle){let l=t.parent;ln(l)}else if(t.parent.isSquare){let i=t.parent;t.updateValue(t);let o=tV[t.variable];i.receiveNumber(i,t.value,o,{expanded:t.expanded,numberAtom:t})}ty(e.parent)}},t4={size:2*tD.size/3,offscreen:tk.offscreen,overlaps:tk.overlaps,grab:e=>e.parent,touch:e=>e.parent,draw:tk.draw,hasBorder:!0,borderColour:Colour.Black,colour:Colour.Grey},tM={offscreen:tT.offscreen,overlaps(e,t,l){e.y-=e.height/2,e.height*=2;let i=tT.overlaps(e,t,l);return e.height/=2,e.y+=e.height/2,i},colour:Colour.Grey,rotation:0,touch:e=>e.parent,grab:e=>e.parent,x:50,width:tD.size/2+tD.size/4,height:tD.size/3,draw(e){let{x:t,y:l}=tx(e),{width:i,height:o}=e,a=new Path2D,r=[[t,l],[t+i,l],[t+i,l+o],[t,l+o],];e.rotation>0&&(r=r.map(a=>tZ(a,[t+i/2,l+o/2],e.rotation*Math.PI/3)));let[n,...d]=r;for(let h of(a.moveTo(...n),d))a.lineTo(...h);eJ.fillStyle=e.colour,eJ.fill(a)}},t5={draw(e){let{x:t,y:l}=tx(e),i=Math.round(e.width),o=Math.round(e.height);eJ.fillStyle=Colour.Grey,eJ.fillRect(Math.round(t),Math.round(l),i,o)},overlaps:tT.overlaps,offscreen:tT.offscreen,height:t1-t3,width:tD.size+2*tX,x:-tX,dragOnly:!0,grab:e=>e.parent.expanded?e:e.parent,touch:e=>e.parent.expanded?e:e.parent,cursor:e=>e.parent.expanded?"ns-resize":"pointer",move(e){e.parent.positionSelectionBack(e.parent)},drop(e){let t=Math.round((e.y+t3/2)/t1),l=e.parent.value,[i,o]=e.parent.getStartAndEndId(e.parent),a=e.parent.getCenterId(e.parent);e.isTop&&(o=a-t),e.isTop||(i=a-(t-1));let r=[!1,!1,!1,!1,!1,!1,!1,!1,!1,!1];for(let n=i;n<=o;n++)r[n]=!0;let d=X({channel:l.channel,values:r});if(e.parent.value=d,e.parent.deleteOptions(e.parent),e.parent.createOptions(e.parent),e.dx=0,e.dy=0,e.parent.parent.isSquare){let h=e.parent.parent,s=tV[e.parent.channelSlot];h.receiveNumber(h,d,s)}if(e.parent.parent.isPaddle){let c=e.parent.parent;ln(c)}},dragLockX:!0},tK={overlaps:tT.overlaps,offscreen:tT.offscreen,width:(tD.size-t3)/2,height:tD.size,grab:e=>e.parent,touch:e=>e.parent,dragLockX:!0,draw:tT.draw,colour:Colour.Grey},t6={draw:tT.draw,overlaps:tT.overlaps,offscreen:tT.offscreen,height:t3,width:tD.size,grab:e=>e.parent,hasBorder:!0,colourTicker:1/0,colours:[999],colourId:0,dcolourId:1,update(e){e.needsColoursUpdateCountdown>=0&&(e.needsColoursUpdateCountdown--,e.needsColoursUpdateCountdown<0&&(e.needsColoursUpdate=!0)),e.needsColoursUpdate&&(e.updateColours(e),e.needsColoursUpdateCountdown=-1,e.needsColoursUpdate=!1)},getId(e){let t=e.parent,l=t.getCenterId(t),i=e.y/t1;return l-i},updateColours(e){e.isGradient=!0,e.gradient=tL({colours:e.colours,width:e.width*C,height:e.height*C,gradient:e.gradient})},touch(e){let t=e.getId(e);return e.parent.value.values[t]?e.parent:e},click(e){let t=[!1,!1,!1,!1,!1,!1,!1,!1,!1,!1];t[e.value]=!0;let l=X({values:t,channel:e.parent.value.channel}),i=e.parent;if(i.value=l,i.deleteOptions(i),i.createOptions(i),i.needsColoursUpdate=!0,i.parent.isSquare){let o=i.parent,a=tV[i.channelSlot];o.receiveNumber(o,l,a)}if(i.parent.isPaddle){let r=i.parent;ln(r)}},construct(e){if(e.pityTop){let t=tw(e,lt);t.y=-t.height}if(e.pityBottom){let l=tw(e,lt);l.y=e.height}}},t9=["red","green","blue",],tJ={behindChildren:!0,highlighter:!0,rightDraggable:!0,rightDrag(e){let t=tc(tJ);tp(t);let{x:l,y:i}=tx(e);return e6.offset.x-=e.x-l,e6.offset.y-=e.y-i,t.variable=e.variable,e.expanded&&t.expand(t),t.updateAppearance(t),t},drag(e){if(e.parent.isSquare){let t=e.parent;t[e.channelSlot]=void 0;let l=tV[e.channelSlot];t.receiveNumber(t,void 0,l),tS(t,e),e.updateAppearance(e),e.attached=!1}else if(e.parent.isTallRectangle){let i=e.parent;tS(i,e),i.operationAtoms[e.highlightedSlot]=void 0;let o="padTop"===e.highlightedSlot?"add":"subtract";if(i.value[o]=void 0,e.expanded&&(e.unexpand(e),e.expand(e)),e.attached=!1,i.expanded)i.unexpand(i),i.expand(i);else{let a="padTop"===e.highlightedSlot?"handleTop":"handleBottom";tb(i,i[a],{quiet:!0}),tb(i,i[e.highlightedSlot],{quiet:!0}),i.expand(i),i.unexpand(i)}}return e},hover(e){let{x:t,y:l}=tx(e),i=t,o=l,a=t+e.width,r=l+e.height,n=1/0,d,h,s=lp();for(let c of s){if(c===e)continue;if(c.isTallRectangle){if(!c.expanded)continue;let u=["padTop","padBottom"];for(let g of u){let $=c;for(;$.isTallRectangle&&void 0!==$.operationAtoms[g];)$=$.operationAtoms[g];if(!$.isTallRectangle||!$.expanded)continue;let p=$[g],{x:f,y:v}=tx(p),m=f,_=f+p.width,w=v,b=v+p.height;if(!(i>_)&&!(ab))return e.highlightedSlot=g,p}continue}if(!c.isSquare||!c.expanded)continue;let{x:S,y:R}=tx(c.pickerPad),C=S,T=S+c.pickerPad.width,k=R,E=R+c.pickerPad.height;if(i>T||aE)continue;let z=["red","green","blue"].filter(e=>void 0===c[e]);if(0===z.length)continue;let{x:O,y:D}=tx(c);for(let P of z){let A=tV[P],I=O+c.size+2*tX+A*(tD.size+tH),B=D+tX,L=Math.hypot(t-I,l-B);Le)),s.lineTo(...[r+a,d].map(e=>e)),s.lineTo(...[h,n+o].map(e=>e)),s.lineTo(...[r,d].map(e=>e)),s.closePath(),eJ.fillStyle=e.colour,eJ.fill(s),e.hasBorder&&(eJ.lineWidth=tR,eJ.strokeStyle=e.borderColour,e.isTool&&(eJ.lineWidth=1.5*tR,eJ.strokeStyle=tC[e.colour.splash]),eJ.stroke(s))},offscreen:tT.offscreen,overlaps:tT.overlaps,hasBorder:!0,isTallRectangle:!0,size:t3+tX/3*2,height:t3+tX/3*2,width:t3+tX/3*2,construct(e){e.variable=t9[Random.Uint8%3],e.value=X({variable:e.variable}),e.updateAppearance(e),e.isTool||(e.width+=tR/2,e.height+=tR/2,e.size+=tR/2),e.operationAtoms={padTop:void 0,padBottom:void 0}},makeOperationAtoms(e){if(void 0!==e.value.add&&void 0===e.operationAtoms.padtop){if(void 0===e.value.add.variable){let t=tw(e,tW);t.value=e.value.add,e.operationAtoms.padTop=t,t.x=e.padTop.x+tX,t.y=e.padTop.y+e.padTop.height/2-t.height/2,t.highlightedSlot="padTop"}else{let l=tw(e,tJ);l.value=e.value.add,l.variable=e.value.add.variable,l.makeOperationAtoms(l),l.highlightedSlot="padTop",l.x=0,l.y=e.padTop.y+e.padTop.height/2-l.height/2,l.updateAppearance(l),e.operationAtoms.padTop=l}}if(void 0!==e.value.subtract&&void 0===e.operationAtoms.padBottom&&void 0===e.value.subtract.variable){let i=tw(e,tW);i.value=e.value.subtract,e.operationAtoms.padBottom=i,i.x=e.padBottom.x+tX,i.y=e.padBottom.y+e.padBottom.height/2-i.height/2,i.highlightedSlot="padBottom"}},updateAppearance(e){"red"===e.variable?e.colour=Colour.Red:"green"===e.variable?e.colour=Colour.Green:"blue"===e.variable&&(e.colour=Colour.Blue),e.borderColour=borderColours[e.colour.splash]},expanded:!1,click(e){e.expanded?e.unexpand(e):e.expand(e)},expand(e){for(let t of(e.expanded=!0,void 0===e.value.add&&(e.y<0||!(e.parent.isTallRectangle&&e.parent.operationAtoms.padBottom===e))&&(e.handleTop=tw(e,lk),e.handleTop.width=e.handleTop.height,e.handleTop.height*=2,e.handleTop.y=e.height/2-e.handleTop.height,e.handleTop.x=e.width/2-e.handleTop.width/2,e.handleTop.behindParent=!0,e.padTop=tw(e,lT),e.padTop.height=t8.height,e.padTop.width=tD.size+2*tH,e.padTop.x=e.width/2-e.padTop.width/2,e.padTop.y=-e.padTop.height-tX),void 0===e.value.subtract&&(e.y>0||!(e.parent.isTallRectangle&&e.parent.operationAtoms.padTop===e))&&(e.handleBottom=tw(e,lk),e.handleBottom.width=e.handleBottom.height,e.handleBottom.height*=2,e.handleBottom.y=e.height/2,e.handleBottom.x=e.width/2-e.handleBottom.width/2,e.handleBottom.behindParent=!0,e.padBottom=tw(e,lT),e.padBottom.height=t8.height,e.padBottom.width=tD.size+2*tH,e.padBottom.x=e.width/2-e.padBottom.width/2,e.padBottom.y=e.height+tX),e.handleRight=tw(e,lk),e.handleRight.y=e.height/2-e.handleRight.height/2,e.handleRight.x=e.width/2,e.handleRight.width*=2.5,e.handleRight.behindParent=!0,e.padRight=tw(e,lT),e.padRight.height=t8.height,e.padRight.width=tX+(e.width+tX/1.5)*3,e.padRight.y=e.height/2-e.padRight.height/2,e.padRight.x=e.width/2+(tD.size+2*tH)/2+tX,e.red=tw(e,tQ),e.red.x=e.padRight.x+tX/Math.SQRT2,e.red.borderColour=Colour.Red,e.red.colour=Colour.Black,e.red.value="red",e.green=tw(e,tQ),e.green.x=e.padRight.x+tX/Math.SQRT2+(e.green.width+tX)*1,e.green.borderColour=Colour.Green,e.green.colour=Colour.Black,e.green.value="green",e.blue=tw(e,tQ),e.blue.x=e.padRight.x+tX/Math.SQRT2+(e.blue.width+tX)*2,e.blue.borderColour=Colour.Blue,e.blue.colour=Colour.Black,e.blue.value="blue",e.winnerPin=tw(e,le),e.winnerPin.x=e[e.variable].x+e.winnerPin.width/2,e.winnerPin.y=e.winnerPin.height/2,e.winnerPin.colour=e[e.variable].borderColour,e.winnerPin.borderColour=e.winnerPin.colour,["padTop","padBottom"])){let l=e.operationAtoms[t];void 0!==l&&(tp(l),t0(e,l))}for(let i of e.children)i.isTallRectangle&&i.expanded&&(i.unexpand(i),i.expand(i))},unexpand(e){e.expanded=!1,tb(e,e.red),tb(e,e.green),tb(e,e.blue),tb(e,e.padRight),tb(e,e.handleRight),tb(e,e.winnerPin),void 0===e.value.add&&(tb(e,e.padTop,{quiet:!0}),tb(e,e.handleTop,{quiet:!0})),void 0===e.value.subtract&&(tb(e,e.padBottom,{quiet:!0}),tb(e,e.handleBottom,{quiet:!0}))}},tQ={draw(e){tJ.draw(e)},offscreen:tT.offscreen,overlaps:tT.overlaps,hasBorder:!0,size:t3+tX/3*2,height:t3+tX/3*2,width:t3+tX/3*2,grab:e=>e.parent,click(e){if(e.value===e.parent.variable)return;e.parent.variable=e.value,e.parent.value.variable=e.value,e.parent.winnerPin.x=e.x+e.parent.winnerPin.width/2,e.parent.winnerPin.colour=e.borderColour,e.parent.winnerPin.borderColour=e.borderColour,e.parent.updateAppearance(e.parent);let t=e.parent,l=t,i=t.parent;for(;!i.isSquare;){if(i===ts)return;l=i,i=i.parent}let o=0;"green"===l.channelSlot&&(o=1),"blue"===l.channelSlot&&(o=2);let a=i.variableAtoms[o];i.receiveNumber(i,a.value,o,{expanded:a.expanded,numberAtom:a})}},le={draw(e){tJ.draw(e)},offscreen:tT.offscreen,overlaps:tT.overlaps,hasBorder:!0,size:(t3+tX/3*2)/2,height:(t3+tX/3*2)/2,width:(t3+tX/3*2)/2,grab:e=>e.parent,touch:e=>e.parent},lt={draw(){},overlaps:tT.overlaps,offscreen:tT.offscreen,grab:e=>e.parent.parent,touch:e=>e.parent,colour:Colour.Grey,width:tD.size,height:t1-t3,y:0,x:0};paddles=[];let ll=tD.size/2,li={stayAtBack:!0,attached:!0,noDampen:!0,isPaddle:!0,behindChildren:!0,draw:tT.draw,overlaps:tT.overlaps,offscreen:tT.offscreen,colour:Colour.Grey,size:tD.size+4*tX,width:tD.size+4*tX,height:tD.size+4*tX,dragOnly:!0,dragLockY:!0,scroll:0,rightTriangle:void 0,x:Math.round(ll),y:tD.size+tX+ll,construct(e){e.cellAtoms=[],e.slots=[];let t=tw(e,ly);e.handle=t,e.setLimits(e),e.x=e.minX,e.expanded=!1,e.pinhole=tw(t,l_),e.dummyLeft=tw(e,la),e.dummyLeft.visible=!1,e.dummyRight=tw(e,la),e.dummyRight.visible=!1,ln(e)},setLimits(e){e.maxX=e.handle.width,e.minX=e.handle.width-e.width},drop(e){let t=e.maxX-e.x,l=e.x-e.minX;te,rightDraggable:!0,getColour(e){let t=e.cellAtoms;if(0===t.length){let l=Z({channels:[void 0,void 0,void 0]});return l}if(1===t.length){let i=Q(t[0].value);return i}let o=lu(t),a=makeDiagram({left:o});return el(a),a},rightDrag(e){let t=e.cellAtoms;if(0===t.length){let l=tc(tD);e6.offset.x=-l.width/2,e6.offset.y=-l.height/2;let i=Z({channels:[void 0,void 0,void 0]});return tO(i),tp(l),l.value=i,l.update(l),l}if(1===t.length){let o=Q(t[0].value),a=t[0].clone(t[0]);return e6.offset.x=-a.width/2,e6.offset.y=-a.height/2,tO(o),tp(a),a.value=o,a.update(a),a}let r=tc(tD);e6.offset.x=-r.width/2,e6.offset.y=-r.height/2;let n=lu(t),d=makeDiagram({left:n});return el(d),r.value=d,tp(r),tO(d),r.update(r),r}},lo=(e,t)=>{let l=new Path2D,[i,...o]=t;for(let a of(l.moveTo(...i.map(e=>Math.round(e))),o))l.lineTo(...a.map(e=>Math.round(e)));l.closePath(),eJ.fillStyle=e,eJ.fill(l)},la={visible:!0,isSlot:!0,behindChildren:!0,draw(e){if(!e.visible)return;let[t,l]=tx(e);e.width,e.height,eJ.fillStyle=e.colour;let i=e.width/3,o=e.width/3,a=t+e.width/2-i/2,r=l+e.height/2-o/2;eJ.fillRect(...[a,r,i,o].map(e=>Math.round(e)))},offscreen:tT.offscreen,overlaps:tT.overlaps,colour:Colour.Black,size:tD.size,grab:e=>e.parent,dragOnly:!0},lr=tD.size,ln=e=>{let t=li.width,l=li.size;if(e.cellAtoms.length>0){let i=1/0,o=-1/0,a=-1/0,r=1/0;for(let n of e.cellAtoms){let d=n.x,h=n.y,s=d,c=d+lr,u=h,g=h+lr;sa&&(a=c),uo&&(o=g)}let $=0,p=0,f=li.height/2-tD.size/2,v=li.width/2-tD.size/2,m=f,_=v;for(let w of(i!==m&&(o+=$=m-i),r!==_&&(a+=p=_-r),e.cellAtoms))w.y+=$,w.x+=p;let b=a+v,S=o+f;t=b,l=S}for(let R of(void 0!==e.rightTriangle&&(e.rightTriangle.x=t,e.rightTriangle.y=l/2-e.rightTriangle.height/2,t=t+t+e.rightTriangle.width),(e.hasSymmetry||void 0!==e.chance)&&(t+=lb.size/3),e.width=t,e.height=l,e.setLimits(e),e.slots))tb(e,R);if(e.slots=[],void 0!==e.rightTriangle)for(let C of e.cellAtoms){let T=tw(e,la,{bottom:!0});C.slot=T,e.slots.push(T),T.x=C.x+e.rightTriangle.x+e.rightTriangle.width,T.y=C.y,T.cellAtom=C,void 0!==C.slotted&&(C.slotted.x=C.x+e.rightTriangle.x+e.rightTriangle.width,C.slotted.y=C.y,T.colour=Colour.Grey)}void 0!==e.rightTriangle&&(void 0!==e.cellAtoms[0]&&void 0!==e.cellAtoms[0].slot?e.offset=e.cellAtoms[0].slot.x-e.cellAtoms[0].x:e.offset=0),void 0!==e.symmetryCircle&&(e.symmetryCircle.x=e.width-e.symmetryCircle.width/2,e.symmetryCircle.y=e.height/2-e.symmetryCircle.height/2),void 0!==e.chance&&(e.chance.x=e.width-e.chance.width/2,e.chance.y=e.height/2-e.chance.height/2),void 0!==e.chance&&void 0!==e.symmetryCircle&&(e.symmetryCircle.y-=e.symmetryCircle.height/2,e.chance.y+=e.symmetryCircle.height/2,e.height>100&&(e.symmetryCircle.y-=tX/2,e.chance.y+=tX/2)),e.handle.y=e.height/2-e.handle.height/2,0===e.cellAtoms.length&&(e.dummyLeft.x=ll,e.dummyLeft.y=e.height/2-e.dummyLeft.height/2,e.dummyRight.x=e.width-ll-e.dummyLeft.width,e.dummyRight.y=e.height/2-e.dummyRight.height/2),lg(e),lf()},ld=e=>{let t=F(e);return 1===t.size},lh=(e,t)=>{for(let l=0;l<3;l++){let i=e.channels[l],o=t.channels[l];if(void 0===i&&void 0!==o||void 0!==i&&void 0===o||(void 0!==i||void 0!==o)&&i.variable!==o.variable)return!1}let a=M(e),r=M(t);for(let n of a){let d=r.indexOf(n);if(-1===d)return!1;r.splice(d,1)}return!(r.length>0)},ls=(e,t)=>{if(t.stamp)return;let l=ld(t);if(!l){let i;for(let o=0;o{let t=[...e].sort((e,t)=>e.xt.x?1:e.yt.y?1:0);return t},lu=e=>{let t=lc(e),l=t[0],i=[];for(let o of e){let a=(o.x-l.x)/o.width,r=(o.y-l.y)/o.height,n=Q(o.value),d=makeDiagramCell({x:a,y:r,content:n});i.push(d)}return i},lg=e=>{if(!e.expanded)return;void 0!==e.rightTriangle&&(e.pinhole.locked?e.rightTriangle.colour=Colour.splash(999):e.rightTriangle.colour=Colour.splash(0));let t=DRAGON_TRANSFORMATIONS.NONE;if(e.hasSymmetry){let[l,i,o]=lw(e.symmetryCircle.value),a=l>0,r=i>0,n=o>0,d=`${r?"X":""}${a?"Y":""}${n?"R":""}`;""===d?d="NONE":("XR"===d||"YR"===d)&&(d="XYR"),t=DRAGON_TRANSFORMATIONS[d]}let h=lc(e.cellAtoms),s=h[0],c=[],u=[],g=[];for(let $ of h){let p=($.x-s.x)/$.width,f=($.y-s.y)/$.height;if($.isLeftSlot){let v=X({values:[!0,!0,!0,!0,!0,!0,!0,!0,!0,!0],channel:0}),m=X({values:[!0,!0,!0,!0,!0,!0,!0,!0,!0,!0],channel:1}),_=X({values:[!0,!0,!0,!0,!0,!0,!0,!0,!0,!0],channel:2}),w=Z({channels:[v,m,_]});ls(g,w);let b=makeDiagramCell({x:p,y:f,content:w});c.push(b)}else if($.value.isDiagram){let S=lc($.value.left);for(let R of S){let C=Q(R.content);ls(g,C);let T=p+R.x,k=f+R.y,E=makeDiagramCell({x:T,y:k,width:R.width,height:R.height,content:C});c.push(E)}}else{let z=Q($.value);ls(g,z);let O=makeDiagramCell({x:p,y:f,content:z});c.push(O)}if(!$.isLeftSlot&&$.value.isDiagram){let D=ei($.value),[P,A]=eo(D),I=makeDiagramCell({x:p,y:f,instruction:DRAGON_INSTRUCTION.merge,splitX:P,splitY:A});u.push(I)}let B=void 0===$.slotted?void 0:$.slotted.value;if(void 0===B){let L=makeDiagramCell({x:p,y:f,instruction:DRAGON_INSTRUCTION.nothing});u.push(L)}else if(B.isDiagram){let G=ei(B),[N,U]=eo(G),Y=makeDiagramCell({x:p,y:f,instruction:DRAGON_INSTRUCTION.split,splitX:N,splitY:U});u.push(Y);let H=lc(B.left);for(let V of H){let j=Q(V.content);ls(g,j);let W=p+V.x,q=f+V.y,F=makeDiagramCell({x:W,y:q,width:V.width,height:V.height,content:j,instruction:DRAGON_INSTRUCTION.recolour});u.push(F)}}else{let M=Q(B);ls(g,M);let K=makeDiagramCell({x:p,y:f,content:M});u.push(K)}}let J=ei(makeDiagram({left:c,right:u})),ee=e.pinhole.locked,et=void 0===e.chance?void 0:e.chance.getValue(e.chance),el=makeRule({steps:[J],transformations:t,locked:ee,chance:et});e.rule=el,void 0!==e.registry&&ed(e.registry),ee&&void 0!==e.rightTriangle&&(e.registry=registerRule(el))},l$=(e=state.colourTode.atoms)=>{let t=[...e];for(let l of t)t.push(...l$(l.children));return t},lp=()=>{let e=[...state.colourTode.atoms];for(let t of paddles)for(let l of t.children)!l.isPinhole&&(l.isPaddleHandle||e.push(l));for(let i of e)i.isSquare&&i.expanded&&e.push(...i.children);return e},lf=()=>{if(paddles.length>1&&lU("triangle"),paddles.length>2){let e=0;for(let t of paddles)void 0!==t.rightTriangle&&e++;e>=2&&lU("hexagon")}let l;for(let i of paddles){if(void 0===l){i.y=li.y+li.scroll,l=i;continue}i.y=l.y+l.height+ll,l=i}},lv=(e,t=paddles.indexOf(e))=>{paddles.splice(t,1),void 0!==e.registry&&ed(e.registry),t$(e),lf()},lm=()=>{let e=tc(li);return paddles.push(e),lf(),tp(e),e},ly={isPaddleHandle:!0,attached:!0,behindChildren:!0,draw:tT.draw,overlaps:tT.overlaps,offscreen:tT.offscreen,colour:Colour.Grey,size:li.x,x:-li.x,y:li.size/2-li.x/2,touch:e=>e.parent.pinhole,grab:e=>e.parent.pinhole},l_={isPinhole:!0,attached:!0,locked:!1,borderScale:.5,borderColour:Colour.Black,draw(e){e.locked?(e.hasBorder=!0,e.colour=Colour.Grey):(e.hasBorder=!1,e.colour=Colour.Black),tk.draw(e)},overlaps:tk.overlaps,offscreen:tk.offscreen,colour:Colour.Black,size:ly.size-tX/2,y:tX/2/2,x:tX/2/2,click(e){let t=e.parent,l=t.parent;if(e.locked)e.locked=!1,l.grabbable=!0,t.draggable=!0,l.draggable=!0,e.draggable=!0,lg(l);else{for(let i of(e.locked=!0,t.draggable=!1,e.draggable=!1,l.cellAtoms)){if(i.expanded&&i.unexpand(i),void 0!==i.slotted){let o=i.slotted;o.expanded&&o.unexpand(o)}i.joins.length>0&&i.joinExpanded&&i.joinUnepxand(i)}0===l.cellAtoms.length&&(l.grabbable=!1,l.draggable=!1),lg(l)}},grab:e=>e.parent.parent},lx=new Map;lx.set(0,DRAGON_TRANSFORMATIONS.NONE),lx.set(100,DRAGON_TRANSFORMATIONS.X),lx.set(10,DRAGON_TRANSFORMATIONS.Y),lx.set(110,DRAGON_TRANSFORMATIONS.XY),lx.set(1,DRAGON_TRANSFORMATIONS.R),lx.set(111,DRAGON_TRANSFORMATIONS.XYR),lx.set(101,DRAGON_TRANSFORMATIONS.XYR),lx.set(11,DRAGON_TRANSFORMATIONS.XYR);let lw=getRGB,lb={hasBorder:!0,draw(e){if(tk.draw(e),void 0===e.value)return;let[t,l,i]=lw(e.value);t>0&&lD.drawX(e),l>0&&lP.drawY(e),i>0&&lA.drawR(e)},offscreen:tk.offscreen,overlaps:tk.overlaps,expanded:!1,borderColour:Colour.Grey,colour:Colour.Black,value:0,click(e){e.expanded?e.unexpand(e):e.expand(e)},expand(e){e.pad=tw(e,lT),e.handle=tw(e,lk),e.handle.width+=tX,e.expanded=!0;let[t,l,i]=lw(e.value);e.xToggle=tw(e,lD),e.yToggle=tw(e,lP),e.rToggle=tw(e,lA),t>0&&(e.xToggle.value=!0),l>0&&(e.yToggle.value=!0),i>0&&(e.rToggle.value=!0)},unexpand(e){tb(e,e.pad),tb(e,e.handle),tb(e,e.xToggle),tb(e,e.yToggle),tb(e,e.rToggle),e.expanded=!1},size:tD.size,update(e){let{x:t,y:l}=tx(e),i=state.colourTode.atoms.indexOf(e),o=t,a=l,r=t+e.width,n=l+e.height;if(e6.content===e)for(let d of paddles){let h=state.colourTode.atoms.indexOf(d),{x:s,y:c}=tx(d),u=s+d.width,g=c,$=c+d.height;if(!d.hasSymmetry&&d.expanded&&i>h&&o<=u&&r>=u&&(a<$&&a>g||n>g&&n<$)){void 0!==e.highlightPaddle&&tb(e,e.highlightPaddle),e.highlightPaddle=tw(e,lS,{bottom:!0}),e.highlightPaddle.width=l0,e.highlightPaddle.height=d.height,e.highlightPaddle.y=g,e.highlightPaddle.x=u-l0/2,e.highlightedPaddle=d;return}}void 0!==e.highlightPaddle&&(tb(e,e.highlightPaddle),e.highlightPaddle=void 0,e.highlightedPaddle=void 0)},drop(e){if(!e.attached&&void 0!==e.highlightedPaddle){let t=e.highlightedPaddle;e.attached=!0,t0(t,e),t.hasSymmetry=!0,t.symmetryCircle=e,ln(t),e.dx=0,e.dy=0}},drag(e){if(e.attached){let t=e.parent;e.attached=!1,tS(t,e),t.hasSymmetry=!1,t.symmetryCircle=void 0,ln(t)}return e},rightDraggable:!0,rightDrag(e){let t=tc(lb);t.value=e.value;let{x:l,y:i}=tx(e);return e6.offset.x-=e.x-l,e6.offset.y-=e.y-i,t.x=l,t.y=i,tp(t),t}},l0=tR,lS={behindParent:!0,draw:tT.draw,offscreen:tT.offscreen,overlaps:tT.overlaps,draggable:!1,grabbable:!1,justVisual:!0,colour:Colour.splash(999),borderColour:Colour.splash(999),hasAbsolutePosition:!0,hasInner:!1},lR={draw:tT.draw,offscreen:tT.offscreen,overlaps:tT.overlaps,dragOnly:!0,width:lb.size,x:lb.size*Math.sqrt(3)/2+tX,height:2*lb.size-tX,y:-lb.size/2+tX/2,colour:Colour.Grey,grab:e=>e.parent},lC={draw:tT.draw,offscreen:tT.offscreen,overlaps:tT.overlaps,dragOnly:!0,width:lb.size/2+tX,x:lb.size/2,height:lb.size/3,y:lb.size/2-lb.size/3/2,colour:Colour.Grey,grab:e=>e.parent},lT={draw:tT.draw,offscreen:tT.offscreen,overlaps:tT.overlaps,dragOnly:!0,width:lb.size,x:lb.size+tX,height:3*lb.size-tX,y:-(3*lb.size)/3+tX/2,colour:Colour.Grey,grab:e=>e.parent},lk={draw:tT.draw,offscreen:tT.offscreen,overlaps:tT.overlaps,dragOnly:!0,width:lb.size/2,x:lb.size/2+lb.size/4,height:lb.size/3,y:lb.size/2-lb.size/3/2,colour:Colour.Grey,grab:e=>e.parent},lE=(e,t)=>{switch(t=!t,e){case"right":return t?"down":"up";case"down":return t?"left":"right";case"left":return t?"up":"down";case"up":return t?"right":"left"}throw Error("Invalid rotation or clockwiseness")},lz={hasBorder:!0,colour:Colour.Black,borderColour:Colour.Black,draw(e){tN.draw(e)},touch:e=>(e.colour=Colour.Silver,e),click(e){let t=e.parent;e.colour=Colour.Black,t.direction=lE(t.direction,!0),e.value=!0,t.updateValue(t);let l=t.parent;l.isSquare&&l.receiveNumber(l,t.value,t.channelId,{expanded:t.expanded,numberAtom:t})},offscreen:tN.offscreen,overlaps:tN.overlaps,value:!1,size:tD.size-1.5*tX,grab:e=>e.parent,x:lR.x+lR.width/2-(tD.size-1.5*tX)/2,y:lR.y+1.5*tX/2},lO={hasBorder:!0,colour:Colour.Black,borderColour:Colour.Black,draw(e){tU.draw(e)},touch:e=>(e.colour=Colour.Silver,e),click(e){let t=e.parent;e.colour=Colour.Black,t.direction=lE(t.direction,!1),e.value=!0,t.updateValue(t);let l=t.parent;l.isSquare&&l.receiveNumber(l,t.value,t.channelId,{expanded:t.expanded,numberAtom:t})},offscreen:tU.offscreen,overlaps:tU.overlaps,value:!1,size:tD.size-1.5*tX,grab:e=>e.parent,x:lR.x+lR.width/2-(tD.size-1.5*tX)/2,y:lR.y+lR.height-(tD.size-1.5*tX)-tX/2},lD={hasBorder:!0,borderColour:Colour.Black,colour:Colour.Grey,draw(e){e.colour=e.value?Colour.Silver:Colour.Grey,tk.draw(e),e.drawX(e)},drawX(e){let{x:t,y:l}=tx(e),i=e.size,o=l+e.size/2-1*tR/2;eJ.fillStyle=e.borderColour,eJ.fillRect(t,o,i,1*tR)},offscreen:tk.offscreen,overlaps:tk.overlaps,expanded:!1,click(e){e.value=!e.value;let[t,l,i]=lw(e.parent.value);t=e.value?100:0,e.parent.value=t+l+i;let o=e.parent;if(o.parent!==ts){let a=o.parent;lg(a)}},value:!1,size:tD.size-tX,grab:e=>e.parent,x:lT.x+lT.width/2-(tD.size-tX)/2,y:lT.y+tX/2},lP={hasBorder:!0,borderColour:Colour.Black,colour:Colour.Grey,draw(e){e.colour=e.value?Colour.Silver:Colour.Grey,tk.draw(e),e.drawY(e)},drawY(e,t=e.size,l=0){let{x:i,y:o}=tx(e),a=i+e.size/2-1*tR/2;eJ.fillStyle=e.borderColour,eJ.fillRect(a,o+l,1*tR,t)},offscreen:tk.offscreen,overlaps:tk.overlaps,expanded:!1,click(e){e.value=!e.value;let[t,l,i]=lw(e.parent.value);l=e.value?10:0,e.parent.value=t+l+i;let o=e.parent;if(o.parent!==ts){let a=o.parent;lg(a)}},value:!1,size:tD.size-tX,grab:e=>e.parent,x:lT.x+lT.width/2-(tD.size-tX)/2,y:tX/2},lA={hasBorder:!0,borderColour:Colour.Black,colour:Colour.Grey,draw(e){e.colour=e.value?Colour.Silver:Colour.Grey,tk.draw(e),e.drawR(e)},drawR(e){let{x:t,y:l}=tx(e),i=t+e.size/2,o=l+e.size/2,a=e.size/2-3*tR;eJ.fillStyle=e.borderColour,eJ.beginPath(),eJ.arc(i,o,a,0,2*Math.PI),eJ.fill(),a-=tR,eJ.fillStyle=e.colour,eJ.beginPath(),eJ.arc(i,o,a,0,2*Math.PI),eJ.fill()},offscreen:tk.offscreen,overlaps:tk.overlaps,expanded:!1,click(e){e.value=!e.value;let[t,l,i]=lw(e.parent.value);i=e.value?1:0,e.parent.value=t+l+i;let o=e.parent;if(o.parent!==ts){let a=o.parent;lg(a)}},value:!1,size:tD.size-tX,grab:e=>e.parent,x:lT.x+lT.width/2-(tD.size-tX)/2,y:lT.y+lT.height-(tD.size-tX)-tX/2},lI=e=>{let t=tc({...tD});if(t.value=Q(e),void 0!==t.value){if(void 0!==t.value.joins)for(let l of t.value.joins){let i=lI(l);t.joins.push(i)}t.stamp=t.value.stamp}if(!t.value.isDiagram)for(let o=0;o<3;o++){let a=t.value.channels[o];if(void 0===a||void 0===a.variable)continue;let r=tc(tY);t.variableAtoms[o]=r,r.highlightedSlot=tj[o],r.channelId=o;let n=o-1<0?tj[2]:tj[o-1],d=o+1>2?tj[0]:tj[o+1];a.subtract?r.direction="down":a.add?r.direction="up":a.variable===n?r.direction="left":a.variable===d&&(r.direction="right"),r.updateValue(r)}return void 0!==t.value&&t.value.isDiagram&&t.update(t),t},lB=10,lL={element:tD,draw(e){(e.previousBrushColour!==state.brush.colour||e.toolbarNeedsColourUpdate)&&e.update(e),e.unlocked&&e.element.draw(e)},overlaps:(e,t,l)=>e.element.overlaps(e,t,l),grab:(e,t,l)=>e,drag(e){if(e===squareTool){let t=lI(e.value);return tp(t),t}let l=tc({...e.element,x:e.x,y:e.y});return tp(l),l.value,l},cursor:()=>"move"},lG=0,lN=(e,t)=>{let{width:l=tD.size,height:i=tD.size,size:o}=e,a=tH;i{let t=unlocks[e];t.unlocked||(t.unlocked=!0,t.grabbable=!0)};squareTool=lN(tD),lB+=tR;let l7=lN(tY,"triangle");lB-=tR;let lY=lN(lb,"circle"),lX=lN(t2,"hexagon"),l3={};lm(),squareTool.value=makeArrayFromSplash(state.brush.colour),lY.borderScale=1,squareTool.update=e=>{if(void 0===e.joinDrawId&&(e.joinDrawId=-1,e.joinDrawTimer=0),void 0!==e.value&&e===squareTool&&(e.previousBrushColour!==state.brush.colour||e.toolbarNeedsColourUpdate)){for(let t of(e.previousBrushColour=state.brush.colour,void 0===e.multiAtoms&&(e.multiAtoms=[]),e.multiAtoms))tb(e,t);if(e.multiAtoms=[],e.value.isDiagram){let l=e.value,[i,o]=eo(l),a=e.width/i,r=e.height/o;for(let n of l.left){let d=tw(e,tD);d.x=n.x*a,d.y=n.y*r,d.width=n.width*a,d.height=n.height*r,d.value=n.content,d.update(d),e.multiAtoms.push(d)}}}let h=Q(e.value);if(e.colours=M(h),e.colourId>=e.colours.length&&(e.colourId=0),e.toolbarNeedsColourUpdate&&e===squareTool){for(let s of(e.toolbarNeedsColourUpdate=!1,e.isGradient=!0,e.joins=[],e.value.joins)){let c=lI(s);e.joins.push(c)}tD.updateGradient(e)}else e.colour=Colour.splash(999),e.borderColour=Colour.splash(999)},l7.update=squareTool.update,lY.update=squareTool.update,l3.update=squareTool.update,lX.update=squareTool.update,on.keydown(e=>{(e.ctrlKey||e.metaKey)&&"s"===e.key?(e.preventDefault(),lW()):(e.ctrlKey||e.metaKey)&&"o"===e.key?(e.preventDefault(),lq()):(e.ctrlKey||e.metaKey)&&"c"===e.key&&(e.preventDefault(),l2())},{passive:!1}),on.paste(async e=>{let t=e.clipboardData.getData("text");if(""!==t){lj(t);return}let l=e.clipboardData.items[0],i=l.getAsFile(),o=await i.text();lj(o)}),on.dragover(e=>{e.stopPropagation(),e.preventDefault()},{passive:!1}),on.drop(async e=>{e.stopPropagation(),e.preventDefault();let t=e.dataTransfer.items[0],l=t.getAsFile(),i=await l.text();lj(i)},{passive:!1});let l1={},lH={};l1.cellAtoms=(e,t)=>{let l=[];for(let i of t)l.push({isLeftSlot:i.isLeftSlot,value:i.value,x:i.x,y:i.y,slotted:i.slotted?i.slotted.value:void 0});return l};let l8=!1;lH.cellAtoms=(e,t)=>{let l=[];for(let i of t){l8||(i.isLeftSlot||tO(i.value),l8=!0);let o=i.isLeftSlot?tc(la):lI(i.value);if(o.isLeftSlot=i.isLeftSlot,tp(o),t0(e,o),o.attached=!0,o.x=i.x,o.y=i.y,o.highlightedSide="left",l.push(o),void 0!==i.slotted){let a=lI(i.slotted);tp(a),t0(e,a),a.attached=!0,a.cellAtom=o,a.highlightedSide="slot",a.slottee=!0,o.slotted=a}}return l},l1.symmetryCircle=(e,t)=>{if(void 0!==t)return t.value},lH.symmetryCircle=(e,t)=>{let l=tw(e,lb);return l.value=t,l},l1.chance=(e,t)=>{if(void 0!==t)return t.ons},lH.chance=(e,t)=>{let l=tw(e,t2);return l.ons=t,l};l1.expanded=(e,t)=>t,l1.x=(e,t)=>t,l1.y=(e,t)=>t,l1.width=(e,t)=>t,l1.height=(e,t)=>t,l1.hasSymmetry=(e,t)=>t,lH.expanded=(e,t)=>t,lH.x=(e,t)=>t,lH.y=(e,t)=>t,lH.width=(e,t)=>t,lH.height=(e,t)=>t,lH.hasSymmetry=(e,t)=>t,l1.pinhole=(e,t)=>t.locked,lH.pinhole=(e,t)=>(e.pinhole.locked=t,e.pinhole),l1.rightTriangle=(e,t)=>void 0!==t,lH.rightTriangle=(e,t)=>{if(!t)return;let l=tw(e,tY);return l};let lV=()=>{let e=[];for(let t of paddles){let l={};for(let i in t){let o=l1[i];if(void 0===o)continue;let a=o(t,t[i]);void 0!==a&&(l[i]=a)}e.push(l)}return JSON.stringify(e)},lj=e=>{if(middleClicked){middleClicked=!1;return}l8=!1,lU("triangle"),lU("circle"),lU("hexagon");try{for(;paddles.length>0;)lv(paddles[paddles.length-1]);for(let t of JSON.parse(e)){let l=lm();for(let i in t){let o=lH[i];if(void 0===o)continue;let a=o(l,t[i]);void 0!==a&&(l[i]=a)}ln(l),lg(l)}lf()}catch(r){console.error(r),alert("Error loading rules... Sorry! Please contact @todepond :)")}},lW=async()=>{let e=lV(paddles);if(window.showSaveFilePicker)try{let t=await showSaveFilePicker({excludeAcceptAllOption:!0,suggestedName:"spell",startIn:"downloads",types:[{description:"JSON",accept:{"application/json":[".json"]}}]}),l=await t.createWritable();await l.write(e),await l.close()}catch(i){console.error("Failed to save file:",i)}else{let o=new Blob([e],{type:"application/json"}),a=URL.createObjectURL(o),r=document.createElement("a");r.href=a,r.download="spell.json",r.click(),URL.revokeObjectURL(a)}},lq=()=>{let e=document.createElement("input");e.type="file",e.onchange=async t=>{let l=e.files[0],i=await l.text();lj(i),Keyboard.Control=!1},e.click(),Keyboard.Control=!1},l2=()=>{let e=lV(paddles);print(e),navigator.clipboard.writeText(e)}});