var c=Object.defineProperty;var k=(e,t,i)=>t in e?c(e,t,{enumerable:!0,configurable:!0,writable:!0,value:i}):e[t]=i;var r=(e,t,i)=>k(e,typeof t!="symbol"?t+"":t,i);import{E as f,u as g}from"./index-BRMV7KFd.js";import{M as y,S as I,a as d,b as l,c as u,d as x,u as M}from"./MixStore-C8oBH7hU.js";class S{constructor(t){r(this,"errors",[]);this.mix=t}validate(){switch(this.errors=[],this.mix.state==="closed"&&this.mix.items.length===0&&this.errors.push({key:"mix_does_not_have_any_items"}),this.mix.kind){case"musli":case"porridge":{this.baseMixCase();break}case"dry_fruits":case"crunchy_fruits":{this.crunchyFruitsCase();break}case"biscuits":{this.biscuitsCase();break}case"mixit_bars":{this.mixitBarsCase();break}case"fruit_bags":{this.bagsCase();break}}}baseMixCase(){const t=this.findBaseItems(),[i]=t;this.mix.state==="closed"&&i===void 0&&this.errors.push({key:"base_not_found"}),i&&(t.length>1&&this.errors.push({key:"mix_cant_have_more_than_1_base"}),i.amount>1&&this.errors.push({key:"base_quantity_is_more_than_1"})),this.mix.hasBase&&this.mix.basePercent<y&&this.errors.push({key:"mix_is_full"})}crunchyFruitsCase(){if(this.mix.nonBaseItemsVolume>I&&this.errors.push({key:"mix_is_full"}),this.mix.state==="closed"&&this.mix.nonBaseItemsVolume<d){const t=Math.round(this.mix.nonBaseItemsVolume/this.mix.tubeVolume*100),i=Math.round(d/this.mix.tubeVolume*100);this.errors.push({key:"mix_is_not_full_enough",data:{current_volume:t,max_volume:i}})}}biscuitsCase(){this.mix.totalNonBaseItemsQuantity>l&&this.errors.push({key:"mix_is_full"}),this.mix.state==="closed"&&this.mix.totalNonBaseItemsQuantity<l&&this.errors.push({key:"mix_has_less_items_than_x",data:{current_count:7}})}bagsCase(){this.mix.totalNonBaseItemsQuantity>u&&this.errors.push({key:"mix_is_full"}),this.mix.state==="closed"&&this.mix.totalNonBaseItemsQuantity<u&&this.errors.push({key:"mix_has_less_items_than_x",data:{current_count:10}})}mixitBarsCase(){this.mix.totalNonBaseItemsQuantity>x&&this.errors.push({key:"mix_is_full"}),this.mix.state==="closed"&&this.mix.totalNonBaseItemsQuantity<x&&this.errors.push({key:"mix_has_less_items_than_x",data:{current_count:8}})}findBaseItems(){return this.mix.items.filter(t=>t.ingredient.isBase)}isValid(){return this.validate(),this.errors.length===0}getErrors(){return this.errors}}class p{constructor(t){r(this,"mixStore",M());r(this,"modalStore",g());r(this,"t");r(this,"validator");this.kind=t,this.t=i=>f.global.t(i),this.validator=new S(this.mixStore.getMix(t))}addIngredient(t){var o,n;const{ingredient:i,quantity:a}=t;if((o=this.mixStore.getMix(this.kind))==null?void 0:o.items.find(s=>s.ingredient.id===i.id)){if(this.mixStore.increaseQuantity({kind:this.kind,ingredient:i,quantity:a}),!this.validator.isValid()){const[s]=this.validator.getErrors(),h=this.t(`general.mix.validation.${s.key}`,s.data);this.mixStore.decreaseQuantity({kind:this.kind,ingredient:i,quantity:a}),this.modalStore.openModal({id:"modal-alert",title:h})}}else{const s=this.mixStore.getMix(this.kind);if(s&&(i.isBase&&s.shouldHasBase&&s.hasBase?(this.mixStore.changeBase({kind:this.kind,ingredient:i}),this.validator.isValid()&&this.modalStore.openModal({id:"modal-alert",title:this.t("general.mix.validation.base_was_changed")})):this.mixStore.addItem({kind:this.kind,ingredient:i,quantity:a})),!this.validator.isValid()){const[h]=this.validator.getErrors(),_=this.t(`general.mix.validation.${h.key}`,h.data);this.mixStore.removeItem({kind:this.kind,ingredient:i}),this.modalStore.openModal({id:"modal-alert",title:_})}}(n=this.mixStore.getMix(this.kind))==null||n.calculateNutrients()}removeIngredient(t){var m,o;const{ingredient:i}=t,a=(m=this.mixStore.getMix(this.kind))==null?void 0:m.items.find(n=>n.ingredient.id===i.id);a&&a.amount>1?(this.mixStore.decreaseQuantity({kind:this.kind,ingredient:i,quantity:1}),this.validator.isValid()||(this.mixStore.increaseQuantity({kind:this.kind,ingredient:i,quantity:1}),alert(this.validator.getErrors()))):(this.mixStore.removeItem({kind:this.kind,ingredient:i}),this.validator.isValid()||(this.mixStore.addItem({kind:this.kind,ingredient:i,quantity:1}),alert(this.validator.getErrors()))),(o=this.mixStore.getMix(this.kind))==null||o.calculateNutrients()}}export{p as M,S as a};