(()=>{"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(l,n){function r(t){try{a(s.next(t))}catch(t){n(t)}}function h(t){try{a(s.throw(t))}catch(t){n(t)}}function a(t){var i;t.done?l(t.value):(i=t.value,i instanceof e?i:new e((function(t){t(i)}))).then(r,h)}a((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,l,n,r){t.save(),t.translate(525,525),t.rotate(n),t.translate(-525,-525);const h=75*l,a=90*l,o=25*l/2,u=(this.belongsTo-this.stolenFrom-1+4)%4,c=void 0===r?-1:r.getFamily(),f=void 0===r?0:r.getValue(),d=(i,e,s,n)=>{i.drawTile(t,e,s,l,!1,n,i.isEqual(c,f))},g=Math.PI/2;switch(u){case 0:d(this.tiles[0],i,e+o,g),d(this.tiles[1],i+a,e,0),d(this.tiles[2],i+a+h+s*l,e,0);break;case 1:d(this.tiles[0],i,e,0),d(this.tiles[1],i+a,e+o,-g),d(this.tiles[2],i+a+h+3*s*l,e,0);break;case 2:d(this.tiles[0],i,e,0),d(this.tiles[1],i+h+s*l,e,0),d(this.tiles[2],i+a+h+s*l,e+o,-g);break;default:console.error(`Position non prise en charge: ${u}`)}t.restore()}}const e="m",s=1,l="p",n=2,r="s",h=3,a="w",o=4,u="d",c=5;class f{constructor(i){this.tiles=i.map((i=>new t(i.getFamily(),i.getValue(),i.isRed()))),this.sort()}sort(){this.tiles.sort(((t,i)=>t.isLessThan(i)?-1:1))}find(t,i){const e=this.findTileIndex(t,i);if(-1!==e){[this.tiles[e],this.tiles[0]]=[this.tiles[0],this.tiles[e]];const t=this.tiles.shift();return this.sort(),t}}findTileIndex(t,i){return this.tiles.findIndex((e=>e.getFamily()===t&&e.getValue()===i))}count(t,i){return this.tiles.filter((e=>e.getFamily()===t&&e.getValue()===i)).length}findGroups(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}if(e<=3){const l=this.count(e,s-1)>0,n=this.count(e,s-2)>0;if(l&&n){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.findGroups(!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()),l=this.find(t.getFamily(),t.getValue()),n=this.findGroups(e);if(this.tiles.push(s),this.tiles.push(l),this.sort(),void 0!==n)return n.push(new i([t,s,l],0,0)),this.tiles.push(t),this.sort(),n}tryFormSequence(t,e){const s=this.find(t.getFamily(),t.getValue()-1),l=this.find(t.getFamily(),t.getValue()-2),n=this.findGroups(e);if(this.tiles.push(s),this.tiles.push(l),this.sort(),void 0!==n)return n.push(new i([l,s,t],0,0)),this.tiles.push(t),this.sort(),n}}class d{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){return new f(this.tiles).findGroups(t)}drawHand(t,i,e,s,l,n=void 0,r=!1,h=0){const a=(75+s)*l,o=Math.cos(h)*a,u=Math.sin(h)*a;for(let s=0;st.preloadImg())))},new((e=void 0)||(e=Promise))((function(l,n){function r(t){try{a(s.next(t))}catch(t){n(t)}}function h(t){try{a(s.throw(t))}catch(t){n(t)}}function a(t){var i;t.done?l(t.value):(i=t.value,i instanceof e?i:new e((function(t){t(i)}))).then(r,h)}a((s=s.apply(t,i||[])).next())}));var t,i,e,s}cleanup(){this.tiles.forEach((t=>t.cleanup())),this.tiles=[]}}class g{constructor(t=!1){this.tiles=[],this.tileIndexMap=new Map,this.initTiles(t)}displayFamilies(t,i,e,s,l=0,n=0){let r=i,h=e;const a=[{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 a){for(let i=e.start;i<=e.end;i++){const n=this.find(e.family,i);n&&(n.drawTile(t,r,h,s,!1,0,!1),r+=(75+l)*s)}r=i,h+=(100+n)*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 l=s[0];if(l>=this.tiles.length)return this.rebuildIndexMap(),this.find(t,i);[this.tiles[l],this.tiles[0]]=[this.tiles[0],this.tiles[l]],this.updateIndicesAfterSwap(0,l);const n=this.tiles.shift();return this.decrementIndicesAfterShift(),n}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 d;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(l,n){function r(t){try{a(s.next(t))}catch(t){n(t)}}function h(t){try{a(s.throw(t))}catch(t){n(t)}}function a(t){var i;t.done?l(t.value):(i=t.value,i instanceof e?i:new e((function(t){t(i)}))).then(r,h)}a((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],l=this.getTileKey(e.getFamily(),e.getValue()),n=this.getTileKey(s.getFamily(),s.getValue()),r=this.tileIndexMap.get(l)||[],h=this.tileIndexMap.get(n)||[],a=r.indexOf(i),o=h.indexOf(t);-1!==a&&(r[a]=t),-1!==o&&(h[o]=i),this.tileIndexMap.set(l,r),this.tileIndexMap.set(n,h)}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{r.cleanup()}}else console.error("Impossible d'obtenir le contexte du canvas.")}else console.error("Canvas introuvable dans le DOM.")}))}()})();