(()=>{"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 a(t){try{r(s.next(t))}catch(t){h(t)}}function l(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(a,l)}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,a){t.save(),t.translate(525,525),t.rotate(h),t.translate(-525,-525);const l=75*n,r=90*n,o=25*n/2,u=(this.belongsTo-this.stolenFrom-1+4)%4,d=void 0===a?-1:a.getFamily(),f=void 0===a?0:a.getValue(),c=(i,e,s,h)=>{i.drawTile(t,e,s,n,!1,h,i.isEqual(d,f))},m=Math.PI/2;switch(u){case 0:c(this.tiles[0],i,e+o,m),c(this.tiles[1],i+r,e,0),c(this.tiles[2],i+r+l+s*n,e,0);break;case 1:c(this.tiles[0],i,e,0),c(this.tiles[1],i+r,e+o,-m),c(this.tiles[2],i+r+l+3*s*n,e,0);break;case 2:c(this.tiles[0],i,e,0),c(this.tiles[1],i+l+s*n,e,0),c(this.tiles[2],i+r+l+s*n,e+o,-m);break;default:console.error(`Position non prise en charge: ${u}`)}t.restore()}}const e="m",s=1,n="p",h=2,a="s",l=3,r="w",o=4,u="d",d=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 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.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()),n=this.find(t.getFamily(),t.getValue()),h=this.findGroups(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.findGroups(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}}class c{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,n,h=void 0,a=!1,l=0){const r=(75+s)*n,o=Math.cos(l)*r,u=Math.sin(l)*r;for(let s=0;st.preloadImg())))},new((e=void 0)||(e=Promise))((function(n,h){function a(t){try{r(s.next(t))}catch(t){h(t)}}function l(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(a,l)}r((s=s.apply(t,i||[])).next())}));var t,i,e,s}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,e,s,n=0,h=0){let a=i,l=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,a,l,s,!1,0,!1),a+=(75+n)*s)}a=i,l+=(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 c;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 a(t){try{r(s.next(t))}catch(t){h(t)}}function l(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(a,l)}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()),a=this.tileIndexMap.get(n)||[],l=this.tileIndexMap.get(h)||[],r=a.indexOf(i),o=l.indexOf(t);-1!==r&&(a[r]=t),-1!==o&&(l[o]=i),this.tileIndexMap.set(n,a),this.tileIndexMap.set(h,l)}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;t1){let a=e.find(s,n),l=e.find(s,n);i.push(t),i.push(a),i.push(l),h+=3}else e.push(t),e.shuffle()}else{let t=e.pop(),s=t.getFamily(),n=t.getValue();if(s<4&&1!==n&&n<6&&e.count(s,n+1)>0&&e.count(s,n+2)>0){let a=e.find(s,n+1),l=e.find(s,n+2);i.push(t),i.push(a),i.push(l),h+=3}else e.push(t),e.shuffle()}return i.sort(),i},brelan_valeur(t=0){let i,e,s=new c,n=new m;n.shuffle(),Math.random()<1/3?(i=5,e=Math.floor(3*Math.random())+1):Math.random()<.5?(i=4,e=1):(i=4,e=t+1),s.push(n.find(i,e)),s.push(n.find(i,e)),s.push(n.find(i,e));let h=3;for(i=Math.floor(3*Math.random())+1,e=Math.floor(9*Math.random())+1;n.count(i,e)<2;)i=Math.floor(3*Math.random())+1,e=Math.floor(9*Math.random())+1;for(s.push(n.find(i,e)),s.push(n.find(i,e)),h+=2;14!==h;)if(Math.random()<.5){let t=n.pop(),i=t.getFamily(),e=t.getValue();n.count(i,e)>1?(s.push(t),s.push(n.find(i,e)),s.push(n.find(i,e)),h+=3):(n.push(t),n.shuffle())}else{let t=Math.floor(3*Math.random())+1,i=Math.floor(7*Math.random())+1;n.count(t,i)>1&&n.count(t,i+1)>1&&n.count(t,i+2)>1&&(s.push(n.find(t,i)),s.push(n.find(t,i+1)),s.push(n.find(t,i+2)),h+=3)}return s.sort(),s},main_pure(t=0){let i=new c,e=new m;e.shuffle();let s=Math.floor(3*Math.random())+1,n=Math.floor(9*Math.random())+1;i.push(e.find(s,n)),i.push(e.find(s,n));let h=2;for(;h<14;)if(Math.random()<.5){let t=Math.floor(9*Math.random())+1;if(e.count(s,t)>2)for(let n=0;n<3;n++)i.push(e.find(s,t)),h++}else{let t=Math.floor(7*Math.random())+1;if(e.count(s,t)>0&&e.count(s,t+1)>0&&e.count(s,t+2)>0)for(let n=0;n<3;n++)i.push(e.find(s,t+n)),h++}return i.sort(),i},main_semi_pure(t=0){let i=new c,e=new m;e.shuffle();let s=Math.floor(3*Math.random())+1,n=Math.floor(9*Math.random())+1;i.push(e.find(s,n)),i.push(e.find(s,n));let h=2,a=Math.floor(2*Math.random())+4,l=Math.floor(3*Math.random())+1;for(i.push(e.find(a,l)),i.push(e.find(a,l)),i.push(e.find(a,l)),h+=3;h<14;)if(Math.random()<.5)if(Math.random()<.5){let t=Math.floor(9*Math.random())+1;if(e.count(s,t)>2)for(let n=0;n<3;n++)i.push(e.find(s,t)),h++}else if(Math.random()<.5){let t=Math.floor(4*Math.random())+1;if(e.count(4,t)>2)for(let s=0;s<3;s++)i.push(e.find(4,t)),h++}else{let t=Math.floor(3*Math.random())+1;if(e.count(5,t)>2)for(let s=0;s<3;s++)i.push(e.find(5,t)),h++}else{let t=Math.floor(7*Math.random())+1;if(e.count(s,t)>0&&e.count(s,t+1)>0&&e.count(s,t+2)>0)for(let n=0;n<3;n++)i.push(e.find(s,t+n)),h++}return i.sort(),i},double_suite(t=0){let i=new c,e=new m;e.shuffle();let s=e.pop(),n=e.find(s.getFamily(),s.getValue());i.push(s),i.push(n);let h=2,a=Math.floor(3*Math.random())+1,l=Math.floor(7*Math.random())+1;for(;e.count(a,l)<2||e.count(a,l+1)<2||e.count(a,l+2)<2;)a=Math.floor(3*Math.random())+1,l=Math.floor(7*Math.random())+1;for(i.push(e.find(a,l)),i.push(e.find(a,l)),i.push(e.find(a,l+1)),i.push(e.find(a,l+1)),i.push(e.find(a,l+2)),i.push(e.find(a,l+2)),h+=6;14!==h;)if(Math.random()<.5){let t=e.pop(),s=t.getFamily(),n=t.getValue();e.count(s,n)>1?(i.push(t),i.push(e.find(s,n)),i.push(e.find(s,n)),h+=3):(e.push(t),e.shuffle())}else{let t=Math.floor(3*Math.random())+1,s=Math.floor(7*Math.random())+1;e.count(t,s)>1&&e.count(t,s+1)>1&&e.count(t,s+2)>1&&(i.push(e.find(t,s)),i.push(e.find(t,s+1)),i.push(e.find(t,s+2)),h+=3)}return i.sort(),i},sept_pairs(i=0){let e=new c,s=new m;s.shuffle();let n=0;for(;7!==n;){let i=s.pop();e.getTiles().every((t=>0!==t.compare(i)))&&(n++,e.push(i),e.push(new t(i.getFamily(),i.getValue(),!1)))}return e.sort(),e}};var g=function(t,i,e,s){return new(e||(e=Promise))((function(n,h){function a(t){try{r(s.next(t))}catch(t){h(t)}}function l(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(a,l)}r((s=s.apply(t,i||[])).next())}))};const M="myCanvas",y=1e3/30;class w{constructor(t=M){this.hands=[],this.handTexts=[],this.wind=Math.floor(4*Math.random()),this.animationFrameId=null,this.lastFrameTime=0,this.cleanupCallbacks=[],this.BASE_X=50,this.BASE_Y=90,this.DY=160,this.SIZE=.75;const i=document.getElementById(t);if(!i)throw new Error(`Canvas avec ID '${t}' introuvable`);this.canvas=i;const e=i.getContext("2d");if(!e)throw new Error("Impossible d'obtenir le contexte 2D du canvas");this.ctx=e}drawFrame(){this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height),this.drawHandsAndLabels()}drawHandsAndLabels(){for(let t=0;t{this.animationFrameId=requestAnimationFrame(t);const e=i-this.lastFrameTime;ethis.preloadHand(t)))]),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.hands.forEach((t=>t.cleanup())),this.hands=[]}}let v=null;function F(){v&&(v.cleanup(),v=null)}"undefined"!=typeof window&&function(){return g(this,void 0,void 0,(function*(){try{v=new w(M),yield v.initialize(),window.cleanup=F}catch(t){console.error("Erreur lors de l'initialisation:",t)}}))}().catch(console.error)})();