(function(){const s=document.createElement("link").relList;if(s&&s.supports&&s.supports("modulepreload"))return;for(const o of document.querySelectorAll('link[rel="modulepreload"]'))t(o);new MutationObserver(o=>{for(const e of o)if(e.type==="childList")for(const a of e.addedNodes)a.tagName==="LINK"&&a.rel==="modulepreload"&&t(a)}).observe(document,{childList:!0,subtree:!0});function r(o){const e={};return o.integrity&&(e.integrity=o.integrity),o.referrerPolicy&&(e.referrerPolicy=o.referrerPolicy),o.crossOrigin==="use-credentials"?e.credentials="include":o.crossOrigin==="anonymous"?e.credentials="omit":e.credentials="same-origin",e}function t(o){if(o.ep)return;o.ep=!0;const e=r(o);fetch(o.href,e)}})();function Z(n){const s=new URLSearchParams(location.search).get("host")??"*",r=o=>window.parent.postMessage({m3:1,...o},s);let t=0;return window.addEventListener("message",o=>{var a,f,h;if(o.source!==window.parent||s!=="*"&&o.origin!==s)return;const e=o.data;if(!(!e||e.m3!==1))switch(e.type){case"init":n.onInit(e),r({type:"ready"});break;case"state":n.onState(e.env);break;case"ack":(a=n.onAck)==null||a.call(n,e);break;case"view_changed":(f=n.onViewChanged)==null||f.call(n,e.view);break;case"cmd_failed":(h=n.onCmdFailed)==null||h.call(n,e.req_id,e.code);break}}),r({type:"loaded"}),{cmd(o,e={}){const a=++t;return r({type:"cmd",cmd_type:o,payload:e,req_id:a}),a},toast(o,e,a="info"){r({type:"toast",text_fa:o,text_en:e,kind:a})}}}const tt={k:{w:` `,b:` `},q:{w:` `,b:` `},r:{w:` `,b:` `},b:{w:` `,b:` `},n:{w:` `,b:` `},p:{w:` `,b:` `}};function S(n,s){var r;return!n||!s?"":((r=tt[n])==null?void 0:r[s])??""}class et{constructor(s){this.particles=[],this.shockwaves=[],this.animId=null,this.lastTime=0,this.canvas=s,this.ctx=s.getContext("2d"),this.resize(),window.addEventListener("resize",()=>this.resize()),this.startLoop()}resize(){var t,o,e;const s=(t=this.canvas.parentElement)==null?void 0:t.getBoundingClientRect();if(!s)return;const r=window.devicePixelRatio||1;this.canvas.width=Math.round(s.width*r),this.canvas.height=Math.round(s.height*r),this.canvas.style.width=`${s.width}px`,this.canvas.style.height=`${s.height}px`,(e=(o=this.ctx).resetTransform)==null||e.call(o),this.ctx.scale(r,r)}startLoop(){if(this.animId)return;const s=r=>{const t=this.lastTime?Math.min((r-this.lastTime)/1e3,.1):.016;this.lastTime=r,this.update(t),this.render(),this.animId=requestAnimationFrame(s)};this.animId=requestAnimationFrame(s)}update(s){for(let r=this.shockwaves.length-1;r>=0;r--){const t=this.shockwaves[r];t.radius+=(t.maxRadius-t.radius)*12*s,t.alpha-=t.decay*s,(t.alpha<=0||t.radius>=t.maxRadius*.98)&&this.shockwaves.splice(r,1)}for(let r=this.particles.length-1;r>=0;r--){const t=this.particles[r];t.x+=t.vx*s,t.y+=t.vy*s,t.vx*=Math.pow(t.drag,s*60),t.vy*=Math.pow(t.drag,s*60),t.vy+=t.gravity*s,t.vRot&&t.rotation!==void 0&&(t.rotation+=t.vRot*s),t.alpha-=t.decay*s,t.alpha<=0&&this.particles.splice(r,1)}}render(){const s=this.canvas.width/(window.devicePixelRatio||1),r=this.canvas.height/(window.devicePixelRatio||1);this.ctx.clearRect(0,0,s,r);for(const t of this.shockwaves)this.ctx.save(),this.ctx.beginPath(),this.ctx.arc(t.x,t.y,Math.max(1,t.radius),0,Math.PI*2),this.ctx.strokeStyle=t.color,this.ctx.globalAlpha=Math.max(0,t.alpha),this.ctx.lineWidth=t.lineWidth,this.ctx.stroke(),this.ctx.restore();for(const t of this.particles){if(this.ctx.save(),this.ctx.globalAlpha=Math.max(0,t.alpha),this.ctx.fillStyle=t.color,t.shape==="rect"||t.shape==="spark"){this.ctx.translate(t.x,t.y),t.rotation!==void 0&&this.ctx.rotate(t.rotation);const o=t.size||t.radius*2;t.shape==="spark"?this.ctx.fillRect(-o,-o*.25,o*2,o*.5):this.ctx.fillRect(-o*.5,-o*.5,o,o)}else this.ctx.beginPath(),this.ctx.arc(t.x,t.y,Math.max(.5,t.radius),0,Math.PI*2),this.ctx.fill();this.ctx.restore()}}captureImpact(s,r,t="w"){this.shockwaves.push({x:s,y:r,radius:4,maxRadius:48,color:t==="w"?"rgba(255, 235, 180, 0.9)":"rgba(120, 140, 180, 0.9)",alpha:.9,decay:2.8,lineWidth:3});const o=t==="w"?["#ffffff","#fef08a","#f59e0b","#fbbf24"]:["#94a3b8","#cbd5e1","#64748b","#38bdf8"],e=18;for(let a=0;a{if(i<0||i>7||p<0||p>7)return!1;const g=w(i,p),c=t.get(g);return!c||!c.color?(f.push({target:g,isCapture:!1}),!0):(c.color!==r&&f.push({target:g,isCapture:!0}),!1)};switch(o.type){case"p":{const i=r==="w"?1:-1,p=r==="w"?1:6,g=w(e,a+i),c=t.get(g);if(c&&!c.color&&(f.push({target:g,isCapture:!1}),a===p)){const d=w(e,a+i*2),u=t.get(d);u&&!u.color&&f.push({target:d,isCapture:!1})}for(const d of[-1,1])if(e+d>=0&&e+d<=7){const u=w(e+d,a+i),v=t.get(u);v&&v.color&&v.color!==r&&f.push({target:u,isCapture:!0})}break}case"n":{const i=[[-2,-1],[-2,1],[-1,-2],[-1,2],[1,-2],[1,2],[2,-1],[2,1]];for(const[p,g]of i){const c=e+p,d=a+g;if(c>=0&&c<=7&&d>=0&&d<=7){const u=w(c,d),v=t.get(u);!v||!v.color?f.push({target:u,isCapture:!1}):v.color!==r&&f.push({target:u,isCapture:!0})}}break}case"b":{const i=[[-1,-1],[-1,1],[1,-1],[1,1]];for(const[p,g]of i){let c=e+p,d=a+g;for(;h(c,d);)c+=p,d+=g}break}case"r":{const i=[[-1,0],[1,0],[0,-1],[0,1]];for(const[p,g]of i){let c=e+p,d=a+g;for(;h(c,d);)c+=p,d+=g}break}case"q":{const i=[[-1,-1],[-1,1],[1,-1],[1,1],[-1,0],[1,0],[0,-1],[0,1]];for(const[p,g]of i){let c=e+p,d=a+g;for(;h(c,d);)c+=p,d+=g}break}case"k":{for(let p=-1;p<=1;p++)for(let g=-1;g<=1;g++){if(p===0&&g===0)continue;const c=e+p,d=a+g;if(c>=0&&c<=7&&d>=0&&d<=7){const u=w(c,d),v=t.get(u);!v||!v.color?f.push({target:u,isCapture:!1}):v.color!==r&&f.push({target:u,isCapture:!0})}}const i=r==="w"?0:7;if(a===i&&e===4){const p=t.get(w(5,i)),g=t.get(w(6,i)),c=t.get(w(7,i));p&&!p.color&&g&&!g.color&&(c==null?void 0:c.type)==="r"&&c.color===r&&f.push({target:w(6,i),isCapture:!1});const d=t.get(w(3,i)),u=t.get(w(2,i)),v=t.get(w(1,i)),m=t.get(w(0,i));d&&!d.color&&u&&!u.color&&v&&!v.color&&(m==null?void 0:m.type)==="r"&&m.color===r&&f.push({target:w(2,i),isCapture:!1})}break}}return f.filter(i=>!ot(n,i.target,s,r))}function ot(n,s,r,t){const o=r.map(c=>{if(c.square===n)return{square:n,type:null,color:null};if(c.square===s){const d=r.find(u=>u.square===n);return{square:s,type:(d==null?void 0:d.type)??null,color:t}}return{...c}}),e=o.find(c=>c.type==="k"&&c.color===t);if(!e)return!1;const{f:a,r:f}=_(e.square),h=t==="w"?"b":"w",i=new Map;for(const c of o)i.set(c.square,c);const p=[[-2,-1],[-2,1],[-1,-2],[-1,2],[1,-2],[1,2],[2,-1],[2,1]];for(const[c,d]of p){const u=a+c,v=f+d;if(u>=0&&u<=7&&v>=0&&v<=7){const m=i.get(w(u,v));if(m&&m.color===h&&m.type==="n")return!0}}const g=h==="w"?1:-1;for(const c of[-1,1]){const d=a-c,u=f-g;if(d>=0&&d<=7&&u>=0&&u<=7){const v=i.get(w(d,u));if(v&&v.color===h&&v.type==="p")return!0}}for(const[c,d]of[[-1,0],[1,0],[0,-1],[0,1]]){let u=a+c,v=f+d;for(;u>=0&&u<=7&&v>=0&&v<=7;){const m=i.get(w(u,v));if(m&&m.color){if(m.color===h&&(m.type==="r"||m.type==="q"))return!0;break}u+=c,v+=d}}for(const[c,d]of[[-1,-1],[-1,1],[1,-1],[1,1]]){let u=a+c,v=f+d;for(;u>=0&&u<=7&&v>=0&&v<=7;){const m=i.get(w(u,v));if(m&&m.color){if(m.color===h&&(m.type==="b"||m.type==="q"))return!0;break}u+=c,v+=d}}for(let c=-1;c<=1;c++)for(let d=-1;d<=1;d++){if(c===0&&d===0)continue;const u=a+c,v=f+d;if(u>=0&&u<=7&&v>=0&&v<=7){const m=i.get(w(u,v));if(m&&m.color===h&&m.type==="k")return!0}}return!1}let l=null,H=null,E=null,C=null,k="",y=[],x=null,R=null,V=!1;const b=(n,s)=>(E==null?void 0:E.locale)==="en"?s:n,T=Z({onInit(n){E=n,document.documentElement.lang=n.locale,document.documentElement.dir=n.dir,document.getElementById("board-grid")||O(),z()},onState(n){n.t==="snap"&&(document.getElementById("board-grid")||O(),H=l,l=n.body,st(H,l),l.turn!==l.me&&(k="",y=[],x=null),z())},onAck(n){n.status==="rejected"&&T.toast("حرکت پذیرفته نشد","Move not accepted","warn")}});function st(n,s){var f;if(!n||!s||!C)return;const r=new Map(n.board.map(h=>[h.square,h])),t=new Map(s.board.map(h=>[h.square,h]));let o="",e="",a=null;for(const[h,i]of t.entries()){const p=r.get(h);p&&(p.color&&!i.color?o=h:i.color&&(!p.color||p.color!==i.color||p.type!==i.type)&&(e=h,p.color&&p.color!==i.color&&(a=p)))}if(o&&e){R={from:o,to:e};const h=document.querySelector(`[data-square="${e}"]`);if(h){const i=h.getBoundingClientRect(),p=(f=document.getElementById("vfx-canvas"))==null?void 0:f.getBoundingClientRect();if(p){const c=i.left-p.left+i.width/2,d=i.top-p.top+i.height/2;a?C.captureImpact(c,d,a.color):C.landingPuff(c,d)}const g=h.querySelector(".piece-wrapper");g&&(g.classList.remove("anim-land"),g.offsetWidth,g.classList.add("anim-land"))}}s.phase==="over"&&s.winner!==null&&!V&&(V=!0,setTimeout(()=>C==null?void 0:C.victoryConfetti(),350))}function O(){const n=document.getElementById("app");n.innerHTML=`
`;const s=document.getElementById("vfx-canvas");C=new et(s);const r=document.getElementById("board-grid");r.addEventListener("click",t=>{const o=t.target.closest(".cell");o&&o.dataset.square&&!o.disabled&&it(o.dataset.square)});for(let t=0;t<64;t++){const o=document.createElement("button");o.className="cell",o.dataset.index=String(t),r.appendChild(o)}}function z(){var m;if(!l)return;const n=l.me,s=n!=null&&l.players[n]!==void 0;let r,t;s?(r=l.players[n],t=l.players.find(M=>M.seat!==n)||l.players[1-n]):(r=l.players.find(M=>M.color==="w")||l.players[0],t=l.players.find(M=>M.color==="b")||l.players[1]);const o=(r==null?void 0:r.color)||"w",e=o==="b",a=r?l.turn===r.seat&&l.phase==="play":!1,f=t?l.turn===t.seat&&l.phase==="play":!1;N("top-bar",t,f,!1),N("bottom-bar",r,a,s);const h=new Map;for(const M of l.board)h.set(M.square,M);const i=e?[7,6,5,4,3,2,1,0]:[0,1,2,3,4,5,6,7],p=e?[0,1,2,3,4,5,6,7]:[7,6,5,4,3,2,1,0],g=document.getElementById("board-grid");if(!g)return;const c=g.children;let d=0;const u=l.phase==="play"&&l.turn===n,v=l.check&&l.phase==="play"?(m=l.board.find(M=>{var q;return M.type==="k"&&M.color===((q=l.players[l.turn])==null?void 0:q.color)}))==null?void 0:m.square:null;for(const M of p)for(const q of i){const B=String.fromCharCode(97+q)+(M+1),I=h.get(B)||{type:null,color:null},$=c[d++];$.dataset.square=B,$.dataset.color=I.color||"";const F=(q+M)%2===0?"dark":"light",j=k===B,W=R&&(R.from===B||R.to===B),D=v===B;$.className=`cell ${F}`+(j?" selected":"")+(W?" last-move":"")+(D?" in-check":"");const U=y.some(P=>P.target===B),K=y.some(P=>P.target===B&&P.isCapture);let L="";const Y=e?q===7:q===0,G=e?M===7:M===0;if(Y&&(L+=`${M+1}`),G&&(L+=`${String.fromCharCode(97+q)}`),I.type&&I.color){const P=S(I.type,I.color),X=I.type==="n"&&j?" anim-knight":"";L+=`
${P}
`}K?L+='
':U&&(L+='
'),$.innerHTML=L;const J=u&&!0&&I.color===o,Q=k?u:J;$.disabled=!Q}at()}function N(n,s,r,t){const o=document.getElementById(n);if(!o||!s||!l)return;o.className="player-bar"+(r?" active":"");const e=rt(s.color),a=nt(s.color),f=a>0?`+${a}`:"",h=s.bot?'BOT':"",i=t?`${b("شما","YOU")}`:"",p=l.phase==="over"?l.winner===s.seat?b("برنده 🏆","Winner 🏆"):"":r?t?b("نوبت شماست","Your turn"):b("در حال تفکر…","Thinking…"):"";o.innerHTML=`
${s.color==="w"?"♔":"♚"}
${s.name} ${h} ${i}
${p}
${e.map(g=>`
${S(g.type,g.color)}
`).join("")} ${f}
`}function rt(n){if(!l)return[];const s=n==="w"?"b":"w",r={p:8,n:2,b:2,r:2,q:1},t={p:0,n:0,b:0,r:0,q:0};for(const e of l.board)e.color===s&&e.type&&t[e.type]!==void 0&&t[e.type]++;const o=[];for(const e of["q","r","b","n","p"]){const a=r[e]-(t[e]||0);for(let f=0;f
${b("ارتقای پیاده به:","Promote pawn to:")}
${["q","r","b","n"].map(e=>` `).join("")}
`,n.querySelectorAll("[data-promo]").forEach(e=>{e.addEventListener("click",()=>{var f;const a=e.dataset.promo;if(x&&a){T.cmd("move",{from:x.from,to:x.to,promotion:a});const h=document.querySelector(`[data-square="${x.to}"]`);if(h){const i=h.getBoundingClientRect(),p=(f=document.getElementById("vfx-canvas"))==null?void 0:f.getBoundingClientRect();p&&(C==null||C.promotionBurst(i.left-p.left+i.width/2,i.top-p.top+i.height/2))}x=null,k="",y=[],z()}})}),(r=n.querySelector("[data-cancel-promo]"))==null||r.addEventListener("click",()=>{x=null,z()});return}if(l.phase==="over"){l.winner!==null&&(l.winner,l.me);const o=l.winner===null;let e="",a="";o?(e=l.result_reason==="stalemate"?b("پات — مساوی!","Stalemate — Draw!"):b("بازی مساوی شد","Draw!"),a=b("هیچ‌کدام از طرفین برنده نشد","No side achieved victory")):(e=`${(t=l.players[l.winner])==null?void 0:t.name} ${b("برنده شد! 🏆","Wins! 🏆")}`,a=l.result_reason==="checkmate"?b("با کیش و مات حریف","By checkmate"):l.result_reason==="abandoned"?b("به دلیل ترک حریف","By abandonment"):""),n.innerHTML=`
${e}
${a}
`;return}n.innerHTML=""}}function it(n){var o;if(!l||l.phase!=="play"||x)return;const s=l.me;if(s==null||l.turn!==s)return;const r=(o=l.players[s])==null?void 0:o.color;if(!r)return;const t=l.board.find(e=>e.square===n);if(k){if(k===n){k="",y=[],z();return}if(t!=null&&t.color&&t.color===r){k=n;try{y=A(n,l.board,r)}catch{y=[]}z();return}const e=l.board.find(f=>f.square===k),a=r==="b"?"1":"8";if((e==null?void 0:e.type)==="p"&&n[1]===a){x={from:k,to:n},k="",y=[],z();return}T.cmd("move",{from:k,to:n}),k="",y=[],z()}else if(t&&t.color===r){k=n;try{y=A(n,l.board,r)}catch{y=[]}z()}}