(()=>{"use strict";class t{constructor(t,i,s){this.family=t,this.value=i,this.red=s,this.imgFront=new Image,this.imgBack=new Image,this.imgGray=new Image,this.img=new Image,this.imgSrc="",this.tilt=0,this.setImgSrc()}getFamily(){return this.family}getValue(){return this.value}isEqual(t,i){return this.family===t&&this.value===i}isRed(){return this.red}compare(t){return this.family!==t.family?this.family{t.onload=null,t.onerror=null}))}preloadImg(){return t=this,i=void 0,e=function*(){const t=[{img:this.imgFront,src:"img/Regular/Front.svg"},{img:this.imgBack,src:"img/Regular/Back.svg"},{img:this.imgGray,src:"img/Regular/Gray.svg"},{img:this.img,src:this.imgSrc}];yield Promise.all(t.map((({img:t,src:i})=>this.loadImg(t,i))))},new((s=void 0)||(s=Promise))((function(h,n){function a(t){try{r(e.next(t))}catch(t){n(t)}}function l(t){try{r(e.throw(t))}catch(t){n(t)}}function r(t){var i;t.done?h(t.value):(i=t.value,i instanceof s?i:new s((function(t){t(i)}))).then(a,l)}r((e=e.apply(t,i||[])).next())}));var t,i,s,e}loadImg(t,i){return new Promise(((s,e)=>{t.onload=()=>s(),t.onerror=()=>e(),t.src=i}))}setImgSrc(){if(this.imgSrc="img/Regular/",this.family<=3){const t=["","Man","Pin","Sou"];this.imgSrc+=t[this.family]+String(this.value),this.red&&(this.imgSrc+="-Dora")}else if(4===this.family){const t=["","Ton","Nan","Shaa","Pei"];this.imgSrc+=t[this.value]}else if(5===this.family){const t=["","Chun","Hatsu","Haku"];this.imgSrc+=t[this.value]}this.imgSrc+=".svg"}}class i{constructor(t,i,s){this.tiles=t,this.stolenFrom=i,this.belongsTo=s}push(t){this.tiles.push(t)}pop(){return this.tiles.pop()}getTiles(){return this.tiles}compare(t){const i=this.tiles[0].compare(t.tiles[0]);return 0!==i?i:this.tiles[1].compare(t.tiles[1])}drawGroup(t,i,s,e,h,n,a){t.save(),t.translate(525,525),t.rotate(n),t.translate(-525,-525);const l=75*h,r=90*h,o=25*h/2,c=(this.belongsTo-this.stolenFrom-1+4)%4,d=void 0===a?-1:a.getFamily(),u=void 0===a?0:a.getValue(),m=(i,s,e,n)=>{i.drawTile(t,s,e,h,!1,n,i.isEqual(d,u))},p=Math.PI/2;switch(c){case 0:m(this.tiles[0],i,s+o,p),m(this.tiles[1],i+r,s,0),m(this.tiles[2],i+r+l+e*h,s,0);break;case 1:m(this.tiles[0],i,s,0),m(this.tiles[1],i+r,s+o,-p),m(this.tiles[2],i+r+l+3*e*h,s,0);break;case 2:m(this.tiles[0],i,s,0),m(this.tiles[1],i+l+e*h,s,0),m(this.tiles[2],i+r+l+e*h,s+o,-p);break;default:console.error(`Position non prise en charge: ${c}`)}t.restore()}}const s="m",e=1,h="p",n=2,a="s",l=3,r="w",o=4,c="d",d=5;class u{constructor(t=""){this.isolate=!1,this.tiles=[],this.initializeFromString(t)}initializeFromString(t){for(let i=0;is.getFamily()===t&&s.getValue()===i))}eject(t){if(t<0||t>=this.tiles.length)throw new Error("Invalid tile index");[this.tiles[0],this.tiles[t]]=[this.tiles[t],this.tiles[0]];const i=this.tiles.shift();return this.sort(),i}get(t){if(!(void 0===t||t<0||t>=this.tiles.length))return this.tiles[t]}sort(){this.tiles.sort(((t,i)=>t.isLessThan(i)?-1:1))}count(t,i){return this.tiles.filter((s=>s.getFamily()===t&&s.getValue()===i)).length}toGroup(t=!1){if(0===this.tiles.length)return[];const i=this.tiles.pop(),s=i.getFamily(),e=i.getValue();if(this.count(s,e)>=1&&!t){const t=this.tryFormPair(i);if(t)return t}if(this.count(s,e)>=2){const s=this.tryFormTriplet(i,t);if(s)return s}const h=this.count(s,e-1)>0,n=this.count(s,e-2)>0;if(h&&n){const s=this.tryFormSequence(i,t);if(s)return s}this.tiles.push(i),this.sort()}tryFormPair(t){const s=this.find(t.getFamily(),t.getValue()),e=this.toGroup(!0);if(this.tiles.push(s),this.sort(),void 0!==e)return this.tiles.push(t),this.sort(),e.push(new i([t,s],0,0)),e}tryFormTriplet(t,s){const e=this.find(t.getFamily(),t.getValue()),h=this.find(t.getFamily(),t.getValue()),n=this.toGroup(s);if(this.tiles.push(e),this.tiles.push(h),this.sort(),void 0!==n)return n.push(new i([t,e,h],0,0)),this.tiles.push(t),this.sort(),n}tryFormSequence(t,s){const e=this.find(t.getFamily(),t.getValue()-1),h=this.find(t.getFamily(),t.getValue()-2),n=this.toGroup(s);if(this.tiles.push(e),this.tiles.push(h),this.sort(),void 0!==n)return n.push(new i([h,e,t],0,0)),this.tiles.push(t),this.sort(),n}drawHand(t,i,s,e,h,n=void 0,a=!1,l=0){const r=(75+e)*h,o=Math.cos(l)*r,c=Math.sin(l)*r;for(let e=0;et.preloadImg())))},new((s=void 0)||(s=Promise))((function(h,n){function a(t){try{r(e.next(t))}catch(t){n(t)}}function l(t){try{r(e.throw(t))}catch(t){n(t)}}function r(t){var i;t.done?h(t.value):(i=t.value,i instanceof s?i:new s((function(t){t(i)}))).then(a,l)}r((e=e.apply(t,i||[])).next())}));var t,i,s,e}cleanup(){this.tiles.forEach((t=>t.cleanup())),this.tiles=[]}}class m{constructor(t=!1){this.tiles=[],this.tileIndexMap=new Map,this.initTiles(t)}displayFamilies(t,i,s,e,h=0,n=0){let a=i,l=s;const r=[{family:1,start:1,end:9},{family:2,start:1,end:9},{family:3,start:1,end:9},{family:4,start:1,end:4},{family:5,start:1,end:3}];for(const s of r){for(let i=s.start;i<=s.end;i++){const n=this.find(s.family,i);n&&(n.drawTile(t,a,l,e,!1,0,!1),a+=(75+h)*e)}a=i,l+=(100+n)*e}}length(){return this.tiles.length}pop(){if(0===this.tiles.length)throw new Error("Cannot pop from an empty deck");const t=this.tiles.pop(),i=this.getTileKey(t.getFamily(),t.getValue()),s=this.tileIndexMap.get(i);return s&&s.length>0&&(s.pop(),0===s.length?this.tileIndexMap.delete(i):this.tileIndexMap.set(i,s)),t}push(t){const i=this.tiles.length;this.tiles.push(t);const s=this.getTileKey(t.getFamily(),t.getValue()),e=this.tileIndexMap.get(s)||[];e.push(i),this.tileIndexMap.set(s,e)}find(t,i){const s=this.getTileKey(t,i),e=this.tileIndexMap.get(s);if(!e||0===e.length)return;const h=e[0];if(h>=this.tiles.length)return this.rebuildIndexMap(),this.find(t,i);[this.tiles[h],this.tiles[0]]=[this.tiles[0],this.tiles[h]],this.updateIndicesAfterSwap(0,h);const n=this.tiles.shift();return this.decrementIndicesAfterShift(),n}count(t,i){const s=this.getTileKey(t,i),e=this.tileIndexMap.get(s);return e?e.length:0}shuffle(){for(let t=this.tiles.length-1;t>0;t--){const i=Math.floor(Math.random()*(t+1));[this.tiles[t],this.tiles[i]]=[this.tiles[i],this.tiles[t]]}this.rebuildIndexMap()}getRandomHand(){const t=new u;if(this.shuffle(),this.tiles.length<13)throw new Error("Not enough tiles in deck to create a hand");for(let i=0;i<13;i++)t.push(this.pop());return t}cleanup(){this.tiles.forEach((t=>t.cleanup())),this.tiles=[],this.tileIndexMap.clear()}preload(){return t=this,i=void 0,e=function*(){const t=this.tiles.map((t=>t.preloadImg()));yield Promise.all(t)},new((s=void 0)||(s=Promise))((function(h,n){function a(t){try{r(e.next(t))}catch(t){n(t)}}function l(t){try{r(e.throw(t))}catch(t){n(t)}}function r(t){var i;t.done?h(t.value):(i=t.value,i instanceof s?i:new s((function(t){t(i)}))).then(a,l)}r((e=e.apply(t,i||[])).next())}));var t,i,s,e}getTileKey(t,i){return`${t}-${i}`}updateIndicesAfterSwap(t,i){if(t===i)return;const s=this.tiles[t],e=this.tiles[i],h=this.getTileKey(s.getFamily(),s.getValue()),n=this.getTileKey(e.getFamily(),e.getValue()),a=this.tileIndexMap.get(h)||[],l=this.tileIndexMap.get(n)||[],r=a.indexOf(i),o=l.indexOf(t);-1!==r&&(a[r]=t),-1!==o&&(l[o]=i),this.tileIndexMap.set(h,a),this.tileIndexMap.set(n,l)}decrementIndicesAfterShift(){var t;for(const[i,s]of this.tileIndexMap.entries())this.tileIndexMap.set(i,s.filter((t=>0!==t)).map((t=>t>0?t-1:t))),0===(null===(t=this.tileIndexMap.get(i))||void 0===t?void 0:t.length)&&this.tileIndexMap.delete(i)}rebuildIndexMap(){this.tileIndexMap.clear();for(let t=0;t{this.animationFrameId=requestAnimationFrame(t);const s=i-this.lastFrameTime;s{this.canvas.removeEventListener("mousemove",t),this.canvas.removeEventListener("mousedown",i)}))}handleMouseMove(t){const i=this.canvas.getBoundingClientRect(),s=t.clientX-i.left,e=t.clientY-i.top,h=this.selectedTile;this.selectedTile=void 0;for(let t=0;t=i.x&&s<=i.x+i.width&&e>=i.y&&e<=i.y+i.height){this.selectedTile=t;break}}h!==this.selectedTile&&(this.isDirty=!0)}handleMouseDown(){void 0!==this.selectedTile&&(this.decks[0].push(this.hands[8].eject(this.selectedTile)),this.decks[0].shuffle(),this.hands[8].sort(),this.hands[8].push(this.decks[0].pop()),this.isDirty=!0)}preloadDeck(t){return p(this,void 0,void 0,(function*(){yield t.preload()}))}preloadHand(t){return p(this,void 0,void 0,(function*(){yield t.preload()}))}initialize(){return p(this,void 0,void 0,(function*(){try{this.decks.push(new m(!1)),this.hands.push(new u("s1s2s3"),new u("m2m3m4"),new u("p5p5p5"),new u("w2w2w2"),new u("d3d3d3"),new u("s4p5m6"),new u("m9s9p9"),new u("d1d2d3"),this.decks[0].getRandomHand()),yield Promise.all([...this.decks.map((t=>this.preloadDeck(t))),...this.hands.map((t=>this.preloadHand(t)))]),this.hands[8].push(this.decks[0].pop()),this.hands[8].sort(),this.initEventListeners(),this.isDirty=!0,this.drawFrame(),this.startAnimationLoop()}catch(t){console.error("Erreur d'initialisation:",t)}}))}cleanup(){null!==this.animationFrameId&&(cancelAnimationFrame(this.animationFrameId),this.animationFrameId=null),this.cleanupCallbacks.forEach((t=>t())),this.cleanupCallbacks=[],this.decks.forEach((t=>t.cleanup())),this.hands.forEach((t=>t.cleanup())),this.decks=[],this.hands=[],this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height),this.staticCtx.clearRect(0,0,this.staticCanvas.width,this.staticCanvas.height)}}let E=null;function v(){E&&(E.cleanup(),E=null)}"undefined"!=typeof window&&function(){return p(this,void 0,void 0,(function*(){try{E=new I(g),yield E.initialize(),window.cleanup=v}catch(t){console.error("Erreur lors de l'initialisation:",t)}}))}().catch(console.error)})();