(()=>{let b=0,M=document.body.getAttribute("data-puzzle");const A=document.querySelector("#table").innerHTML,k=document.querySelector(".confetti-screen"),j=document.querySelector(".victory"),W=document.querySelector("#new"),I=document.querySelector("#victory-new"),U=document.querySelector("#reset"),i=document.querySelector("#share-sluffle");let u=document.querySelector(".controls-settings .moves"),t=document.querySelector(".controls-settings .time");const o=document.querySelector("#hash");let a=document.querySelectorAll("#table .row").length,E,l,Y=!1,c,n=a*a,T=Array.from({length:n-1},(b,a)=>a+1);const x=new URL(window.location.href).pathname.split("/"),f=M;let v,R=window.dontcheat.load()||{},d=R[f];d?(document.querySelector(".scores .time .right")&&(document.querySelector(".scores .time .right").innerHTML=p(d.time)),document.querySelector(".scores .moves .right")&&(document.querySelector(".scores .moves .right").innerHTML=d.moves)):d={time:999999999999,moves:999999999999};function K(a){return a.toString(16).padStart(2,"0")}function Q(){Y=!0,E=Date.now(),l=window.requestAnimationFrame(B)}function G(){const b=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],a=new Date,c=b[a.getMonth()],d=a.getDate(),e=a.getFullYear(),f=`${c} ${d} ${e}`;return f}function B(){time=Date.now()-E,t.innerHTML="TIME: "+p(time),l=window.requestAnimationFrame(B)}function p(b){let f=Math.floor(b/1e3%60),c=Math.floor(b/(1e3*60)%60),d=Math.floor(b/(1e3*60*60)%24),e=Math.floor(b/(1e3*60*60*24)),a="";return e&&(a+=e+"d "),d&&(a+=d+"h "),c&&(a+=c+"m "),a+=f+"s",a.trim()}function D(b){var a=new Uint8Array((b||40)/2);return window.crypto.getRandomValues(a),Array.from(a,K).join("")}const O=x.indexOf(f);let e=x[O+1]||D(8),g="#"+e,w=!1;e==="daily"&&(g=G(),e=G(),w=!0,q()),q();function q(){if(w){let a=window.dontcheat.getCurrentStreak(f),b=document.querySelector(".streak-today .right"),c=document.querySelector(".streak-current .right"),d=document.querySelector(".streak-longest .right"),e=document.querySelector("#victory-streak");b.innerHTML=a.hasCompletedToday?"✔":"Not Yet",c.innerHTML=a.streak,d.innerHTML=a.longestStreak,e.innerHTML=a.streak}}o.innerHTML=g;function J(c){var b,e,a,d=0;for(a=c.length;a>1;a--)b=Math.floor(random()*a),a-1!=b&&(H(c,a-1,b),d++);return d}function H(a,b,c){var d=a[b];a[b]=a[c],a[c]=d}const L=(a,b)=>a.reduce((a,c,d)=>(d%b==0?a.push([c]):a[a.length-1].push(c))&&a,[]);function F(){k.classList.add("hidden"),j.classList.add("hidden"),cancelAnimationFrame(l),t.innerHTML="TIME: 0s",document.querySelector("#table").innerHTML=A,e=D(8),g="#"+e,random=new Math.seedrandom(g),o.innerHTML=g,h(),m(),b=0,u.innerHTML="moves: "+b}function N(){k.classList.add("hidden"),j.classList.add("hidden"),cancelAnimationFrame(l),t.innerHTML="TIME: 0s",document.querySelector("#table").innerHTML=A,random=new Math.seedrandom(g),o.innerHTML=g,h(),m(),b=0,u.innerHTML="moves: "+b}function h(){random=new Math.seedrandom(g);let b=[...T],d=J(b);b.push(0);let e=b.indexOf(0);d+=e+Math.floor(e/a),d%2&&H(b,0,2),c=L(b,a),S(c)}function P(){k.classList.add("hidden"),j.classList.add("hidden"),h(),W?.addEventListener("click",()=>{F(),h()}),I?.addEventListener("click",()=>{F(),h()}),i?.addEventListener("click",()=>{let a=`https://sliding.toys/mystic-square/${f}/${e}`;V(a),i.querySelector(".share-text").style.opacity=0,i.querySelector(".shared-text").style.opacity=1,setTimeout(()=>{i.querySelector(".share-text").style.opacity=1,i.querySelector(".shared-text").style.opacity=0},1500)}),U?.addEventListener("click",()=>{N(),h()}),m(),r(c),s(c),document.addEventListener("keyup",j=>{var g,h,i;let d;for(g=0;g<a;g++)for(h=0;h<a;h++)c[g][h]===0&&(d=[g,h]);let f,e=j.key;if(e==="ArrowDown"||e==="s"?f=[d[0]-1,d[1]]:e==="ArrowUp"||e==="w"?f=[d[0]+1,d[1]]:e==="ArrowRight"||e==="d"?f=[d[0],d[1]-1]:(e==="ArrowLeft"||e==="a")&&(f=[d[0],d[1]+1]),!f)return;i=document.body.querySelector(`.cell[data-position="${f.join("-")}"]`),i&&(b=y(i,c,b),c=s(),r(c)&&z())})}function z(){if(cancelAnimationFrame(l),k&&k.classList.remove("hidden"),j&&j.classList.remove("hidden"),b<d.moves){d.moves=b;const a=document.querySelector(".scores .moves .right");a&&(a.innerHTML=b),window.dontcheat.save(f,"moves",b)}if(time<d.time||d.time===void 0){d.time=time;const a=document.querySelector(".scores .time .right");a&&(a.innerHTML=p(time)),window.dontcheat.save(f,"time",time)}window.dontcheat.saveTop(f,time,b,e),w&&(window.dontcheat.saveDaily(f,time,b,e),q())}function m(){document.querySelectorAll("#table button").forEach(a=>{a.addEventListener("mousedown",()=>{b=y(a,c,b),c=s(),r(c)&&z()})})}function S(b){let c=document.getElementsByClassName("cell");for(let d=0;d<a;d++)for(let e=0;e<a;e++){if(b[d][e]==0)continue;let f=c[a*d+e];f.querySelector("span").innerHTML=b[d][e].toString(),f.setAttribute("data-value",b[d][e].toString())}return b}function s(){let d=document.getElementsByClassName("cell"),b=[];for(let c=0;c<a;c++){let d=[];for(let b=0;b<a;b++)d.push(0);b.push(d)}c=b;for(let b=0;b<n-1;b++){let a=d[b],e=a.getAttribute("data-position").split("-"),f=e[0],g=e[1],h=parseInt(a.getAttribute("data-value"));c[f][g]=h,h===v[f][g]?a.classList.add("correct"):a.classList.remove("correct")}return c}function r(d){flag=0;let c=1,b=[];for(let d=0;d<a;d++){let e=[];for(let b=1;b<=a;b++)e.push(c),c++;b.push(e)}b[a-1][a-1]=0,v=b;for(let b=0;b<a;b++)for(let c=0;c<a;c++)d[b][c]==v[b][c]&&flag++;return flag==n}function V(b){if(window.clipboardData&&window.clipboardData.setData)return window.clipboardData.setData("Text",b);if(document.queryCommandSupported&&document.queryCommandSupported("copy")){var a=document.createElement("textarea");a.textContent=b,a.style.position="fixed",document.body.appendChild(a),a.select();try{return document.execCommand("copy")}catch(a){return console.warn("Copy to clipboard failed.",a),prompt("Copy to clipboard: Ctrl+C, Enter",b)}finally{document.body.removeChild(a)}}}function y(d,g,e){let j=d.getAttribute("data-position").split("-"),b=parseInt(j[0]),c=parseInt(j[1]),h=parseInt(d.getAttribute("data-X")),i=parseInt(d.getAttribute("data-Y")),f=!0;return b>0&&g[b-1][c]==0&&(b--,i--,f=!1),b<a-1&&g[b+1][c]==0&&(b++,i++,f=!1),c>0&&g[b][c-1]==0&&(c--,h--,f=!1),c<a-1&&g[b][c+1]==0&&(c++,h++,f=!1),e===0&&Q(),f?e:(e++,d.setAttribute("data-position",b.toString()+"-"+c.toString()),d.setAttribute("data-X",h.toString()),d.setAttribute("data-Y",i.toString()),d.style.transform="translate("+h*100+"%,"+i*100+"%)",u.innerHTML="moves: "+e,e)}function X(j,g,f,b,c){const d=document.getElementById(j),e=window.devicePixelRatio||1,a=d.getContext("2d"),h=f*b+(f+1)*c,i=g*b+(g+1)*c;d.width=h*e,d.height=i*e,d.style.width=h+"px",d.style.height=i+"px",a.scale(e,e),a.fillStyle="white",a.fillRect(0,0,h,i),a.fillStyle="#efefef",a.strokeStyle="#e7e7e7",a.lineWidth=4;for(let d=0;d<g;d++)for(let k=0;k<f;k++){const e=c+k*(b+c)+a.lineWidth/2,g=c+d*(b+c)+a.lineWidth/2,h=5,i=b-a.lineWidth,j=b-a.lineWidth;a.beginPath(),a.moveTo(e+h,g),a.lineTo(e+i-h,g),a.arcTo(e+i,g,e+i,g+h,h),a.lineTo(e+i,g+j-h),a.arcTo(e+i,g+j,e+i-h,g+j,h),a.lineTo(e+h,g+j),a.arcTo(e,g+j,e,g+j-h,h),a.lineTo(e,g+h),a.arcTo(e,g,e+h,g,h),a.closePath(),a.stroke(),a.fill()}}const C=getComputedStyle(document.querySelector(".table")).getPropertyValue("--sliderSize").trim();let Z=C?parseInt(C,10):75;X("game-table",a,a,Z-10,10),P()})()