UNPKG

chessground12

Version:
2 lines (1 loc) 33.2 kB
var Fe=["white","black","none"];var Se=["a","b","c","d","e","f","g","h","i","j","k","l"],le=["1","2","3","4","5","6","7","8","9","10","11","12"],Ie=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];var O=[{width:12,height:12},{width:8,height:8},{width:6,height:6}],We=["mousedown","touchmove"],Ue=["click"];var rr=[...le].reverse(),jo=[1,2,3,4,5,6,7,8,9,10,11,12],nr=[...jo].reverse();function Zo(e){return Se.slice(0,e)}function Qo(e){return le.slice(0,e)}function Me(e){let o=O[e];return Array.prototype.concat(...Zo(o.width).map(t=>Qo(o.height).map(r=>t+r)))}function X(e){return Me(e).map(g)}var V=e=>Se[e[0]]+le[e[1]],g=e=>{if(e.length==2)return[e.charCodeAt(0)-97,e.charCodeAt(1)-49];{let o=parseInt(e.slice(1));return[e.charCodeAt(0)-97,o-1]}};function De(e){return e.replace("+","p").toLowerCase()+"-piece"}function xe(e,o=!1){let t=e.slice(0,e.indexOf("-")),r=t.length>1?t.replace("p","+"):t;return o?r.toUpperCase():r}function Y(e){return xe(e,!0)+"@"}function ze(e){return["k-piece"]}function je(e){let o,t=()=>(o===void 0&&(o=e()),o);return t.clear=()=>{o=void 0},t}var Ze=()=>{let e;return{start(){e=performance.now()},cancel(){e=void 0},stop(){if(!e)return 0;let o=performance.now()-e;return e=void 0,o}}},_=e=>e==="white"?"black":"white",ee=(e,o)=>e.role===o.role&&e.color===o.color&&e.promoted===o.promoted,Xo=(e,o)=>e.color===o?"ally":"enemy",k=(e,o)=>`${e.color} ${Xo(e,o)} ${e.promoted?"promoted ":""}${e.role}`,I=(e,o)=>{let t=e[0]-o[0],r=e[1]-o[1];return t*t+r*r},oe=(e,o)=>(t,r)=>[(r?t[0]:o.width-1-t[0])*e.width/o.width,(r?o.height-1-t[1]:t[1])*e.height/o.height],C=(e,o)=>{e.style.transform=`translate(${o[0]}px,${o[1]}px)`},te=(e,o)=>{e.style.visibility=o?"visible":"hidden"},A=e=>{if(e.clientX||e.clientX===0)return[e.clientX,e.clientY];if(e.targetTouches?.[0])return[e.targetTouches[0].clientX,e.targetTouches[0].clientY]},pe=e=>e.buttons===2||e.button===2,D=(e,o)=>{let t=document.createElement(e);return o&&(t.className=o),t},Qe=e=>Array.from(e.classList).includes("mini");function ue(e,o,t,r){let n=g(e);return o||(n[0]=r.width-1-n[0],n[1]=r.height-1-n[1]),[t.left+t.width*(n[0]+.5)/r.width,t.top+t.height*(r.height-n[1]-.5)/r.height]}function q(e,o){return Math.abs(e-o)}function Yo(e){return(o,t,r,n)=>q(o,r)<2&&(e==="white"?n===t+1||t<=1&&n===t+2&&o===r:n===t-1||t>=6&&n===t-2&&o===r)}var re=(e,o,t,r)=>{let n=q(e,t),i=q(o,r);return n===1&&i===2||n===2&&i===1},_o=(e,o,t,r)=>{let n=q(e,t),i=q(o,r);return n===1&&i===4||n===4&&i===1},ke=(e,o,t,r)=>q(e,t)===q(o,r),Ce=(e,o,t,r)=>e===t||o===r,Ke=(e,o,t,r)=>ke(e,o,t,r)||Ce(e,o,t,r);function Jo(e,o,t){return(r,n,i,a)=>q(r,i)<2&&q(n,a)<2||t&&n===a&&n===(e==="white"?0:7)&&(r===4&&(i===2&&o.includes(0)||i===6&&o.includes(7))||o.includes(i))}function et(e,o){let t=o==="white"?"1":"8",r=[];for(let[n,i]of e)n[1]===t&&i.color===o&&i.role==="r-piece"&&r.push(g(n)[0]);return r}var ot=(e,o,t,r)=>ke(e,o,t,r)||re(e,o,t,r),tt=(e,o,t,r)=>Ce(e,o,t,r)||re(e,o,t,r);function Ee(e,o,t,r,n){let i=e.get(o),a=i.role,c=i.color,s=g(o),l=()=>!1;switch(n){default:switch(a){case"p-piece":l=Yo(c);break;case"r-piece":l=Ce;break;case"n-piece":l=re;break;case"b-piece":l=ke;break;case"q-piece":l=Ke;break;case"c-piece":l=tt;break;case"h-piece":case"a-piece":l=ot;break;case"g-piece":l=_o;break;case"k-piece":l=Jo(c,et(e,c),t);break}}return X(r).filter(d=>(s[0]!==d[0]||s[1]!==d[1])&&l(s[0],s[1],d[0],d[1])).map(V)}var rt=()=>!0;function W(e,o,t,r){let n=o.color,i=rt;switch(r){default:console.warn("Unknown drop variant",r)}return X(t).filter(c=>e.get(V(c))?.color!==n&&i(c[0],c[1])).map(V)}var nt=["green","red","blue","yellow"];function Xe(e,o){if(o.touches&&o.touches.length>1)return;o.stopPropagation(),o.preventDefault(),o.ctrlKey?M(e):ne(e);let t=A(o),r=K(t,y(e),e.dom.bounds(),e.geometry);r&&(e.drawable.current={orig:r,pos:t,brush:it(o),snapToValidMove:e.drawable.defaultSnapToValidMove},Ye(e))}function Ye(e){requestAnimationFrame(()=>{let o=e.drawable.current;if(o){let t=K(o.pos,y(e),e.dom.bounds(),e.geometry);t||(o.snapToValidMove=!1);let r=o.snapToValidMove?to(o.orig,o.pos,y(e),e.dom.bounds(),e.geometry):t;r!==o.mouseSq&&(o.mouseSq=r,o.dest=r!==o.orig?r:void 0,e.dom.redrawNow()),Ye(e)}})}function _e(e,o){e.drawable.current&&(e.drawable.current.pos=A(o))}function Je(e){let o=e.drawable.current;o&&(o.mouseSq&&at(e.drawable,o),Ae(e))}function Ae(e){e.drawable.current&&(e.drawable.current=void 0,e.dom.redraw())}function eo(e){e.drawable.shapes.length&&(e.drawable.shapes=[],e.dom.redraw(),oo(e.drawable))}function it(e){let o=(e.shiftKey||e.ctrlKey)&&pe(e),t=e.altKey||e.metaKey||e.getModifierState?.("AltGraph");return nt[(o?1:0)+(t?2:0)]}function at(e,o){let t=n=>n.orig===o.orig&&n.dest===o.dest,r=e.shapes.find(t);r&&(e.shapes=e.shapes.filter(n=>!t(n))),(!r||r.brush!==o.brush)&&e.shapes.push(o),oo(e)}function oo(e){e.onChange&&e.onChange(e.shapes)}function N(e,o){return o.animation.enabled?dt(e,o):G(e,o)}function G(e,o){let t=e(o);return o.dom.redraw(),t}function Ne(e,o){return{key:e,pos:g(e),piece:o}}function st(e,o){return o.sort((t,r)=>I(e.pos,t.pos)-I(e.pos,r.pos))[0]}function ct(e,o){let t=new Map,r=[],n=new Map,i=[],a=[],c=new Map,s,l,p;for(let[d,m]of e)c.set(d,Ne(d,m));for(let d of Me(o.geometry))s=o.pieces.get(d),l=c.get(d),s?l?ee(s,l.piece)||(i.push(l),a.push(Ne(d,s))):a.push(Ne(d,s)):l&&i.push(l);for(let d of a)l=st(d,i.filter(m=>ee(d.piece,m.piece))),l&&(p=[l.pos[0]-d.pos[0],l.pos[1]-d.pos[1]],t.set(d.key,p.concat(p)),r.push(l.key));for(let d of i)r.includes(d.key)||n.set(d.key,d.piece);return{anims:t,fadings:n}}function ro(e,o){let t=e.animation.current;if(t===void 0){e.dom.destroyed||e.dom.redrawNow();return}let r=1-(o-t.start)*t.frequency;if(r<=0)e.animation.current=void 0,e.dom.redrawNow();else{let n=lt(r);for(let i of t.plan.anims.values())i[2]=i[0]*n,i[3]=i[1]*n;e.dom.redrawNow(!0),requestAnimationFrame((i=performance.now())=>ro(e,i))}}function dt(e,o){let t=new Map(o.pieces),r=e(o),n=ct(t,o);if(n.anims.size||n.fadings.size){let i=o.animation.current&&o.animation.current.start;o.animation.current={start:performance.now(),frequency:1/o.animation.duration,plan:n},i||ro(o,performance.now())}else o.dom.redraw();return r}function lt(e){return e<.5?4*e*e*e:(e-1)*(2*e-2)*(2*e-2)+1}function no(e,o){if(!o.isTrusted||o.button!==void 0&&o.button!==0||o.touches&&o.touches.length>1)return;let t=e.dom.bounds(),r=A(o),n=K(r,y(e),t,e.geometry);if(!n)return;let i=e.pieces.get(n),a=e.selected;!a&&e.drawable.enabled&&(e.drawable.eraseOnClick||!i||i.color!==e.turnColor)&&eo(e),o.cancelable!==!1&&(!o.touches||e.blockTouchScroll||i||a||pt(e,r))&&o.preventDefault();let c=!!e.premovable.current,s=!!e.predroppable.current;e.stats.ctrlKey=o.ctrlKey,e.selected&&he(e,e.selected,n)?N(d=>ie(d,n),e):ie(e,n);let l=e.selected===n,p=co(e,n);if(i&&p&&l&&lo(e,n)){e.draggable.current={orig:n,piece:i,origPos:r,pos:r,started:e.draggable.autoDistance&&e.stats.dragged,element:p,previouslySelected:a,originTarget:o.target,keyHasChanged:!1},p.cgDragging=!0,p.classList.add("dragging");let d=e.dom.elements.ghost;d&&(d.className="ghost "+k(i,e.orientation),C(d,oe(t,e.dimensions)(g(n),y(e))),te(d,!0)),He(e)}else c&&H(e),s&&R(e);e.dom.redraw()}function pt(e,o){let t=y(e),r=e.dom.bounds(),n=Math.pow(r.width/e.dimensions.width,2);for(let i of e.pieces.keys()){let a=ue(i,t,r,e.dimensions);if(I(a,o)<=n)return!0}return!1}function me(e,o,t,r){let n="a0";e.pieces.set(n,o),e.dom.redraw();let i=A(t);e.draggable.current={orig:n,piece:o,origPos:i,pos:i,started:!0,element:()=>co(e,n),originTarget:t.target,newPiece:!0,force:!!r,keyHasChanged:!1},ge(e)&&(e.predroppable.dropDests=W(e.pieces,o,e.geometry,e.variant)),He(e)}function He(e){requestAnimationFrame(()=>{let o=e.draggable.current;if(!o)return;e.animation.current?.plan.anims.has(o.orig)&&(e.animation.current=void 0);let t=e.pieces.get(o.orig);if(!t||!ee(t,o.piece))U(e);else if(!o.started&&I(o.pos,o.origPos)>=Math.pow(e.draggable.distance,2)&&(o.started=!0),o.started){if(typeof o.element=="function"){let n=o.element();if(!n)return;n.cgDragging=!0,n.classList.add("dragging"),o.element=n}let r=e.dom.bounds();C(o.element,[o.pos[0]-r.left-r.width/(2*e.dimensions.width),o.pos[1]-r.top-r.height/(2*e.dimensions.height)]),o.keyHasChanged||=o.orig!==K(o.pos,y(e),r,e.geometry)}He(e)})}function io(e,o){e.draggable.current&&(!o.touches||o.touches.length<2)&&(e.draggable.current.pos=A(o))}function ao(e,o){let t=e.draggable.current;if(!t)return;if(o.type==="touchend"&&o.cancelable!==!1&&o.preventDefault(),o.type==="touchend"&&t.originTarget!==o.target&&!t.newPiece){e.draggable.current=void 0;return}H(e),R(e);let r=A(o)||t.pos,n=K(r,y(e),e.dom.bounds(),e.geometry);n&&t.started&&t.orig!==n?t.newPiece?fe(e,t.orig,n,t.force):(e.stats.ctrlKey=o.ctrlKey,Re(e,t.orig,n)&&(e.stats.dragged=!0)):t.newPiece?e.pieces.delete(t.orig):e.draggable.deleteOnDropOff&&!n&&(e.pieces.delete(t.orig),x(e.events.change)),(t.orig===t.previouslySelected||t.keyHasChanged)&&(t.orig===n||!n)?M(e):e.selectable.enabled||M(e),so(e),e.draggable.current=void 0,e.dom.redraw()}function U(e){let o=e.draggable.current;o&&(o.newPiece&&e.pieces.delete(o.orig),e.draggable.current=void 0,M(e),so(e),e.dom.redraw())}function so(e){let o=e.dom.elements;o.ghost&&te(o.ghost,!1)}function co(e,o){let t=e.dom.elements.board.firstChild;for(;t;){if(t.cgKey===o&&t.tagName==="PIECE")return t;t=t.nextSibling}}function po(e,o){if(e.dropmode.active=!0,e.dropmode.piece=o,U(e),M(e),o){if(ge(e))e.predroppable.dropDests=W(e.pieces,o,e.geometry,e.variant);else if(e.movable.dests){let t=new Map([[o.role,e.movable.dests.get(Y(o.role))]]);e.dropmode.active=!0,e.dropmode.dropDests=t}}}function j(e){e.dropmode.active=!1}function uo(e,o){if(!e.dropmode.active)return;H(e),R(e);let t=e.dropmode.piece;if(t){e.pieces.set("a0",t);let r=A(o),n=r&&K(r,y(e),e.dom.bounds(),e.geometry);n&&fe(e,"a0",n)}e.dom.redraw()}function x(e,...o){e&&setTimeout(()=>e(...o),1)}function mo(e){e.orientation=_(e.orientation),e.animation.current=e.draggable.current=e.selected=void 0}function fo(e,o){e.pieces.clear();for(let[t,r]of o)r?e.pieces.set(t,r):e.pieces.delete(t)}function ho(e,o){for(let[t,r]of o)r?e.plinths?.set(t,r):e.plinths?.delete(t)}function go(e,o,t){e.lastMove=[o,t]}function bo(e,o){let t=ze(e.variant);if(e.check=void 0,o===!0&&(o=e.turnColor),o){for(let[r,n]of e.pieces)if(t.includes(n.role)&&n.color===o){e.check=r;break}}}function ut(e,o,t,r){R(e),e.premovable.current=[o,t],x(e.premovable.events.set,o,t,r)}function H(e){e.premovable.current&&(e.premovable.current=void 0,x(e.premovable.events.unset))}function mt(e,o,t){H(e),e.predroppable.current={role:o,key:t},x(e.predroppable.events.set,o,t)}function R(e){let o=e.predroppable;o.current&&(o.current=void 0,x(o.events.unset))}function Le(e,o,t){let r=e.pieces.get(o),n=e.pieces.get(t);if(o===t||!r)return!1;let i=n&&n.color!==r.color?n:void 0;return t===e.selected&&M(e),x(e.events.move,o,t,i),console.log(e.autoCastle),e.autoCastle||(e.pieces.set(t,r),e.pieces.delete(o),console.log("wkkkkkk")),e.lastMove=[o,t],e.check=void 0,x(e.events.change),i||!0}function be(e,o,t,r){if(e.pieces.has(t))if(r)e.pieces.delete(t);else return!1;return x(e.events.dropNewPiece,o,t),e.pieces.set(t,o),e.lastMove=[t],e.check=void 0,x(e.events.change),e.movable.dests=void 0,e.dropmode.dropDests=void 0,e.turnColor=_(e.turnColor),!0}function vo(e,o,t){let r=Le(e,o,t);return r&&(e.movable.dests=void 0,e.dropmode.dropDests=void 0,e.turnColor=_(e.turnColor),e.animation.current=void 0),r}function Re(e,o,t){if(he(e,o,t)){let r=vo(e,o,t);if(r){let n=e.hold.stop();M(e);let i={premove:!1,ctrlKey:e.stats.ctrlKey,holdTime:n};return r!==!0&&(i.captured=r),x(e.movable.events.after,o,t,i),!0}}else if(ft(e,o,t))return ut(e,o,t,{ctrlKey:e.stats.ctrlKey}),M(e),!0;return M(e),!1}function fe(e,o,t,r){let n=e.pieces.get(o);n&&(Po(e,n.role,t)||r)?(e.pieces.delete(o),be(e,n,t,r),e.dropmode.active=!1,x(e.movable.events.afterNewPiece,n.role,t,{premove:!1,predrop:!1})):n&&ht(e,o,t)?mt(e,n.role,t):(H(e),R(e),j(e)),e.pieces.delete(o),M(e)}function ie(e,o,t){if(x(e.events.select,o),e.selected){if(e.selected===o&&!e.draggable.enabled){M(e),e.hold.cancel();return}else if((e.selectable.enabled||t)&&e.selected!==o&&Re(e,e.selected,o)){e.stats.dragged=!1;return}}(yo(e,o)||Be(e,o))&&(Te(e,o),e.hold.start())}function Te(e,o){e.selected=o,Be(e,o)?e.premovable.dests=Ee(e.pieces,o,e.premovable.castle,e.geometry,e.variant):(e.premovable.dests=void 0,e.predroppable.dropDests=void 0)}function M(e){e.selected=void 0,e.premovable.dests=void 0,e.predroppable.dropDests=void 0,e.hold.cancel()}function yo(e,o){let t=e.pieces.get(o);return!!t&&(e.movable.color==="both"||e.movable.color===t.color&&e.turnColor===t.color)}function he(e,o,t){return o!==t&&yo(e,o)&&(e.movable.free||!!e.movable.dests?.get(o)?.includes(t))}function Po(e,o,t){return e.movable.free?!0:!!e.movable.dests?.get(Y(o))?.includes(t)}function Be(e,o){let t=e.pieces.get(o);return!!t&&e.premovable.enabled&&e.movable.color===t.color&&e.turnColor!==t.color}function ge(e){let o=e.dropmode.active?e.dropmode.piece:e.draggable.current?.piece;return!!o&&(e.dropmode.active||e.draggable.current?.orig==="a0")&&e.predroppable.enabled&&e.movable.color===o.color&&e.turnColor!==o.color}function ft(e,o,t){return o!==t&&Be(e,o)&&Ee(e.pieces,o,e.premovable.castle,e.geometry,e.variant).includes(t)}function ht(e,o,t){let r=e.pieces.get(o),n=e.pieces.get(t);return!!r&&(!n||n.color!==e.movable.color)&&e.predroppable.enabled&&e.movable.color===r.color&&e.turnColor!==r.color&&W(e.pieces,r,e.geometry,e.variant).includes(t)}function lo(e,o){let t=e.pieces.get(o);return!!t&&e.draggable.enabled&&(e.movable.color==="both"||e.movable.color===t.color&&(e.turnColor===t.color||e.premovable.enabled))}function wo(e){let o=e.premovable.current;if(!o)return!1;let t=o[0],r=o[1],n=!1;if(he(e,t,r)){let i=vo(e,t,r);if(i){let a={premove:!0};i!==!0&&(a.captured=i),x(e.movable.events.after,t,r,a),n=!0}}return H(e),n}function So(e){let o=e.predroppable.current,t=!1;if(!o)return!1;if(Po(e,o.role,o.key)){let r={role:o.role,color:e.movable.color};be(e,r,o.key)&&(x(e.movable.events.afterNewPiece,o.role,o.key,{premove:!1,predrop:!0}),t=!0)}return R(e),t}function ne(e){H(e),R(e),M(e)}function Oe(e){e.movable.color=e.movable.dests=e.dropmode.dropDests=e.animation.current=void 0,ne(e)}function K(e,o,t,r){let n=O[r],i=Math.floor(n.width*(e[0]-t.left)/t.width);o||(i=n.width-1-i);let a=n.height-1-Math.floor(n.height*(e[1]-t.top)/t.height);return o||(a=n.height-1-a),i>=0&&i<n.width&&a>=0&&a<n.height?V([i,a]):void 0}function to(e,o,t,r,n){let i=g(e),a=X(n).filter(d=>Ke(i[0],i[1],d[0],d[1])||re(i[0],i[1],d[0],d[1])),c=O[n],l=a.map(d=>ue(V(d),t,r,c)).map(d=>I(o,d)),[,p]=l.reduce((d,m,h)=>d[0]<m?d:[m,h],[l[0],0]);return V(a[p])}function y(e){return e.orientation==="white"}function Do(e,o){let t=e.split(" ")[0],r=t.indexOf("["),n=r!==-1?t.slice(r):"",i={},a=o("white"),c=o("black");if(a){i.white={};for(let s of a)i.white[De(s)]=Mo(n,s,"upper")}if(c){i.black={};for(let s of c)i.black[De(s)]=Mo(n,s,"lower")}return i}function Mo(e,o,t){t==="upper"?o=o.toUpperCase():t==="lower"&&(o=o.toLowerCase());let r=0;for(let n=0;n<e.length;n++)e.charAt(n)===o&&(r+=1);return r}function xo(e,o){let t=e.getAttribute("data-role"),r=e.getAttribute("data-color");e.setAttribute("data-nb",""+(o.pockets[r][t]??0));let n=o.dropmode,i=o.dropmode.piece,a=n.active&&i?.role===t&&i.color===r,c=o.predroppable.current?.role;r===o.movable.color&&c===t?e.classList.add("premove"):e.classList.remove("premove"),a?e.classList.add("selected-square"):e.classList.remove("selected-square")}function ko(e,o,t,r){function n(i,a){if(!e.pockets)return;let c=a==="top"?_(e.orientation):e.orientation,s=e.pockets[c];if(!s)return;let l=Object.keys(s),p=String(l.length),d=String(e.dimensions.width),m=String(e.dimensions.height);i.setAttribute("style",`--pocketLength: ${p}; --files: ${d}; --ranks: ${m}`),i.classList.add("pocket",a,"usable"),l.forEach(h=>{let z=k({role:h,color:c,promoted:!1},e.orientation),v=D("piece",z);v.setAttribute("data-color",c),v.setAttribute("data-role",h),xo(v,e),We.forEach(u=>v.addEventListener(u,$=>{(e.movable.free||e.movable.color===c)&&bt(e,$)})),Ue.forEach(u=>v.addEventListener(u,$=>{e.movable.color===c&&gt(e,$)})),i.appendChild(v)})}t&&(t.innerHTML="",o.pocketTop=t,n(o.pocketTop,"top")),r&&(r.innerHTML="",o.pocketBottom=r,n(o.pocketBottom,"bottom"))}function gt(e,o){if(o.button!==void 0&&o.button!==0)return;let t=o.target,r=t.getAttribute("data-role"),n=t.getAttribute("data-color");if(t.getAttribute("data-nb")==="0")return;let a=e.dropmode,c=e.dropmode.piece,s=t.getAttribute("canceledDropMode");t.setAttribute("canceledDropMode",""),(!a.active||c?.role!==r)&&s!=="true"?po(e,{color:n,role:r}):j(e),o.stopPropagation(),o.preventDefault()}function bt(e,o){if(o.button!==void 0&&o.button!==0)return;let t=o.target,r=t.getAttribute("data-role"),n=t.getAttribute("data-color"),i=Number(t.getAttribute("data-nb"));if(t.setAttribute("canceledDropMode",""),i!==0){if(e.events.pocketSelect({role:r,color:n}),e.dropmode.active&&(j(e),e.dropmode.piece?.role===r&&t.setAttribute("canceledDropMode","true")),e.movable.dests){let a=new Map([[r,e.movable.dests.get(xe(r,!0)+"@")]]);e.dropmode.dropDests=a}o.stopPropagation(),o.preventDefault(),me(e,{color:n,role:r},o)}}function Co(e){function o(t){let r=t?.firstChild;for(;r;)xo(r,e),r=r.nextSibling}o(e.dom.elements.pocketBottom),o(e.dom.elements.pocketTop)}function Ko(e){let o=e.draggable.current?.piece;if(o&&o.color!==e.turnColor){let t=W(e.pieces,o,e.geometry,e.variant);e.predroppable.dropDests=t}}function Ve(e,o){o.animation&&(qe(e.animation,o.animation),(e.animation.duration||0)<70&&(e.animation.enabled=!1))}function ve(e,o){if(o.movable?.dests&&(e.movable.dests=void 0),o.dropmode?.dropDests&&(e.dropmode.dropDests=void 0),o.drawable?.autoShapes&&(e.drawable.autoShapes=[]),qe(e,o),o.geometry&&(e.dimensions=O[o.geometry]),o.fen){let t=new Map,r=e.pieces.get("a0");r!==void 0&&t.set("a0",r),e.pieces=t,e.drawable.shapes=[],e.pocketRoles&&(e.pockets=Do(o.fen,e.pocketRoles))}if("check"in o&&bo(e,o.check||!1),"lastMove"in o&&!o.lastMove?e.lastMove=void 0:o.lastMove&&(e.lastMove=o.lastMove),e.selected&&Te(e,e.selected),Ko(e),Ve(e,o),!e.movable.rookCastle&&e.movable.dests){let t=e.movable.color==="white"?"1":"8",r="e"+t,n=e.movable.dests.get(r),i=e.pieces.get(r);if(!n||!i||i.role!=="k-piece")return;e.movable.dests.set(r,n.filter(a=>!(a==="a"+t&&n.includes("c"+t))&&!(a==="h"+t&&n.includes("g"+t))))}}function qe(e,o){for(let t in o)Eo(e[t])&&Eo(o[t])?qe(e[t],o[t]):e[t]=o[t]}function Eo(e){return typeof e=="object"}function Ao(e,o){function t(){e.plinthsPlaced=!1,mo(e),o()}return{set(r){r.orientation&&r.orientation!==e.orientation&&t(),Ve(e,r),(r.fen?N:G)(n=>ve(n,r),e)},state:e,toggleOrientation:t,setPieces(r){N(n=>fo(n,r),e)},setPlinths(r){N(n=>ho(n,r),e)},setLastMove(r,n){N(i=>go(i,r,n),e)},selectSquare(r,n){r?N(i=>ie(i,r,n),e):e.selected&&(M(e),e.dom.redraw())},move(r,n){N(i=>Le(i,r,n),e)},newPiece(r,n){N(i=>be(i,r,n),e)},playPremove(){if(e.premovable.current){if(N(wo,e))return!0;e.dom.redraw()}return!1},playPredrop(){if(e.predroppable.current){let r=So(e);return e.dom.redraw(),r}return!1},cancelPremove(){G(H,e)},cancelPredrop(){G(R,e)},cancelMove(){G(r=>{ne(r),U(r)},e)},stop(){G(r=>{Oe(r),U(r)},e)},setAutoShapes(r){G(n=>n.drawable.autoShapes=r,e)},setShapes(r){G(n=>n.drawable.shapes=r,e)},getKeyAtDomPos(r){return K(r,y(e),e.dom.bounds(),e.geometry)},redrawAll:o,dragNewPiece(r,n,i){me(e,r,n,i)},destroy(){Oe(e),e.dom.unbind&&e.dom.unbind(),e.dom.destroyed=!0}}}function No(){return{pieces:new Map,plinths:new Map,plinthsPlaced:!1,orientation:"white",turnColor:"white",coordinates:!0,autoCastle:!0,viewOnly:!1,disableContextMenu:!1,addPieceZIndex:!1,addDimensionsCssVars:!1,blockTouchScroll:!1,pieceKey:!1,highlight:{lastMove:!0,check:!0},animation:{enabled:!0,duration:200},movable:{free:!0,color:"both",showDests:!0,events:{},rookCastle:!0},premovable:{enabled:!0,showDests:!0,castle:!0,events:{}},predroppable:{enabled:!1,showDropDests:!0,events:{}},draggable:{enabled:!0,distance:3,autoDistance:!0,showGhost:!0,deleteOnDropOff:!1},dropmode:{active:!1,showDropDests:!0},selectable:{enabled:!0},stats:{dragged:!("ontouchstart"in window)},events:{},drawable:{enabled:!0,visible:!0,defaultSnapToValidMove:!0,eraseOnClick:!0,shapes:[],autoShapes:[],brushes:{green:{key:"g",color:"#15781B",opacity:1,lineWidth:10},red:{key:"r",color:"#882020",opacity:1,lineWidth:10},blue:{key:"b",color:"#003088",opacity:1,lineWidth:10},yellow:{key:"y",color:"#e68f00",opacity:1,lineWidth:10},paleBlue:{key:"pb",color:"#003088",opacity:.4,lineWidth:15},paleGreen:{key:"pg",color:"#15781B",opacity:.4,lineWidth:15},paleRed:{key:"pr",color:"#882020",opacity:.4,lineWidth:15},paleGrey:{key:"pgr",color:"#4a4a4a",opacity:.35,lineWidth:15}},pieces:{baseUrl:"https://lichess1.org/assets/piece/cburnett/"},prevSvgHash:""},hold:Ze(),dimensions:O[0],geometry:0,variant:"shuuro",chess960:!1,notation:0}}function E(e){return document.createElementNS("http://www.w3.org/2000/svg",e)}function Lo(e,o,t){let r=e.drawable,n=r.current,i=n&&n.mouseSq?n:void 0,a=new Map,c=e.dom.bounds();for(let h of r.shapes.concat(r.autoShapes).concat(i?[i]:[]))h.dest&&a.set(h.dest,(a.get(h.dest)||0)+1);let s=r.shapes.concat(r.autoShapes).map(h=>({shape:h,current:!1,hash:Ro(h,a,!1,c)}));i&&s.push({shape:i,current:!0,hash:Ro(i,a,!0,c)});let l=s.map(h=>h.hash).join(";");if(l===e.drawable.prevSvgHash)return;e.drawable.prevSvgHash=l;let p=o.querySelector("defs"),d=o.querySelector("g"),m=t.querySelector("g");yt(r,s,p),Ho(e,s.filter(h=>!h.shape.customSvg),r.brushes,a,d),Ho(e,s.filter(h=>h.shape.customSvg),r.brushes,a,m)}function yt(e,o,t){let r=new Map,n;for(let c of o)c.shape.dest&&(n=e.brushes[c.shape.brush],c.shape.modifiers&&(n=To(n,c.shape.modifiers)),r.set(n.key,n));let i=new Set,a=t.firstChild;for(;a;)i.add(a.getAttribute("cgKey")),a=a.nextSibling;for(let[c,s]of r.entries())i.has(c)||t.appendChild(Kt(s))}function Ho(e,o,t,r,n){let i=e.dom.bounds(),a=new Map,c=[];for(let p of o)a.set(p.hash,!1);let s=n.firstChild,l;for(;s;)l=s.getAttribute("cgHash"),a.has(l)?a.set(l,!0):c.push(s),s=s.nextSibling;for(let p of c)n.removeChild(p);for(let p of o)a.get(p.hash)||n.appendChild(Mt(e,p,t,r,i))}function Ro({orig:e,dest:o,brush:t,piece:r,modifiers:n,customSvg:i},a,c,s){return[s.width,s.height,c,e,o,t,o&&(a.get(o)||0)>1,r&&Pt(r),n&&wt(n),i&&St(i)].filter(l=>l).join(",")}function Pt(e){return[e.color,e.role,e.promoted,e.scale].filter(o=>o).join(",")}function wt(e){return""+(e.lineWidth||"")}function St(e){let o=0;for(let t=0;t<e.length;t++)o=(o<<5)-o+e.charCodeAt(t)>>>0;return"custom-"+o.toString()}function Mt(e,{shape:o,current:t,hash:r},n,i,a){let c;if(o.customSvg){let s=ye(g(o.orig),e.orientation,e.dimensions);c=Dt(o.customSvg,s,a,e.dimensions)}else if(o.piece)c=Ct(e.drawable.pieces.baseUrl,ye(g(o.orig),e.orientation,e.dimensions),o.piece,a,e.dimensions,e.orientation);else{let s=ye(g(o.orig),e.orientation,e.dimensions);if(o.dest){let l=n[o.brush];o.modifiers&&(l=To(l,o.modifiers)),c=kt(l,s,ye(g(o.dest),e.orientation,e.dimensions),t,(i.get(o.dest)||0)>1,a,e.dimensions)}else c=xt(n[o.brush],s,t,a,e.dimensions)}return c.setAttribute("cgHash",r),c}function Dt(e,o,t,r){let[n,i]=ae(o,t,r),a=L(E("g"),{transform:`translate(${n},${i})`}),c=L(E("svg"),{width:1,height:1,viewBox:"0 0 100 100"});return a.appendChild(c),c.innerHTML=e,a}function xt(e,o,t,r,n){let i=ae(o,r,n),a=Et(),c=(r.width+r.height)/(4*Math.max(r.width,r.height));return L(E("circle"),{stroke:e.color,"stroke-width":a[t?0:1],fill:"none",opacity:Bo(e,t),cx:i[0],cy:i[1],r:c-a[1]/2})}function kt(e,o,t,r,n,i,a){let c=Nt(n&&!r),s=ae(o,i,a),l=ae(t,i,a),p=l[0]-s[0],d=l[1]-s[1],m=Math.atan2(d,p),h=Math.cos(m)*c,z=Math.sin(m)*c;return L(E("line"),{stroke:e.color,"stroke-width":At(e,r),"stroke-linecap":"round","marker-end":"url(#arrowhead-"+e.key+")",opacity:Bo(e,r),x1:s[0],y1:s[1],x2:l[0]-h,y2:l[1]-z})}function Ct(e,o,t,r,n,i){let a=ae(o,r,n),c=t.color[0]+t.role[0].toUpperCase(),s=e.endsWith("/")?e+c+".svg":e;return L(E("image"),{className:k(t,i),x:a[0]-.5,y:a[1]-.5,width:1,height:1,href:s,transform:`scale(${t.scale||1})`,"transform-origin":`${a[0]} ${a[1]}`})}function Kt(e){let o=L(E("marker"),{id:"arrowhead-"+e.key,orient:"auto",markerWidth:4,markerHeight:8,refX:2.05,refY:2.01});return o.appendChild(L(E("path"),{d:"M0,0 V4 L3,2 Z",fill:e.color})),o.setAttribute("cgKey",e.key),o}function L(e,o){for(let t in o)e.setAttribute(t,o[t]);return e}function ye(e,o,t){return o==="white"?e:[t.width-1-e[0],t.height-1-e[1]]}function To(e,o){return{color:e.color,opacity:Math.round(e.opacity*10)/10,lineWidth:Math.round(o.lineWidth||e.lineWidth),key:[e.key,o.lineWidth].filter(t=>t).join("")}}function Et(){return[3/64,4/64]}function At(e,o){return(e.lineWidth||10)*(o?.85:1)/64}function Bo(e,o){return(e.opacity||1)*(o?.9:1)}function Nt(e){return(e?20:10)/64}function ae(e,o,t){let r=Math.min(1,o.width/o.height)*Math.max(1,t.height/t.width),n=Math.min(1,o.height/o.width)*Math.max(1,t.width/t.height);return[(e[0]-(t.width-1)/2)*r,((t.height-1)/2-e[1])*n]}var Ht=Ie,Rt=["1","2","3","4","5","6","7","8","9","10","11","12"],Lt={0:[{coords:Ht,position:"bottom",direction:"forward"},{coords:Rt,position:"side",direction:"forward"}]};function Oo(e,o){e.innerHTML="",e.classList.add("cg-wrap");for(let p of Fe)e.classList.toggle("orientation-"+p,o.orientation===p);e.classList.toggle("manipulable",!o.viewOnly);let t=D("cg-container");e.appendChild(t);let r=D("extension");t.appendChild(r);let n=D("cg-board");t.appendChild(n);let i,a;Qe(e)&&o.pockets&&(i=D("pocketBottom"),a=D("pocketTop"),t.insertBefore(o.orientation==="white"?a:i,n),t.insertBefore(o.orientation==="white"?i:a,n.nextSibling));let c,s;if(o.drawable.visible){let p=o.dimensions.width,d=o.dimensions.height;c=L(E("svg"),{class:"cg-shapes",viewBox:`${-p/2} ${-d/2} ${p} ${d}`,preserveAspectRatio:"xMidYMid slice"}),c.appendChild(E("defs")),c.appendChild(E("g")),s=L(E("svg"),{class:"cg-custom-svgs",viewBox:`${-(p-1)/2} ${-(d-1)/2} ${p} ${d}`,preserveAspectRatio:"xMidYMid slice"}),s.appendChild(E("g")),t.appendChild(c),t.appendChild(s)}o.coordinates&&Lt[o.notation].forEach(p=>{let d=p.position==="side"?o.dimensions.height:o.dimensions.width,m=p.coords.slice(0,d);t.appendChild(Tt(m,`${p.position} ${p.direction}${p.noBlackReverse?"":" "+o.orientation}`))});let l;return o.draggable.showGhost&&(l=D("piece","ghost"),te(l,!1),t.appendChild(l)),{pocketTop:a,pocketBottom:i,board:n,container:t,wrap:e,ghost:l,svg:c,customSvg:s}}function Tt(e,o){let t=D("coords",o),r;for(let n of e)r=D("coord"),r.textContent=n,t.appendChild(r);return t}function qo(e){let o=y(e),t=oe(e.dom.bounds(),e.dimensions),r=e.dom.elements.board,n=e.pieces,i=e.plinths,a=e.animation.current,c=a?a.plan.anims:new Map,s=a?a.plan.fadings:new Map,l=e.draggable.current,p=Bt(e),d=new Set,m=new Set,h=new Map,z=new Map,v,u,$,Z,P,J,Q,b,Pe,de;for(u=r.firstChild;u;){if(v=u.cgKey,$o(u))if($=n.get(v),P=c.get(v),J=s.get(v),Z=u.cgPiece,u.cgDragging&&(!l||l.orig!==v)&&(u.classList.remove("dragging"),C(u,t(g(v),o)),u.cgDragging=!1),!J&&u.cgFading&&(u.cgFading=!1,u.classList.remove("fading")),$){if(P&&u.cgAnimating&&Z===k($,e.orientation)){let f=g(v);f[0]+=P[2],f[1]+=P[3],u.classList.add("anim"),C(u,t(f,o))}else u.cgAnimating&&(u.cgAnimating=!1,u.classList.remove("anim"),C(u,t(g(v),o)),e.addPieceZIndex&&(u.style.zIndex=se(g(v),o)));Z===k($,e.orientation)&&(!J||!u.cgFading)?d.add(v):J&&Z===k(J,e.orientation)?(u.classList.add("fading"),u.cgFading=!0):Ge(h,Z,u)}else Ge(h,Z,u);else if(Fo(u)){let f=u.className;p.get(v)===f?m.add(v):Ge(z,f,u)}u=u.nextSibling}for(let[f,F]of p)if(!m.has(f)){Pe=z.get(F),de=Pe&&Pe.pop();let w=t(g(f),o);if(de)de.cgKey=f,C(de,w);else{let S=D("square",F);S.cgKey=f,C(S,w),r.insertBefore(S,r.firstChild)}}for(let[f,F]of n)if(P=c.get(f),!d.has(f))if(Q=h.get(k(F,e.orientation)),b=Q&&Q.pop(),b){b.cgKey=f,b.cgFading&&(b.classList.remove("fading"),b.cgFading=!1);let w=g(f);e.addPieceZIndex&&(b.style.zIndex=se(w,o)),P&&(b.cgAnimating=!0,b.classList.add("anim"),w[0]+=P[2],w[1]+=P[3]),C(b,t(w,o))}else{let w=k(F,e.orientation),S=D("piece",w),B=g(f);S.cgPiece=w,S.cgKey=f,P&&(S.cgAnimating=!0,B[0]+=P[2],B[1]+=P[3]),C(S,t(B,o)),e.addPieceZIndex&&(S.style.zIndex=se(B,o)),r.appendChild(S)}let we=0;for(let[f,F]of i)if(we+=1,P=c.get(f),!d.has(f)){if(Q=h.get(k(F,e.orientation)),b=Q&&Q.pop(),b){b.cgKey=f,b.cgFading&&(b.classList.remove("fading"),b.cgFading=!1);let w=g(f);e.addPieceZIndex&&(b.style.zIndex=se(w,o)),P&&(b.cgAnimating=!0,b.classList.add("anim"),w[0]+=P[2],w[1]+=P[3]),C(b,t(w,o))}else if(!e.plinthsPlaced){let w=k(F,e.orientation),S=D("square",w),B=g(f);S.cgPiece=w,S.cgKey=f,P&&(S.cgAnimating=!0,B[0]+=P[2],B[1]+=P[3]),C(S,t(B,o)),e.addPieceZIndex&&(S.style.zIndex=se(B,o)),r.appendChild(S),(we==8||we==4&&e.variant=="standard")&&(e.plinthsPlaced=!0)}}for(let f of h.values())Vo(e,f);for(let f of z.values())Vo(e,f)}function Go(e){let o=y(e),t=oe(e.dom.bounds(),e.dimensions),r=e.dom.elements.board.firstChild;for(;r;)($o(r)&&!r.cgAnimating||Fo(r))&&C(r,t(g(r.cgKey),o)),r=r.nextSibling}function $e(e){let o=e.dom.elements.wrap.getBoundingClientRect(),t=e.dom.elements.container,r=Math.floor(o.width*window.devicePixelRatio/e.dimensions.width)*e.dimensions.width/window.devicePixelRatio;t.style.width=r+"px",t.style.height=r+"px",e.dom.bounds.clear(),e.addDimensionsCssVars&&(document.documentElement.style.setProperty("--cg-width",r+"px"),document.documentElement.style.setProperty("--cg-height",r+"px"),e.dom.elements.pocketTop&&(e.dom.elements.pocketTop.style.setProperty("--cg-width",r+"px"),e.dom.elements.pocketTop.style.setProperty("--cg-height",r+"px")),e.dom.elements.pocketBottom&&(e.dom.elements.pocketBottom.style.setProperty("--cg-width",r+"px"),e.dom.elements.pocketBottom.style.setProperty("--cg-height",r+"px")))}function $o(e){return e.tagName==="PIECE"}function Fo(e){return e.tagName==="SQUARE"}function Vo(e,o){for(let t of o)t.classList.contains("l-piece")||e.dom.elements.board.removeChild(t)}function se(e,o){let t=3+e[1]*8+(7-e[0]);return o&&(t=69-t),t+""}function Bt(e){let o=new Map;if(e.lastMove&&e.highlight.lastMove)for(let n of e.lastMove)n!=="a0"&&T(o,n,"last-move");if(e.check&&e.highlight.check&&T(o,e.check,"check"),e.selected){if(T(o,e.selected,"selected"),e.movable.showDests){let n=e.movable.dests?.get(e.selected);if(n)for(let a of n)T(o,a,"move-dest"+(e.pieces.has(a)?" oc":""));let i=e.premovable.dests;if(i)for(let a of i)T(o,a,"premove-dest"+(e.pieces.has(a)?" oc":""))}}else if(e.dropmode.active||e.draggable.current?.orig==="a0"){let n=e.dropmode.active?e.dropmode.piece:e.draggable.current?.piece;if(n){if(e.dropmode.showDropDests&&n.color===e.turnColor){let i=e.movable.dests?.get(Y(n.role));if(i)for(let a of i)T(o,a,"move-dest")}else if(e.predroppable.showDropDests){let i=e.predroppable.dropDests;if(i)for(let a of i)T(o,a,"premove-dest"+(e.pieces.get(a)?" oc":""))}}}let t=e.premovable.current;if(t)for(let n of t)T(o,n,"current-premove");else e.predroppable.current&&T(o,e.predroppable.current.key,"current-premove");let r=e.exploding;if(r)for(let n of r.keys)T(o,n,"exploding"+r.stage);return o}function T(e,o,t){let r=e.get(o);r?e.set(o,`${r} ${t}`):e.set(o,t)}function Ge(e,o,t){let r=e.get(o);r?r.push(t):e.set(o,[t])}function Uo(e,o){let t=e.dom.elements.board;if("ResizeObserver"in window&&new ResizeObserver(o).observe(e.dom.elements.wrap),e.viewOnly)return;let r=Ot(e);t.addEventListener("touchstart",r,{passive:!1}),t.addEventListener("mousedown",r,{passive:!1}),(e.disableContextMenu||e.drawable.enabled)&&t.addEventListener("contextmenu",n=>n.preventDefault())}function zo(e,o){let t=[];if("ResizeObserver"in window||t.push(ce(document.body,"chessground.resize",o)),!e.viewOnly){let r=Io(e,io,_e),n=Io(e,ao,Je);for(let a of["touchmove","mousemove"])t.push(ce(document,a,r));for(let a of["touchend","mouseup"])t.push(ce(document,a,n));let i=()=>e.dom.bounds.clear();t.push(ce(document,"scroll",i,{capture:!0,passive:!0})),t.push(ce(window,"resize",i,{passive:!0}))}return()=>t.forEach(r=>r())}function ce(e,o,t,r){return e.addEventListener(o,t,r),()=>e.removeEventListener(o,t,r)}function Ot(e){return o=>{e.draggable.current?U(e):e.drawable.current?Ae(e):o.shiftKey||pe(o)?e.drawable.enabled&&Xe(e,o):e.viewOnly||(e.dropmode.active&&(Wo(e,o)===void 0||e.movable.color!==e.turnColor&&Wo(e,o)?.color===e.turnColor)?uo(e,o):(j(e),no(e,o)))}}function Io(e,o,t){return r=>{e.drawable.current?e.drawable.enabled&&t(e,r):e.viewOnly||o(e,r)}}function Wo(e,o){let t=A(o),r=t&&K(t,y(e),e.dom.bounds(),e.geometry);if(r&&e.pieces.get(r))return e.pieces.get(r)}function Mi(e,o,t,r,n){let i=No();ve(i,o||{});function a(){let c="dom"in i?i.dom.unbind:void 0,s=Oo(e,i),l=je(()=>s.board.getBoundingClientRect()),p=h=>{qo(m),Co(m),!h&&s.svg&&Lo(m,s.svg,s.customSvg)},d=()=>{$e(m),Go(m)};s.pocketTop&&(r=s.pocketTop),s.pocketBottom&&(n=s.pocketBottom),ko(i,s,r,n);let m=i;return m.dom={elements:s,bounds:l,redraw:Vt(p),redrawNow:p,unbind:c},m.drawable.prevSvgHash="",$e(m),p(!1),Uo(m,d),c||(m.dom.unbind=zo(m,d)),m.events.insert&&m.events.insert(s),m}return Ao(a(),a)}function Vt(e){let o=!1;return()=>{o||(o=!0,requestAnimationFrame(()=>{e(),o=!1}))}}export{Mi as Chessground};