UNPKG

@lichess-org/chessground

Version:
2 lines (1 loc) 32.7 kB
var Fe=["white","black"];var I=["a","b","c","d","e","f","g","h"],Z=["1","2","3","4","5","6","7","8"];var $e=[...Z].reverse(),Q=I.flatMap(e=>Z.map(o=>e+o)),L=e=>e.every(o=>o>=0&&o<=7)?Q[8*e[0]+e[1]]:void 0,G=e=>L(e),m=e=>[e.charCodeAt(0)-97,e.charCodeAt(1)-49];var be=Q.map(m),ze=Q.map((e,o)=>({key:e,pos:be[o]}));function he(e){let o,t=()=>(o===void 0&&(o=e()),o);return t.clear=()=>{o=void 0},t}var Ie=()=>{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}}},F=e=>e==="white"?"black":"white",W=(e,o)=>(e[0]-o[0])**2+(e[1]-o[1])**2,Y=(e,o)=>e.role===o.role&&e.color===o.color,je=(e,o)=>e[0]===o[0]&&e[1]===o[1],$=e=>(o,t)=>[(t?o[0]:7-o[0])*e.width/8,(t?7-o[1]:o[1])*e.height/8],M=(e,o)=>{e.style.transform=`translate(${o[0]}px,${o[1]}px)`},ve=(e,o,t=1)=>{e.style.transform=`translate(${o[0]}px,${o[1]}px) scale(${t})`},R=(e,o)=>{e.style.display=o?"":"none"},T=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]},ot=he(()=>!("ontouchstart"in window)&&["macintosh","firefox"].every(e=>navigator.userAgent.toLowerCase().includes(e))),re=e=>e.button===2&&!(e.ctrlKey&&ot()),x=(e,o)=>{let t=document.createElement(e);return o&&(t.className=o),t};function ie(e,o,t){let n=m(e);return o||(n[0]=7-n[0],n[1]=7-n[1]),[t.left+t.width*n[0]/8+t.width/16,t.top+t.height*(7-n[1])/8+t.height/16]}var q=(e,o)=>Math.abs(e-o),ae=(e,o,t,n)=>q(e,t)*q(o,n)===2,ye=(e,o,t,n)=>e===t!=(o===n),Se=(e,o,t,n)=>q(e,t)===q(o,n)&&e!==t,Ue=(e,o,t,n)=>ye(e,o,t,n)||Se(e,o,t,n),Ze=(e,o,t,n)=>Math.max(q(e,t),q(o,n))===1;var _e=(e,o,t,n,r)=>{let i=r?1:-1;return e===t&&(n===o+i||n===o+2*i&&(r?o<=1:o>=6))},Xe=(e,o,t,n)=>{let r=t-e,i=n-o;if(r&&i&&Math.abs(r)!==Math.abs(i))return[];let s=Math.sign(r),p=Math.sign(i),d=[],a=e+s,c=o+p;for(;a!==t||c!==n;)d.push([a,c]),a+=s,c+=p;return d.map(L).filter(l=>l!==void 0)};var tt=e=>q(e.orig.pos[0],e.dest.pos[0])<=1&&(q(e.orig.pos[0],e.dest.pos[0])===1?e.dest.pos[1]===e.orig.pos[1]+(e.color==="white"?1:-1):_e(...e.orig.pos,...e.dest.pos,e.color==="white")),nt=e=>ae(...e.orig.pos,...e.dest.pos),Qe=e=>Se(...e.orig.pos,...e.dest.pos),Ye=e=>ye(...e.orig.pos,...e.dest.pos),rt=e=>Qe(e)||Ye(e),it=e=>Ze(...e.orig.pos,...e.dest.pos)||e.orig.pos[1]===e.dest.pos[1]&&e.orig.pos[1]===(e.color==="white"?0:7)&&(e.orig.pos[0]===4&&(e.dest.pos[0]===2&&e.rookFilesFriendlies.includes(0)||e.dest.pos[0]===6&&e.rookFilesFriendlies.includes(7))||e.rookFilesFriendlies.includes(e.dest.pos[0])),at={pawn:tt,knight:nt,bishop:Qe,rook:Ye,queen:rt,king:it};function we(e,o){let t=e.pieces,n=t.get(o);if(!n||n.color===e.turnColor)return[];let r=n.color,i=new Map([...t].filter(([c,l])=>l.color===r)),s=new Map([...t].filter(([c,l])=>l.color===F(r))),p={key:o,pos:m(o)},d=c=>at[n.role](c)&&e.premovable.additionalPremoveRequirements(c),a={orig:p,role:n.role,allPieces:t,friendlies:i,enemies:s,color:r,rookFilesFriendlies:Array.from(t).filter(([c,l])=>c[1]===(r==="white"?"1":"8")&&l.color===r&&l.role==="rook").map(([c])=>m(c)[0]),lastMove:e.lastMove};return ze.filter(c=>d({...a,dest:c})).map(c=>c.key)}function P(e,...o){e&&setTimeout(()=>e(...o),1)}function Je(e){e.orientation=F(e.orientation),e.animation.current=e.draggable.current=e.selected=void 0}function eo(e,o){for(let[t,n]of o)n?e.pieces.set(t,n):e.pieces.delete(t)}function oo(e,o){if(e.check=void 0,o===!0&&(o=e.turnColor),o)for(let[t,n]of e.pieces)n.role==="king"&&n.color===o&&(e.check=t)}function st(e,o,t,n){E(e),e.premovable.current=[o,t],P(e.premovable.events.set,o,t,n)}function D(e){e.premovable.current&&(e.premovable.current=void 0,P(e.premovable.events.unset))}function ct(e,o,t){D(e),e.predroppable.current={role:o,key:t},P(e.predroppable.events.set,o,t)}function E(e){let o=e.predroppable;o.current&&(o.current=void 0,P(o.events.unset))}function lt(e,o,t){if(!e.autoCastle)return!1;let n=e.pieces.get(o);if(!n||n.role!=="king")return!1;let r=m(o),i=m(t);if(r[1]!==0&&r[1]!==7||r[1]!==i[1])return!1;r[0]===4&&!e.pieces.has(t)&&(i[0]===6?t=G([7,i[1]]):i[0]===2&&(t=G([0,i[1]])));let s=e.pieces.get(t);return!s||s.color!==n.color||s.role!=="rook"?!1:(e.pieces.delete(o),e.pieces.delete(t),r[0]<i[0]?(e.pieces.set(G([6,i[1]]),n),e.pieces.set(G([5,i[1]]),s)):(e.pieces.set(G([2,i[1]]),n),e.pieces.set(G([3,i[1]]),s)),!0)}function Pe(e,o,t){let n=e.pieces.get(o),r=e.pieces.get(t);if(o===t||!n)return!1;let i=r&&r.color!==n.color?r:void 0;return t===e.selected&&w(e),P(e.events.move,o,t,i),lt(e,o,t)||(e.pieces.set(t,n),e.pieces.delete(o)),e.lastMove=[o,t],e.check=void 0,P(e.events.change),i||!0}function se(e,o,t,n){if(e.pieces.has(t))if(n)e.pieces.delete(t);else return!1;return P(e.events.dropNewPiece,o,t),e.pieces.set(t,o),e.lastMove=[t],e.check=void 0,P(e.events.change),e.movable.dests=void 0,e.turnColor=F(e.turnColor),!0}function to(e,o,t){let n=Pe(e,o,t);return n&&(e.movable.dests=void 0,e.turnColor=F(e.turnColor),e.animation.current=void 0),n}function Me(e,o,t){if(le(e,o,t)){let n=to(e,o,t);if(n){let r=e.hold.stop();w(e);let i={premove:!1,ctrlKey:e.stats.ctrlKey,holdTime:r};return n!==!0&&(i.captured=n),P(e.movable.events.after,o,t,i),!0}}else if(pt(e,o,t))return st(e,o,t,{ctrlKey:e.stats.ctrlKey}),w(e),!0;return w(e),!1}function ce(e,o,t,n){let r=e.pieces.get(o);r&&(dt(e,o,t)||n)?(e.pieces.delete(o),se(e,r,t,n),P(e.movable.events.afterNewPiece,r.role,t,{premove:!1,predrop:!1})):r&&ut(e,o,t)?ct(e,r.role,t):(D(e),E(e)),e.pieces.delete(o),w(e)}function J(e,o,t){if(P(e.events.select,o),e.selected){if(e.selected===o&&!e.draggable.enabled){w(e),e.hold.cancel();return}else if((e.selectable.enabled||t)&&e.selected!==o&&Me(e,e.selected,o)){e.stats.dragged=!1;return}}(e.selectable.enabled||e.draggable.enabled)&&(no(e,o)||ke(e,o))&&(xe(e,o),e.hold.start())}function xe(e,o){e.selected=o,ke(e,o)?e.premovable.customDests||(e.premovable.dests=we(e,o)):e.premovable.dests=void 0}function w(e){e.selected=void 0,e.premovable.dests=void 0,e.hold.cancel()}function no(e,o){let t=e.pieces.get(o);return!!t&&(e.movable.color==="both"||e.movable.color===t.color&&e.turnColor===t.color)}var le=(e,o,t)=>o!==t&&no(e,o)&&(e.movable.free||!!e.movable.dests?.get(o)?.includes(t));function dt(e,o,t){let n=e.pieces.get(o);return!!n&&(o===t||!e.pieces.has(t))&&(e.movable.color==="both"||e.movable.color===n.color&&e.turnColor===n.color)}function ke(e,o){let t=e.pieces.get(o);return!!t&&e.premovable.enabled&&e.movable.color===t.color&&e.turnColor!==t.color}var pt=(e,o,t)=>o!==t&&ke(e,o)&&(e.premovable.customDests?.get(o)??we(e,o)).includes(t);function ut(e,o,t){let n=e.pieces.get(o),r=e.pieces.get(t);return!!n&&(!r||r.color!==e.movable.color)&&e.predroppable.enabled&&(n.role!=="pawn"||t[1]!=="1"&&t[1]!=="8")&&e.movable.color===n.color&&e.turnColor!==n.color}function ro(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 io(e){let o=e.premovable.current;if(!o)return!1;let t=o[0],n=o[1],r=!1;if(le(e,t,n)){let i=to(e,t,n);if(i){let s={premove:!0};i!==!0&&(s.captured=i),P(e.movable.events.after,t,n,s),r=!0}}return D(e),r}function ao(e,o){let t=e.predroppable.current,n=!1;if(!t)return!1;if(o(t)){let r={role:t.role,color:e.movable.color};se(e,r,t.key)&&(P(e.movable.events.afterNewPiece,t.role,t.key,{premove:!1,predrop:!0}),n=!0)}return E(e),n}function ee(e){D(e),E(e),w(e)}function Ce(e){e.movable.color=e.movable.dests=e.animation.current=void 0,ee(e)}function k(e,o,t){let n=Math.floor(8*(e[0]-t.left)/t.width);o||(n=7-n);let r=7-Math.floor(8*(e[1]-t.top)/t.height);return o||(r=7-r),n>=0&&n<8&&r>=0&&r<8?L([n,r]):void 0}function so(e,o,t,n){let r=m(e),i=be.filter(a=>je(r,a)||Ue(r[0],r[1],a[0],a[1])||ae(r[0],r[1],a[0],a[1])),p=i.map(a=>ie(G(a),t,n)).map(a=>W(o,a)),[,d]=p.reduce((a,c,l)=>a[0]<c?a:[c,l],[p[0],0]);return L(i[d])}var h=e=>e.orientation==="white";var De="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR",gt={p:"pawn",r:"rook",n:"knight",b:"bishop",q:"queen",k:"king"},ft={pawn:"p",rook:"r",knight:"n",bishop:"b",queen:"q",king:"k"};function de(e){e==="start"&&(e=De);let o=new Map,t=7,n=0;for(let r of e)switch(r){case" ":case"[":return o;case"/":if(--t,t<0)return o;n=0;break;case"~":{let i=L([n-1,t]),s=i&&o.get(i);s&&(s.promoted=!0);break}default:{let i=r.charCodeAt(0);if(i<57)n+=i-48;else{let s=r.toLowerCase(),p=L([n,t]);p&&o.set(p,{role:gt[s],color:r===s?"black":"white"}),++n}}}return o}function co(e){return $e.map(o=>I.map(t=>{let n=e.get(t+o);if(n){let r=ft[n.role];return n.color==="white"&&(r=r.toUpperCase()),n.promoted&&(r+="~"),r}else return"1"}).join("")).join("/").replace(/1{2,}/g,o=>o.length.toString())}function Ee(e,o){o.animation&&(Ae(e.animation,o.animation),(e.animation.duration||0)<70&&(e.animation.enabled=!1))}function pe(e,o){if(o.movable?.dests&&(e.movable.dests=void 0),o.drawable?.autoShapes&&(e.drawable.autoShapes=[]),Ae(e,o),o.fen&&(e.pieces=de(o.fen),e.drawable.shapes=o.drawable?.shapes||[]),"check"in o&&oo(e,o.check||!1),"lastMove"in o&&!o.lastMove?e.lastMove=void 0:o.lastMove&&(e.lastMove=o.lastMove),e.selected&&xe(e,e.selected),Ee(e,o),!e.movable.rookCastle&&e.movable.dests){let t=e.movable.color==="white"?"1":"8",n="e"+t,r=e.movable.dests.get(n),i=e.pieces.get(n);if(!r||!i||i.role!=="king")return;e.movable.dests.set(n,r.filter(s=>!(s==="a"+t&&r.includes("c"+t))&&!(s==="h"+t&&r.includes("g"+t))))}}function Ae(e,o){for(let t in o)t==="__proto__"||t==="constructor"||!Object.prototype.hasOwnProperty.call(o,t)||(Object.prototype.hasOwnProperty.call(e,t)&&lo(e[t])&&lo(o[t])?Ae(e[t],o[t]):e[t]=o[t])}function lo(e){if(typeof e!="object"||e===null)return!1;let o=Object.getPrototypeOf(e);return o===Object.prototype||o===null}var O=(e,o)=>o.animation.enabled?vt(e,o):V(e,o);function V(e,o){let t=e(o);return o.dom.redraw(),t}var Ne=(e,o)=>({key:e,pos:m(e),piece:o}),bt=(e,o)=>o.sort((t,n)=>W(e.pos,t.pos)-W(e.pos,n.pos))[0];function ht(e,o){let t=new Map,n=[],r=new Map,i=[],s=[],p=new Map,d,a,c;for(let[l,f]of e)p.set(l,Ne(l,f));for(let l of Q)d=o.pieces.get(l),a=p.get(l),d?a?Y(d,a.piece)||(i.push(a),s.push(Ne(l,d))):s.push(Ne(l,d)):a&&i.push(a);for(let l of s)a=bt(l,i.filter(f=>Y(l.piece,f.piece))),a&&(c=[a.pos[0]-l.pos[0],a.pos[1]-l.pos[1]],t.set(l.key,c.concat(c)),n.push(a.key));for(let l of i)n.includes(l.key)||r.set(l.key,l.piece);return{anims:t,fadings:r}}function po(e,o){let t=e.animation.current;if(t===void 0){e.dom.destroyed||e.dom.redrawNow();return}let n=1-(o-t.start)*t.frequency;if(n<=0)e.animation.current=void 0,e.dom.redrawNow();else{let r=yt(n);for(let i of t.plan.anims.values())i[2]=i[0]*r,i[3]=i[1]*r;e.dom.redrawNow(!0),requestAnimationFrame((i=performance.now())=>po(e,i))}}function vt(e,o){let t=new Map(o.pieces),n=e(o),r=ht(t,o);if(r.anims.size||r.fadings.size){let i=o.animation.current&&o.animation.current.start;o.animation.current={start:performance.now(),frequency:1/o.animation.duration,plan:r},i||po(o,performance.now())}else o.dom.redraw();return n}var yt=e=>e<.5?4*e*e*e:(e-1)*(2*e-2)*(2*e-2)+1;var St=["green","red","blue","yellow"];function uo(e,o){if(o.touches&&o.touches.length>1)return;o.stopPropagation(),o.preventDefault(),o.ctrlKey?w(e):ee(e);let t=T(o),n=k(t,h(e),e.dom.bounds());n&&(e.drawable.current={orig:n,pos:t,brush:wt(o),snapToValidMove:e.drawable.defaultSnapToValidMove},go(e))}function go(e){requestAnimationFrame(()=>{let o=e.drawable.current;if(o){let t=k(o.pos,h(e),e.dom.bounds());t||(o.snapToValidMove=!1);let n=o.snapToValidMove?so(o.orig,o.pos,h(e),e.dom.bounds()):t;n!==o.mouseSq&&(o.mouseSq=n,o.dest=n!==o.orig?n:void 0,e.dom.redrawNow()),go(e)}})}function fo(e,o){e.drawable.current&&(e.drawable.current.pos=T(o))}function mo(e){let o=e.drawable.current;o&&(o.mouseSq&&Pt(e.drawable,o),He(e))}function He(e){e.drawable.current&&(e.drawable.current=void 0,e.dom.redraw())}function bo(e){e.drawable.shapes.length&&(e.drawable.shapes=[],e.dom.redraw(),ho(e.drawable))}var ue=(e,o)=>e.orig===o.orig&&e.dest===o.dest,Te=(e,o)=>e.brush===o.brush;function wt(e){let o=(e.shiftKey||e.ctrlKey)&&re(e),t=e.altKey||e.metaKey||e.getModifierState?.("AltGraph");return St[(o?1:0)+(t?2:0)]}function Pt(e,o){let t=e.shapes.find(n=>ue(n,o));t&&(e.shapes=e.shapes.filter(n=>!ue(n,o))),(!t||!Te(t,o))&&e.shapes.push({orig:o.orig,dest:o.dest,brush:o.brush}),ho(e)}function ho(e){e.onChange&&e.onChange(e.shapes)}function vo(e,o){if(!(e.trustAllEvents||o.isTrusted)||o.buttons!==void 0&&o.buttons>1||o.touches&&o.touches.length>1)return;let t=e.dom.bounds(),n=T(o),r=k(n,h(e),t);if(!r)return;let i=e.pieces.get(r),s=e.selected;if(!s&&e.drawable.enabled&&(e.drawable.eraseOnMovablePieceClick||!i||i.color!==e.turnColor)&&bo(e),o.cancelable!==!1&&(!o.touches||e.blockTouchScroll||i||s||xt(e,n)))o.preventDefault();else if(o.touches)return;let p=!!e.premovable.current,d=!!e.predroppable.current;e.stats.ctrlKey=o.ctrlKey,e.selected&&le(e,e.selected,r)?O(l=>J(l,r),e):J(e,r);let a=e.selected===r,c=Mo(e,r);if(i&&c&&a&&ro(e,r)){e.draggable.current={orig:r,piece:i,origPos:n,pos:n,started:e.draggable.autoDistance&&e.stats.dragged,element:c,previouslySelected:s,originTarget:o.target,keyHasChanged:!1},c.cgDragging=!0,c.classList.add("dragging");let l=e.dom.elements.ghost;l&&(l.className=`ghost ${i.color} ${i.role}`,M(l,$(t)(m(r),h(e))),R(l,!0)),Re(e)}else p&&D(e),d&&E(e);e.dom.redraw()}function xt(e,o){let t=h(e),n=e.dom.bounds(),r=Math.pow(e.touchIgnoreRadius*n.width/16,2)*2;for(let i of e.pieces.keys()){let s=ie(i,t,n);if(W(s,o)<=r)return!0}return!1}function yo(e,o,t,n){e.pieces.set("a0",o),e.dom.redraw();let i=T(t);e.draggable.current={orig:"a0",piece:o,origPos:i,pos:i,started:!0,element:()=>Mo(e,"a0"),originTarget:t.target,newPiece:!0,force:!!n,keyHasChanged:!1},Re(e)}function Re(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||!Y(t,o.piece))j(e);else if(!o.started&&W(o.pos,o.origPos)>=Math.pow(e.draggable.distance,2)&&(o.started=!0),o.started){if(typeof o.element=="function"){let r=o.element();if(!r)return;r.cgDragging=!0,r.classList.add("dragging"),o.element=r}let n=e.dom.bounds();M(o.element,[o.pos[0]-n.left-n.width/16,o.pos[1]-n.top-n.height/16]),e.jsHover?kt(e,o):o.keyHasChanged||=o.orig!==k(o.pos,h(e),n)}Re(e)})}function kt(e,o){let t=k(o.pos,h(e),e.dom.bounds());if(o.orig!==t){if(o.keyHasChanged=!0,t)if(e.movable.dests?.get(o.orig)?.includes(t)??e.premovable.dests?.includes(t)){let r=e.dom.elements.board.querySelector(`.move-dest[data-key="${t}"], .premove-dest[data-key="${t}"]`);r&&!r.classList.contains("hover")&&(qe(e),r.classList.add("hover"))}else qe(e)}else qe(e)}function qe(e){let o=e.dom.elements.board.querySelectorAll(".move-dest, .premove-dest");o.length>0&&o.forEach(t=>t.classList.remove("hover"))}function So(e,o){e.draggable.current&&(!o.touches||o.touches.length<2)&&(e.draggable.current.pos=T(o))}function wo(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}D(e),E(e);let n=T(o)||t.pos,r=k(n,h(e),e.dom.bounds());r&&t.started&&t.orig!==r?t.newPiece?ce(e,t.orig,r,t.force):(e.stats.ctrlKey=o.ctrlKey,Me(e,t.orig,r)&&(e.stats.dragged=!0)):t.newPiece?e.pieces.delete(t.orig):e.draggable.deleteOnDropOff&&!r&&(e.pieces.delete(t.orig),P(e.events.change)),(t.orig===t.previouslySelected||t.keyHasChanged)&&(t.orig===r||!r)?w(e):e.selectable.enabled||w(e),Po(e),e.draggable.current=void 0,e.dom.redraw()}function j(e){let o=e.draggable.current;o&&(o.newPiece&&e.pieces.delete(o.orig),e.draggable.current=void 0,w(e),Po(e),e.dom.redraw())}function Po(e){let o=e.dom.elements;o.ghost&&R(o.ghost,!1)}function Mo(e,o){let t=e.dom.elements.board.firstChild;for(;t;){if(t.cgKey===o&&t.tagName==="PIECE")return t;t=t.nextSibling}}function ko(e,o){e.exploding={stage:1,keys:o},e.dom.redraw(),setTimeout(()=>{xo(e,2),setTimeout(()=>xo(e,void 0),120)},120)}function xo(e,o){e.exploding&&(o?e.exploding.stage=o:e.exploding=void 0,e.dom.redraw())}function Co(e,o){function t(){Je(e),o()}return{set(n){n.orientation&&n.orientation!==e.orientation&&t(),Ee(e,n),(n.fen?O:V)(r=>pe(r,n),e)},state:e,getFen:()=>co(e.pieces),toggleOrientation:t,setPieces(n){O(r=>eo(r,n),e)},selectSquare(n,r){n?O(i=>J(i,n,r),e):e.selected&&(w(e),e.dom.redraw())},move(n,r){O(i=>Pe(i,n,r),e)},newPiece(n,r){O(i=>se(i,n,r),e)},playPremove(){if(e.premovable.current){if(O(io,e))return!0;e.dom.redraw()}return!1},playPredrop(n){if(e.predroppable.current){let r=ao(e,n);return e.dom.redraw(),r}return!1},cancelPremove(){V(D,e)},cancelPredrop(){V(E,e)},cancelMove(){V(n=>{ee(n),j(n)},e)},stop(){V(n=>{Ce(n),j(n)},e)},explode(n){ko(e,n)},setAutoShapes(n){V(r=>r.drawable.autoShapes=n,e)},setShapes(n){V(r=>r.drawable.shapes=n.slice(),e)},getKeyAtDomPos(n){return k(n,h(e),e.dom.bounds())},redrawAll:o,dragNewPiece(n,r,i){yo(e,n,r,i)},destroy(){Ce(e),e.dom.unbind?.(),e.dom.destroyed=!0}}}function Ko(){return{pieces:de(De),orientation:"white",turnColor:"white",coordinates:!0,coordinatesOnSquares:!1,ranksPosition:"right",autoCastle:!0,viewOnly:!1,disableContextMenu:!1,addPieceZIndex:!1,blockTouchScroll:!1,touchIgnoreRadius:1,pieceKey:!1,trustAllEvents:!1,jsHover:!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,additionalPremoveRequirements:e=>!0,events:{}},predroppable:{enabled:!1,events:{}},draggable:{enabled:!0,distance:3,autoDistance:!0,showGhost:!0,deleteOnDropOff:!1},dropmode:{active:!1},selectable:{enabled:!0},stats:{dragged:!("ontouchstart"in window)},events:{},drawable:{enabled:!0,visible:!0,defaultSnapToValidMove:!0,eraseOnMovablePieceClick:!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},purple:{key:"purple",color:"#68217a",opacity:.65,lineWidth:10},pink:{key:"pink",color:"#ee2080",opacity:.5,lineWidth:10},white:{key:"white",color:"white",opacity:1,lineWidth:10},paleWhite:{key:"pwhite",color:"white",opacity:.6,lineWidth:10}},prevSvgHash:""},hold:Ie()}}function No(){let e=v("defs"),o=S(v("filter"),{id:"cg-filter-blur"});return o.appendChild(S(v("feGaussianBlur"),{stdDeviation:"0.013"})),e.appendChild(o),e}function Ho(e,o){let t=e.drawable,n=t.current,r=n&&n.mouseSq?n:void 0,i=new Map,s=e.dom.bounds(),p=t.autoShapes.filter(l=>!l.piece);for(let l of t.shapes.concat(p).concat(r?[r]:[])){if(!l.dest)continue;let f=i.get(l.dest)??new Set,g=fe(ge(m(l.orig),e.orientation),s),u=fe(ge(m(l.dest),e.orientation),s);f.add(Lo(Go(g,u))),i.set(l.dest,f)}let d=[],a=r?t.shapes.findIndex(l=>ue(l,r)&&Te(l,r)):-1;for(let[l,f]of t.shapes.concat(p).entries()){let g=a!==-1&&a===l;d.push({shape:f,current:!1,pendingErase:g,hash:Do(f,Oe(f.dest,i),!1,s,g,Ao(f.dest,i))})}r&&a===-1&&d.push({shape:r,current:!0,hash:Do(r,Oe(r.dest,i),!0,s,!1,Ao(r.dest,i)),pendingErase:!1});let c=d.map(l=>l.hash).join(";");c!==e.drawable.prevSvgHash&&(e.drawable.prevSvgHash=c,Kt(t,d,o),Dt(d,o,l=>Nt(e,l,t.brushes,i,s)))}function Kt(e,o,t){for(let n of[t.shapes,t.shapesBelow]){let r=n.querySelector("defs"),i=o.filter(a=>n===t.shapesBelow==!!a.shape.below),s=new Map;for(let a of i.filter(c=>c.shape.dest&&c.shape.brush)){let c=Oo(e.brushes[a.shape.brush],a.shape.modifiers),{key:l,color:f}=Vo(a.shape);l&&f&&s.set(l,{key:l,color:f,opacity:1,lineWidth:1}),s.set(c.key,c)}let p=new Set,d=r.firstElementChild;for(;d;)p.add(d.getAttribute("cgKey")),d=d.nextElementSibling;for(let[a,c]of s.entries())p.has(a)||r.appendChild(qt(c))}}function Dt(e,o,t){for(let[n,r]of[[o.shapes,o.custom],[o.shapesBelow,o.customBelow]]){let[i,s]=[n,r].map(a=>a.querySelector("g")),p=e.filter(a=>n===o.shapesBelow==!!a.shape.below),d=new Map;for(let a of p)d.set(a.hash,!1);for(let a of[i,s]){let c=[],l=a.firstElementChild,f;for(;l;)f=l.getAttribute("cgHash"),d.has(f)?d.set(f,!0):c.push(l),l=l.nextElementSibling;for(let g of c)a.removeChild(g)}for(let a of p.filter(c=>!d.get(c.hash)))for(let c of t(a))c.isCustom?s.appendChild(c.el):i.appendChild(c.el)}}function Do({orig:e,dest:o,brush:t,piece:n,modifiers:r,customSvg:i,label:s,below:p},d,a,c,l,f){return[c.width,c.height,a,l&&"pendingErase",f,e,o,t,d&&"-",n&&Et(n),r&&At(r),i&&`custom-${Eo(i.html)},${i.center?.[0]??"o"}`,s&&`label-${Eo(s.text)}`,p&&"below"].filter(g=>g).join(",")}var Et=e=>[e.color,e.role,e.scale].filter(o=>o).join(","),At=e=>[e.lineWidth,e.hilite].filter(o=>o).join(",");function Eo(e){let o=0;for(let t=0;t<e.length;t++)o=(o<<5)-o+e.charCodeAt(t)>>>0;return o.toString()}function Nt(e,{shape:o,current:t,pendingErase:n,hash:r},i,s,p){let d=fe(ge(m(o.orig),e.orientation),p),a=o.dest?fe(ge(m(o.dest),e.orientation),p):d,c=o.brush&&Oo(i[o.brush],o.modifiers),l=s.get(o.dest),f=[];if(c){let g=S(v("g"),{cgHash:r});f.push({el:g}),d[0]!==a[0]||d[1]!==a[1]?g.appendChild(Tt(o,c,d,a,t,Oe(o.dest,s),n)):g.appendChild(Ht(i[o.brush],d,t,p,n))}if(o.label){let g=o.label;g.fill??=o.brush&&i[o.brush].color;let u=o.brush?void 0:"tr";f.push({el:Rt(g,r,d,a,l,u),isCustom:!0})}if(o.customSvg){let g=o.customSvg.center??"orig",[u,A]=g==="label"?Fo(d,a,l).map(y=>y-.5):g==="dest"?a:d,C=S(v("g"),{transform:`translate(${u},${A})`,cgHash:r});C.innerHTML=`<svg width="1" height="1" viewBox="0 0 100 100">${o.customSvg.html}</svg>`,f.push({el:C,isCustom:!0})}return f}function Ht(e,o,t,n,r){let i=Ot(),s=(n.width+n.height)/(4*Math.max(n.width,n.height));return S(v("circle"),{stroke:e.color,"stroke-width":i[t?0:1],fill:"none",opacity:Bo(e,t,r),cx:o[0],cy:o[1],r:s-i[1]/2})}function Tt(e,o,t,n,r,i,s){function p(c){let l=Bt(i&&!r),f=n[0]-t[0],g=n[1]-t[1],u=Math.atan2(g,f),A=Math.cos(u)*l,C=Math.sin(u)*l,y=Vo(e);return S(v("line"),{stroke:c?y.color:o.color,"stroke-width":Vt(o,r)*(c?1.14:1),"stroke-linecap":"round","marker-end":`url(#arrowhead-${c?y.key:o.key})`,opacity:e.modifiers?.hilite&&!s?1:Bo(o,r,s),x1:t[0],y1:t[1],x2:n[0]-A,y2:n[1]-C})}if(!e.modifiers?.hilite)return p(!1);let d=S(v("g"),{opacity:o.opacity}),a=S(v("g"),{filter:"url(#cg-filter-blur)"});return a.appendChild(Lt(t,n)),a.appendChild(p(!0)),d.appendChild(a),d.appendChild(p(!1)),d}function qt(e){let o=S(v("marker"),{id:"arrowhead-"+e.key,orient:"auto",overflow:"visible",markerWidth:4,markerHeight:4,refX:e.key.startsWith("hilite")?1.86:2.05,refY:2});return o.appendChild(S(v("path"),{d:"M0,0 V4 L3,2 Z",fill:e.color})),o.setAttribute("cgKey",e.key),o}function Rt(e,o,t,n,r,i){let p=.4*.75**e.text.length,d=Fo(t,n,r),a=i==="tr"?.4:0,c=S(v("g"),{transform:`translate(${d[0]+a},${d[1]-a})`,cgHash:o});c.appendChild(S(v("circle"),{r:.4/2,"fill-opacity":i?1:.8,"stroke-opacity":i?1:.7,"stroke-width":.03,fill:e.fill??"#666",stroke:"white"}));let l=S(v("text"),{"font-size":p,"font-family":"Noto Sans","text-anchor":"middle",fill:"white",y:.13*.75**e.text.length});return l.innerHTML=e.text,c.appendChild(l),c}var ge=(e,o)=>o==="white"?e:[7-e[0],7-e[1]],To=(e,o)=>(e%o+o)%o,qo=(e,o)=>To(e+o,16),Ro=e=>[...e].some(o=>[-3,-2,-1,1,2,3].some(t=>e.has(qo(o,t)))),Oe=(e,o)=>!!e&&o.has(e)&&Ro(o.get(e)),v=e=>document.createElementNS("http://www.w3.org/2000/svg",e),Ao=(e,o)=>e&&o.has(e)?o.get(e).size:0;function S(e,o){for(let t in o)Object.prototype.hasOwnProperty.call(o,t)&&e.setAttribute(t,o[t]);return e}var Oo=(e,o)=>o?{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("")}:e,Ot=()=>[3/64,4/64],Vt=(e,o)=>(e.lineWidth||10)*(o?.85:1)/64;function Vo(e){let o=e.modifiers?.hilite;return{key:o&&`hilite-${o.replace("#","")}`,color:o}}var Bo=(e,o,t)=>(e.opacity||1)*(t?.6:o?.9:1),Bt=e=>(e?20:10)/64;function fe(e,o){let t=Math.min(1,o.width/o.height),n=Math.min(1,o.height/o.width);return[(e[0]-3.5)*t,(3.5-e[1])*n]}function Lt(e,o){let t={from:[Math.floor(Math.min(e[0],o[0])),Math.floor(Math.min(e[1],o[1]))],to:[Math.ceil(Math.max(e[0],o[0])),Math.ceil(Math.max(e[1],o[1]))]};return S(v("rect"),{x:t.from[0],y:t.from[1],width:t.to[0]-t.from[0],height:t.to[1]-t.from[1],fill:"none",stroke:"none"})}var Lo=e=>To(Math.round(e*8/Math.PI),16),Go=(e,o)=>Math.atan2(o[1]-e[1],o[0]-e[0])+Math.PI,Gt=(e,o)=>Math.sqrt([e[0]-o[0],e[1]-o[1]].reduce((t,n)=>t+n*n,0));function Fo(e,o,t){let n=Gt(e,o),r=Go(e,o);if(t&&(n-=33/64,Ro(t))){n-=10/64;let i=Lo(r);i&1&&[-1,1].some(s=>t.has(qo(i,s)))&&(n-=.4)}return[e[0]-Math.cos(r)*n,e[1]-Math.sin(r)*n].map(i=>i+.5)}function $o(e,o){e.innerHTML="",e.classList.add("cg-wrap");for(let c of Fe)e.classList.toggle("orientation-"+c,o.orientation===c);e.classList.toggle("manipulable",!o.viewOnly);let t=x("cg-container");e.appendChild(t);let n=x("cg-board");t.appendChild(n);let r,i,s,p,d;if(o.drawable.visible&&([r,i]=["cg-shapes-below","cg-shapes"].map(c=>Wo(c,!0)),[s,p]=["cg-custom-below","cg-custom-svgs"].map(c=>Wo(c,!1)),d=x("cg-auto-pieces"),t.appendChild(r),t.appendChild(s),t.appendChild(i),t.appendChild(p),t.appendChild(d)),o.coordinates){let c=o.orientation==="black"?" black":"",l=o.ranksPosition==="left"?" left":"";if(o.coordinatesOnSquares){let f=o.orientation==="white"?g=>g+1:g=>8-g;I.forEach((g,u)=>t.appendChild(Ve(Z.map(A=>g+A),"squares rank"+f(u)+c+l,u%2===0?"black":"white")))}else t.appendChild(Ve(Z,"ranks"+c+l,o.ranksPosition==="right"==(o.orientation==="white")?"white":"black")),t.appendChild(Ve(I,"files"+c,F(o.orientation)))}let a;return!o.viewOnly&&o.draggable.enabled&&o.draggable.showGhost&&(a=x("piece","ghost"),R(a,!1),t.appendChild(a)),{board:n,container:t,wrap:e,ghost:a,shapes:i,shapesBelow:r,custom:p,customBelow:s,autoPieces:d}}function Wo(e,o){let t=S(v("svg"),{class:e,viewBox:o?"-4 -4 8 8":"-3.5 -3.5 8 8",preserveAspectRatio:"xMidYMid slice"});return o&&t.appendChild(No()),t.appendChild(v("g")),t}function Ve(e,o,t){let n=x("coords",o),r;return e.forEach((i,s)=>{let p=s%2===(t==="white"?0:1);r=x("coord",`coord-${p?"light":"dark"}`),r.textContent=i,n.appendChild(r)}),n}function zo(e,o){if(!e.dropmode.active)return;D(e),E(e);let t=e.dropmode.piece;if(t){e.pieces.set("a0",t);let n=T(o),r=n&&k(n,h(e),e.dom.bounds());r&&ce(e,"a0",r)}e.dom.redraw()}function jo(e,o){let t=e.dom.elements.board;if("ResizeObserver"in window&&new ResizeObserver(o).observe(e.dom.elements.wrap),(e.disableContextMenu||e.drawable.enabled)&&t.addEventListener("contextmenu",r=>r.preventDefault()),e.viewOnly)return;let n=Wt(e);t.addEventListener("touchstart",n,{passive:!1}),t.addEventListener("mousedown",n,{passive:!1})}function Uo(e,o){let t=[];if("ResizeObserver"in window||t.push(oe(document.body,"chessground.resize",o)),!e.viewOnly){let n=Io(e,So,fo),r=Io(e,wo,mo);for(let s of["touchmove","mousemove"])t.push(oe(document,s,n));for(let s of["touchend","mouseup"])t.push(oe(document,s,r));let i=()=>e.dom.bounds.clear();t.push(oe(document,"scroll",i,{capture:!0,passive:!0})),t.push(oe(window,"resize",i,{passive:!0}))}return()=>t.forEach(n=>n())}function oe(e,o,t,n){return e.addEventListener(o,t,n),()=>e.removeEventListener(o,t,n)}var Wt=e=>o=>{e.draggable.current?j(e):e.drawable.current?He(e):o.shiftKey||re(o)?e.drawable.enabled&&uo(e,o):e.viewOnly||(e.dropmode.active?zo(e,o):vo(e,o))},Io=(e,o,t)=>n=>{e.drawable.current?e.drawable.enabled&&t(e,n):e.viewOnly||o(e,n)};function Zo(e){let o=h(e),t=$(e.dom.bounds()),n=e.dom.elements.board,r=e.pieces,i=e.animation.current,s=i?i.plan.anims:new Map,p=i?i.plan.fadings:new Map,d=e.draggable.current,a=new Set,c=new Map,l=jt(e),f=new Map,g,u,A,C,y,X,me,N,U;for(u=n.firstChild;u;){if(g=u.cgKey,Xo(u))if(A=r.get(g),y=s.get(g),X=p.get(g),C=u.cgPiece,u.cgDragging&&(!d||d.orig!==g)&&(u.classList.remove("dragging"),M(u,t(m(g),o)),u.cgDragging=!1),!X&&u.cgFading&&(u.cgFading=!1,u.classList.remove("fading")),A){if(y&&u.cgAnimating&&C===te(A)){let b=m(g);b[0]+=y[2],b[1]+=y[3],u.classList.add("anim"),M(u,t(b,o))}else u.cgAnimating&&(u.cgAnimating=!1,u.classList.remove("anim"),M(u,t(m(g),o)),e.addPieceZIndex&&(u.style.zIndex=Be(m(g),o)));C===te(A)&&(!X||!u.cgFading)?a.add(g):X&&C===te(X)?(u.classList.add("fading"),u.cgFading=!0):Le(c,C,u)}else Le(c,C,u);else if(Qo(u)){let b=u.className;l.get(g)===b?(R(u,!0),l.delete(g)):Le(f,b,u)}u=u.nextSibling}for(let[b,z]of l){U=f.get(z)?.pop();let H=t(m(b),o);if(U)U.cgKey=b,e.jsHover&&(U.dataset.key=b),M(U,H),R(U,!0);else{let K=x("square",z);K.cgKey=b,e.jsHover&&(K.dataset.key=b),M(K,H),n.insertBefore(K,n.firstChild)}}for(let[,b]of f.entries())for(let z of b)R(z,!1);for(let[b,z]of r)if(y=s.get(b),!a.has(b))if(me=c.get(te(z)),N=me&&me.pop(),N){N.cgKey=b,N.cgFading&&(N.classList.remove("fading"),N.cgFading=!1);let H=m(b);e.addPieceZIndex&&(N.style.zIndex=Be(H,o)),y&&(N.cgAnimating=!0,N.classList.add("anim"),H[0]+=y[2],H[1]+=y[3]),M(N,t(H,o))}else{let H=te(z),K=x("piece",H),ne=m(b);K.cgPiece=H,K.cgKey=b,y&&(K.cgAnimating=!0,ne[0]+=y[2],ne[1]+=y[3]),M(K,t(ne,o)),e.addPieceZIndex&&(K.style.zIndex=Be(ne,o)),n.appendChild(K)}for(let b of c.values())zt(e,b)}function _o(e){let o=h(e),t=$(e.dom.bounds()),n=e.dom.elements.board.firstChild;for(;n;)(Xo(n)&&!n.cgAnimating||Qo(n))&&M(n,t(m(n.cgKey),o)),n=n.nextSibling}function Ge(e){let o=e.dom.elements.wrap.getBoundingClientRect(),t=e.dom.elements.container,n=o.height/o.width,r=Math.floor(o.width*window.devicePixelRatio/8)*8/window.devicePixelRatio,i=r*n;t.style.width=r+"px",t.style.height=i+"px",e.dom.bounds.clear(),e.addDimensionsCssVarsTo?.style.setProperty("---cg-width",r+"px"),e.addDimensionsCssVarsTo?.style.setProperty("---cg-height",i+"px")}var Xo=e=>e.tagName==="PIECE",Qo=e=>e.tagName==="SQUARE";function zt(e,o){for(let t of o)e.dom.elements.board.removeChild(t)}function Be(e,o){let n=e[1];return`${o?10-n:3+n}`}var te=e=>`${e.color} ${e.role}`,It=(e,o)=>e.lastMove?.[1]&&!e.pieces.has(e.lastMove[1])&&e.lastMove[0][0]==="e"&&["h","a"].includes(e.lastMove[1][0])&&e.lastMove[0][1]===e.lastMove[1][1]&&Xe(...m(e.lastMove[0]),...m(e.lastMove[1])).some(t=>e.pieces.has(t))?(o>e.lastMove[0]?"g":"c")+o[1]:o;function jt(e){let o=new Map;if(e.lastMove&&e.highlight.lastMove)for(let[r,i]of e.lastMove.entries())B(o,r===1?It(e,i):i,"last-move");if(e.check&&e.highlight.check&&B(o,e.check,"check"),e.selected&&(B(o,e.selected,"selected"),e.movable.showDests)){for(let r of e.movable.dests?.get(e.selected)??[])B(o,r,"move-dest"+(e.pieces.has(r)?" oc":""));for(let r of e.premovable.customDests?.get(e.selected)??e.premovable.dests??[])B(o,r,"premove-dest"+(e.pieces.has(r)?" oc":""))}let t=e.premovable.current;if(t)for(let r of t)B(o,r,"current-premove");else e.predroppable.current&&B(o,e.predroppable.current.key,"current-premove");let n=e.exploding;if(n)for(let r of n.keys)B(o,r,"exploding"+n.stage);return e.highlight.custom&&e.highlight.custom.forEach((r,i)=>{B(o,i,r)}),o}function B(e,o,t){let n=e.get(o);n?e.set(o,`${n} ${t}`):e.set(o,t)}function Le(e,o,t){let n=e.get(o);n?n.push(t):e.set(o,[t])}function Yo(e,o,t){let n=new Map,r=[];for(let p of e)n.set(p.hash,!1);let i=o.firstElementChild,s;for(;i;)s=i.getAttribute("cgHash"),n.has(s)?n.set(s,!0):r.push(i),i=i.nextElementSibling;for(let p of r)o.removeChild(p);for(let p of e)n.get(p.hash)||o.appendChild(t(p))}function Jo(e,o){let n=e.drawable.autoShapes.filter(r=>r.piece).map(r=>({shape:r,hash:Zt(r),current:!1,pendingErase:!1}));Yo(n,o,r=>Ut(e,r,e.dom.bounds()))}function et(e){let o=h(e),t=$(e.dom.bounds()),n=e.dom.elements.autoPieces?.firstChild;for(;n;)ve(n,t(m(n.cgKey),o),n.cgScale),n=n.nextSibling}function Ut(e,{shape:o,hash:t},n){let r=o.orig,i=o.piece?.role,s=o.piece?.color,p=o.piece?.scale,d=x("piece",`${i} ${s}`);return d.setAttribute("cgHash",t),d.cgKey=r,d.cgScale=p,ve(d,$(n)(m(r),h(e)),p),d}var Zt=e=>[e.orig,e.piece?.role,e.piece?.color,e.piece?.scale].join(",");function nr({el:e,config:o}){return Xt(e,o)}function Xt(e,o){let t=Ko();pe(t,o||{});function n(){let r="dom"in t?t.dom.unbind:void 0,i=$o(e,t),s=he(()=>i.board.getBoundingClientRect()),p=c=>{Zo(a),i.autoPieces&&Jo(a,i.autoPieces),!c&&i.shapes&&Ho(a,i)},d=()=>{Ge(a),_o(a),i.autoPieces&&et(a)},a=t;return a.dom={elements:i,bounds:s,redraw:Qt(p),redrawNow:p,unbind:r},a.drawable.prevSvgHash="",Ge(a),p(!1),jo(a,d),r||(a.dom.unbind=Uo(a,d)),a.events.insert?.(i),a}return Co(n(),n)}function Qt(e){let o=!1;return()=>{o||(o=!0,requestAnimationFrame(()=>{e(),o=!1}))}}export{Xt as Chessground,nr as initModule};