(()=>{"use strict";class t{constructor(t,i,e){this.family=t,this.value=i,this.red=e,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,s=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((e=void 0)||(e=Promise))((function(n,h){function l(t){try{r(s.next(t))}catch(t){h(t)}}function a(t){try{r(s.throw(t))}catch(t){h(t)}}function r(t){var i;t.done?n(t.value):(i=t.value,i instanceof e?i:new e((function(t){t(i)}))).then(l,a)}r((s=s.apply(t,i||[])).next())}));var t,i,e,s}loadImg(t,i){return new Promise(((e,s)=>{t.onload=()=>e(),t.onerror=()=>s(),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,e){this.tiles=t,this.stolenFrom=i,this.belongsTo=e}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,e,s,n,h,l){t.save(),t.translate(525,525),t.rotate(h),t.translate(-525,-525);const a=75*n,r=90*n,o=25*n/2,c=(this.belongsTo-this.stolenFrom-1+4)%4,d=void 0===l?-1:l.getFamily(),u=void 0===l?0:l.getValue(),f=(i,e,s,h)=>{i.drawTile(t,e,s,n,!1,h,i.isEqual(d,u))},m=Math.PI/2;switch(c){case 0:f(this.tiles[0],i,e+o,m),f(this.tiles[1],i+r,e,0),f(this.tiles[2],i+r+a+s*n,e,0);break;case 1:f(this.tiles[0],i,e,0),f(this.tiles[1],i+r,e+o,-m),f(this.tiles[2],i+r+a+3*s*n,e,0);break;case 2:f(this.tiles[0],i,e,0),f(this.tiles[1],i+a+s*n,e,0),f(this.tiles[2],i+r+a+s*n,e+o,-m);break;default:console.error(`Position non prise en charge: ${c}`)}t.restore()}}const e="m",s=1,n="p",h=2,l="s",a=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;ie.getFamily()===t&&e.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((e=>e.getFamily()===t&&e.getValue()===i)).length}toGroup(t=!1){if(0===this.tiles.length)return[];const i=this.tiles.pop(),e=i.getFamily(),s=i.getValue();if(this.count(e,s)>=1&&!t){const t=this.tryFormPair(i);if(t)return t}if(this.count(e,s)>=2){const e=this.tryFormTriplet(i,t);if(e)return e}const n=this.count(e,s-1)>0,h=this.count(e,s-2)>0;if(n&&h){const e=this.tryFormSequence(i,t);if(e)return e}this.tiles.push(i),this.sort()}tryFormPair(t){const e=this.find(t.getFamily(),t.getValue()),s=this.toGroup(!0);if(this.tiles.push(e),this.sort(),void 0!==s)return this.tiles.push(t),this.sort(),s.push(new i([t,e],0,0)),s}tryFormTriplet(t,e){const s=this.find(t.getFamily(),t.getValue()),n=this.find(t.getFamily(),t.getValue()),h=this.toGroup(e);if(this.tiles.push(s),this.tiles.push(n),this.sort(),void 0!==h)return h.push(new i([t,s,n],0,0)),this.tiles.push(t),this.sort(),h}tryFormSequence(t,e){const s=this.find(t.getFamily(),t.getValue()-1),n=this.find(t.getFamily(),t.getValue()-2),h=this.toGroup(e);if(this.tiles.push(s),this.tiles.push(n),this.sort(),void 0!==h)return h.push(new i([n,s,t],0,0)),this.tiles.push(t),this.sort(),h}drawHand(t,i,e,s,n,h=void 0,l=!1,a=0){const r=(75+s)*n,o=Math.cos(a)*r,c=Math.sin(a)*r;for(let s=0;st.preloadImg())))},new((e=void 0)||(e=Promise))((function(n,h){function l(t){try{r(s.next(t))}catch(t){h(t)}}function a(t){try{r(s.throw(t))}catch(t){h(t)}}function r(t){var i;t.done?n(t.value):(i=t.value,i instanceof e?i:new e((function(t){t(i)}))).then(l,a)}r((s=s.apply(t,i||[])).next())}));var t,i,e,s}cleanup(){this.tiles.forEach((t=>t.cleanup())),this.tiles=[]}}class f{constructor(t=!1){this.tiles=[],this.tileIndexMap=new Map,this.initTiles(t)}displayFamilies(t,i,e,s,n=0,h=0){let l=i,a=e;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 e of r){for(let i=e.start;i<=e.end;i++){const h=this.find(e.family,i);h&&(h.drawTile(t,l,a,s,!1,0,!1),l+=(75+n)*s)}l=i,a+=(100+h)*s}}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()),e=this.tileIndexMap.get(i);return e&&e.length>0&&(e.pop(),0===e.length?this.tileIndexMap.delete(i):this.tileIndexMap.set(i,e)),t}push(t){const i=this.tiles.length;this.tiles.push(t);const e=this.getTileKey(t.getFamily(),t.getValue()),s=this.tileIndexMap.get(e)||[];s.push(i),this.tileIndexMap.set(e,s)}find(t,i){const e=this.getTileKey(t,i),s=this.tileIndexMap.get(e);if(!s||0===s.length)return;const n=s[0];if(n>=this.tiles.length)return this.rebuildIndexMap(),this.find(t,i);[this.tiles[n],this.tiles[0]]=[this.tiles[0],this.tiles[n]],this.updateIndicesAfterSwap(0,n);const h=this.tiles.shift();return this.decrementIndicesAfterShift(),h}count(t,i){const e=this.getTileKey(t,i),s=this.tileIndexMap.get(e);return s?s.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,s=function*(){const t=this.tiles.map((t=>t.preloadImg()));yield Promise.all(t)},new((e=void 0)||(e=Promise))((function(n,h){function l(t){try{r(s.next(t))}catch(t){h(t)}}function a(t){try{r(s.throw(t))}catch(t){h(t)}}function r(t){var i;t.done?n(t.value):(i=t.value,i instanceof e?i:new e((function(t){t(i)}))).then(l,a)}r((s=s.apply(t,i||[])).next())}));var t,i,e,s}getTileKey(t,i){return`${t}-${i}`}updateIndicesAfterSwap(t,i){if(t===i)return;const e=this.tiles[t],s=this.tiles[i],n=this.getTileKey(e.getFamily(),e.getValue()),h=this.getTileKey(s.getFamily(),s.getValue()),l=this.tileIndexMap.get(n)||[],a=this.tileIndexMap.get(h)||[],r=l.indexOf(i),o=a.indexOf(t);-1!==r&&(l[r]=t),-1!==o&&(a[o]=i),this.tileIndexMap.set(n,l),this.tileIndexMap.set(h,a)}decrementIndicesAfterShift(){var t;for(const[i,e]of this.tileIndexMap.entries())this.tileIndexMap.set(i,e.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=i.x&&e<=i.x+i.width&&s>=i.y&&s<=i.y+i.height){this.selectedTile=t;break}}n!==this.selectedTile&&(this.isDirty=!0)}handleMouseDown(){void 0!==this.selectedTile&&(this.edeck.push(this.ehand.eject(this.selectedTile)),this.edeck.shuffle(),this.ehand.sort(),this.ehand.push(this.edeck.pop()),this.isDirty=!0)}initialize(){return t=this,i=void 0,s=function*(){yield Promise.all([this.deck.preload(),this.edeck.preload()]);for(let t=0;t<4;t++){const t=this.deck.getRandomHand();t.sort(),this.hands.push(t)}this.ehand=this.edeck.getRandomHand(),this.ehand.push(this.edeck.pop()),this.ehand.sort(),this.drawCanvas(),this.startAnimationLoop()},new((e=void 0)||(e=Promise))((function(n,h){function l(t){try{r(s.next(t))}catch(t){h(t)}}function a(t){try{r(s.throw(t))}catch(t){h(t)}}function r(t){var i;t.done?n(t.value):(i=t.value,i instanceof e?i:new e((function(t){t(i)}))).then(l,a)}r((s=s.apply(t,i||[])).next())}));var t,i,e,s}drawCanvas(){if(!this.isDirty)return;const t=this.offScreenCtx;t.clearRect(0,0,this.offScreenCanvas.width,this.offScreenCanvas.height),t.fillStyle="#007730",t.fillRect(50,50,1e3,1e3),t.fillStyle="#DFDFFF",t.font="50px serif",t.fillText("Exemples de main:",65,100);for(let i=0;i{const s=e-t;s>=this.INTERVAL&&(t=e-s%this.INTERVAL,this.drawCanvas()),this.animationFrameId=requestAnimationFrame(i)};this.animationFrameId=requestAnimationFrame(i)}cleanup(){null!==this.animationFrameId&&(cancelAnimationFrame(this.animationFrameId),this.animationFrameId=null),this.deck.cleanup(),this.hands.forEach((t=>t.cleanup())),this.hands=[],this.edeck.cleanup(),this.ehand.cleanup(),this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height),this.offScreenCtx.clearRect(0,0,this.offScreenCanvas.width,this.offScreenCanvas.height),this.selectedTile=void 0,this.isDirty=!0}};m.initialize().catch((t=>{console.error("Erreur lors de l'initialisation:",t)})),window.cleanup=()=>{m.cleanup()}})();