/* Copyright © 2024 Melbet - All Rights Reserved */ var et=Object.defineProperty,at=Object.defineProperties;var ot=Object.getOwnPropertyDescriptors;var z=Object.getOwnPropertySymbols;var st=Object.prototype.hasOwnProperty,it=Object.prototype.propertyIsEnumerable;var q=(t,e,a)=>e in t?et(t,e,{enumerable:!0,configurable:!0,writable:!0,value:a}):t[e]=a,Y=(t,e)=>{for(var a in e||(e={}))st.call(e,a)&&q(t,a,e[a]);if(z)for(var a of z(e))it.call(e,a)&&q(t,a,e[a]);return t},$=(t,e)=>at(t,ot(e));import{alert as nt,deepMerge as lt,AlertIcons as rt,AlertIconsSvg as ct,CouponSaleErrorCodes as ut,questionAlert as mt,errorAlert as dt,successAlert as j,BaseCoupon as ft,disableReactivity as ht,minus as pt,divide as K,convertToView as G,createMarket as wt,CouponBet as It,uniqueBy as At,parseCouponType as yt,COUPON_TYPES_WITH_SYSTEM as _t,formatMoney as Q,multiply as St,WebApi as vt,useAccountStore as Pt,useGlobalStore as gt,useT as Tt,useNotifications as xt}from"./entry-Bl6dkD65.js";import{computed as Eref as F,unref as m,watch as H,onMounted as bt,onUnmounted as Bt}from"../../../../shared-assets/__shared_vue_deps_SRT2KB67.js";import{createSharedComposable as Ot}from"./SrvKkP3v8vpZ.js";import{useStartIntervalUpdate as Ct}from"./1jWecUzQCXJh.js";import{getCouponStatusDictionaryKey as Et,useWebApiInstance as J,BetsHistorySaleApi as Ft}from"./Dips4JSFyLXy.js";import{fixFloat as Nt}from"./6BifvK_5jOPo.js";const Dt=(t,e,...a)=>{let o,r;const l=new Promise((u,f)=>{o=u,r=f}),i=setTimeout(()=>o(t(...a)),e);return Object.assign(l,{timeoutId:i,cancel:()=>r(clearTimeout(i))})};var Rt=(t,e,a)=>new Promise((o,r)=>{var l=u=>{try{c(a.next(u))}catch(f){r(f)}},i=u=>{try{c(a.throw(u))}catch(f){r(f)}},c=u=>u.done?o(u.value):Promise.resolve(u.value).then(l,i);c((a=a.apply(t,e)).next())});const Vt=t=>Rt(void 0,[t],function*({T:e,text:a,title:o="",options:r={}}){return nt({T:e,options:lt({title:o,html:a,icon:rt.INFO,iconHtml:ct.INFO,customClass:{icon:"swal2-icon--info"}},r)})}),U=async(t,{couponSaleApi:e,account:a,activeAccountId:o=a.id,amount:r,couponId:l,guid:i,newCouponAmount:c,order:u,onSaleCoupon:f,onUpdateCouponSaleInfo:w,successAlertSubstitutionCallback:A,infoAlertSubstitutionCallback:d,questionAlertSubstitutionCallback:_,errorAlertSubstitutionCallback:g})=>{var x;const{T:h}=t,b=!c,B={amount:r,couponId:l,order:u,guid:i,newCouponAmount:c},{data:p,success:O,error:y}=await e.saleCoupon(B);if(O){if(p.waitTime)return Dt(U,p.waitTime,t,{couponSaleApi:e,account:a,activeAccountId:o,amount:r,couponId:l,guid:p.guid,newCouponAmount:c,onUpdateCouponSaleInfo:w,onSaleCoupon:f,successAlertSubstitutionCallback:A,infoAlertSubstitutionCallback:d,questionAlertSubstitutionCallback:_,errorAlertSubstitutionCallback:g});f==null||f(p),u?A?A(h("coupon_was_ordered",{couponId:l})):j({T:h,text:h("coupon_was_ordered",{couponId:l})}):a.id===o?A?A(h("coupon_was_sold",{couponId:l,amount:p.couponSaleInfo.maxSaleAmount,currency:a.iso})):j({T:h,text:h("coupon_was_sold",{couponId:l,amount:p.couponSaleInfo.maxSaleAmount,currency:a.iso})}):A?A(h("coupon_was_sold_to_account",{couponId:l,account:a.label,amount:p.couponSaleInfo.maxSaleAmount,currency:a.iso})):j({T:h,text:h("coupon_was_sold_to_account",{couponId:l,account:a.label,amount:p.couponSaleInfo.maxSaleAmount,currency:a.iso})})}else if(y.code===ut.AMOUNT_CHANGED)if(p!==void 0&&(w==null||w(p.couponSaleInfo)),b){let v;if(_?v=await _(h("coupon_sale_price",{amount:p.couponSaleInfo.maxSaleAmount,currency:a.iso}),{title:y.message}):v=await mt({T:h,text:h("coupon_sale_price",{amount:p.couponSaleInfo.maxSaleAmount,currency:a.iso}),title:y.message}),v.value)return U(t,{couponSaleApi:e,account:a,activeAccountId:o,amount:(x=p==null?void 0:p.couponSaleInfo.maxSaleAmount)!=null?x:r,couponId:l,onUpdateCouponSaleInfo:w,onSaleCoupon:f,successAlertSubstitutionCallback:A,infoAlertSubstitutionCallback:d,questionAlertSubstitutionCallback:_,errorAlertSubstitutionCallback:g})}else d?await d(y.message):await Vt({T:h,text:y.message});else g?g(y.message):dt({T:h,text:y.message})};var Lt=Object.defineProperty,X=Object.getOwnPropertySymbols,Mt=Object.prototype.hasOwnProperty,Wt=Object.prototype.propertyIsEnumerable,jt=(t,e,a)=>e in t?Lt(t,e,{enumerable:!0,configurable:!0,writable:!0,value:a}):t[e]=a,Ht=(t,e)=>{var a={};for(var o in t)Mt.call(t,o)&&e.indexOf(o)<0&&(a[o]=t[o]);if(t!=null&&X)for(var o of X(t))e.indexOf(o)<0&&Wt.call(t,o)&&(a[o]=t[o]);return a},n=(t,e,a)=>(jt(t,typeof e!="symbol"?e+"":e,a),a);const Gt=["amount","hasSaleTransactions","insurancePercent","isAllowInsurance","isAllowPrint","isAllowSale","saleAmount","saleInfo","autoSaleSum","autosaleStatus","statusId"];class Ut extends ft{constructor(e){var a=e,{blocks:o,createdDateTime:r,createdDateTimeFormatted:l,insurancePercent:i,hasSaleTransactions:c=!1,isAllowEdit:u=!1,isAllowInsurance:f=!1,isAllowPrint:w=!1,isAllowRepeat:A=!1,isAllowSale:d=!1,isPromo:_=!1,isPowerBet:g=!1,initialAmount:x,closedPrepaidAmount:h,toPrepaidAmount:b,saleAmount:B=0,saleInfo:p,autoSaleSum:O,autoSaleStatus:y,statusId:v=10,tax:V,possibleWinAmount:L=0,winAmount:C,maxPayout:M,currencyIso:W,cancelReason:N="",typeLabel:D=""}=a,R=Ht(a,["blocks","createdDateTime","createdDateTimeFormatted","insurancePercent","hasSaleTransactions","isAllowEdit","isAllowInsurance","isAllowPrint","isAllowRepeat","isAllowSale","isPromo","isPowerBet","initialAmount","closedPrepaidAmount","toPrepaidAmount","saleAmount","saleInfo","autoSaleSum","autoSaleStatus","statusId","tax","possibleWinAmount","winAmount","maxPayout","currencyIso","cancelReason","typeLabel"]);super(R),n(this,"blocks"),n(this,"createdDateTime"),n(this,"createdDateTimeFormatted"),n(this,"insurancePercent"),n(this,"hasSaleTransactions"),n(this,"isAllowEdit"),n(this,"isAllowInsurance"),n(this,"isAllowPrint"),n(this,"isAllowRepeat"),n(this,"isAllowSale"),n(this,"isPromo"),n(this,"initialAmount"),n(this,"closedPrepaidAmount"),n(this,"toPrepaidAmount"),n(this,"saleAmount"),n(this,"saleInfo"),n(this,"autoSaleSum"),n(this,"statusId"),n(this,"tax"),n(this,"possibleWinAmount"),n(this,"winAmount"),n(this,"maxPayout"),n(this,"currencyIso"),n(this,"cancelReason"),n(this,"typeLabel"),n(this,"isPowerBet"),n(this,"autoSaleStatus"),this.blocks=o,this.createdDateTime=r,this.createdDateTimeFormatted=l,this.insurancePercent=i,this.hasSaleTransactions=c,this.isAllowEdit=u,this.isAllowInsurance=f,this.isAllowPrint=w,this.isAllowRepeat=A,this.isAllowSale=d,this.isPromo=_,this.isPowerBet=g,this.initialAmount=x,this.closedPrepaidAmount=h,this.toPrepaidAmount=b,this.saleAmount=B,this.saleInfo=p,this.autoSaleSum=O,this.autoSaleStatus=y,this.statusId=v,this.tax=V,this.possibleWinAmount=L,this.winAmount=C,this.maxPayout=M,this.currencyIso=W,this.cancelReason=N,this.typeLabel=D,ht(this,Gt)}get cutAmount(){return pt(this.initialAmount,this.amount)}get saleCoef(){return K(this.saleAmount,this.cutAmount)}get saleCoefByView(){return G(this.saleCoef,this.coefViewId)}get winCoef(){return K(this.winAmount,this.amount)}get winCoefByView(){return G(this.winCoef,this.coefViewId)}get isCanRepeat(){return!!(this.isAllowRepeat&&!this.hasSpecialCoef&&!this.hasBonus)}get isCanEdit(){return!!(this.isAllowEdit&&!this.hasSpecialCoef&&!this.hasBonus)}get isCanSale(){return!!(this.isAllowSale&&!this.hasSpecialCoef&&this.saleInfo)}get isCanInsurance(){return this.isAllowInsurance&&!this.hasSpecialCoef}get statusLabel(){return Et(this.statusId)}sell(e=0,a=this.initialAmount){e||(this.statusId=8,this.isAllowInsurance=!1,this.isAllowPrint=!1,this.isAllowSale=!1),this.amount=e,this.saleAmount=a,this.hasSaleTransactions=!0}insure(e=100){this.insurancePercent=e,this.isAllowSale=!1}}const zt=t=>Number.isNaN(parseFloat(t))?t:parseFloat(t),qt=(t,e,a)=>Number.isNaN(parseFloat(t))?e:G(parseFloat(t),a),Yt=(t,e)=>wt({gameId:t.id_game,groupId:t.id_group,playerId:t.id_player,sportId:t.id_sport,subSportId:t.subSportId,typeId:t.type,firstOpponentName:t.opp1,secondOpponentName:t.opp2,opponents:t.opp,coef:zt(t.coef),coefByView:qt(t.coef,t.coef_text,e),param:t.dopv,kind:t.direction,name:t.bet_name,champName:t.champ_name,sportName:t.sport_name,groupName:t.group_name,gameName:t.period,gameType:t.gameType,champId:t.champId}),$t=(t,e,a)=>{var o,r,l;return new It({additionalType:t.GameVid,blockId:(o=t.level)!=null?o:a+1,statusId:(r=t.event_status)!=null?r:-2,type:t.type,scoreFormatted:(l=t.score)!=null?l:void 0,market:Yt(t,e),sportNameEng:t.sportNameEn})},Kt=(t,e)=>{const a=t.bets.map((i,c)=>$t(i,e,c)),o=t.bets.map(i=>({id:i.level,coef:i.level_coef,amount:i.level_summa})),l=At(o.map(({id:i})=>i).filter(i=>typeof i=="number")).map(i=>$(Y({},o.find(c=>c.id===i)),{bets:a.filter(c=>c.blockId===i)}));return{bets:a,blocks:l}},Qt=(t,e,a)=>{var A;const{bets:o,blocks:r}=Kt(e,a),{typeId:l,systemValue:i,systemOffset:c}=yt(_t.includes(e.info.vid)?e.info.system_type:e.info.vid,o.length),u=e.info.amount?parseFloat(e.info.amount):void 0,f=a===0&&parseFloat(e.info.coef_res)||0,w=(A=e.info.possibleWinSum)!=null?A:f!==void 0?Q(St(u,f)):0;return new Ut({id:parseInt(t,10),amount:u,initialAmount:e.info.amount?Q(parseFloat(e.info.amount)+e.info.sum_cut):void 0,bets:o,blocks:r,coef:f,coefByView:e.info.coef_res,createdDateTimeFormatted:e.info.date,gameId:e.info.id_game,insurancePercent:e.info.insurance_percent,isAllowEdit:e.info.allow_edit,isAllowInsurance:e.info.allow_insurance,isAllowPrint:!0,isAllowRepeat:e.info.allow_repeat,isAllowSale:e.info.allow_sale,isPowerBet:e.info.isPowerBet,sportId:e.info.id_sport,statusId:e.info.status,systemOffset:c,systemValue:i,tax:e.info.sum_cut,typeId:l,currencyIso:e.info.currencyIso,possibleWinAmount:w,maxPayout:e.info.maxPayout})};class Jt extends vt{async getCoupons({coefViewId:e}){const a=await this.getRequest("/datalinelive/lastcoupons?isFinance=0&isEdit=1");if(a.data==="empty_fields")return{data:{coupons:[]},success:!0};if(!a.success)return this.$logger.error("getCoupons error",a),{data:void 0,success:!1};const{currency:o}=a.data;return{data:{coupons:Object.entries(a.data.coupons).map(([l,i])=>(i.info.currencyIso=o,Qt(l,i,e)))},success:!0}}}const se=Ot(()=>{const t=Pt(),e=gt(),a=Tt(),{notificationError:o,notificationInfo:r,notificationSuccess:l,notificationQuestion:i}=xt(),c=J(Jt),u=J(Ft),f=E(()=>t.activeAccount),w=E(()=>t.activeId),A=E(()=>e.getCoefViewId),d=F([]),_=F(),g=F(!0),x=F(!0),h=E(()=>m(g)||m(x)),b=F([]),B=E(()=>!!m(w)&&m(d).length>0),p=async()=>{if(!m(w))throw new Error(a("main_authorize"));const{data:s,success:I}=await c.getCoupons({coefViewId:m(A)});if(!I)throw new Error(a("main_error"));return s.coupons},O=async()=>{if(!m(w))throw new Error(a("main_authorize"));const{data:s,success:I}=await u.getCouponSaleInfos({accountId:m(w)});if(!I)throw new Error(a("main_error"));return s},y=async()=>{d.value=await p(),g.value=!1},v=async()=>{const s=await O();_.value=s==null?void 0:s.couponSaleInfos,b.value=s==null?void 0:s.couponGamesInfo},V=()=>Promise.all([y(),v()]),{forceUpdateData:L,timerPause:C,timerStart:M,timerDestroy:W}=Ct({interval:1e3*5,onUpdateData:()=>v(),onCancelIntervalRequest:()=>Promise.resolve(),startTimerAfterComeBack:B}),N=async()=>m(w)?y().then(()=>m(d).length>0?M():C()).catch(()=>C()):C();H([d,_],()=>{m(d).forEach(s=>{var I;s.saleInfo=(I=m(_))==null?void 0:I.find(({couponId:S})=>S===s.id)})}),H(d,async s=>{s.length>0&&await L(),x.value=!1}),H([w,A],()=>N());const D=s=>{var I;const S=m(d).findIndex(P=>P.id===s.couponId);S!==-1&&((I=m(_))==null||I.splice(S,1,s))},R=s=>{if(s.isOrder){v();return}const I=m(d).find(({id:tt})=>tt===s.couponSaleInfo.couponId),S=m(d).indexOf(I),P=s.couponSaleInfo.availableBetAmount,T=Nt(I.saleAmount+s.amount);I.sell(P,T),!P&&S!==-1?m(d).splice(S,1):v(),t.setNewBalance({account_id:s.accountId,new_balance:s.accountMoney}),t.updateData()},Z=({couponId:s,couponInsurancePercent:I})=>{const S=m(d).find(({id:P})=>P===s);S&&S.insure(I)},k=async({coupon:s,newCouponAmount:I,amount:S=s.saleInfo.maxSaleAmount,isSubstituteAlertWithNotification:P=!1})=>{if(!w)throw a("main_authorize");s.isCanSale&&await U({T:a},{couponSaleApi:u,activeAccountId:m(w),account:m(f),amount:S,couponId:s.id,newCouponAmount:I,onUpdateCouponSaleInfo:D,onSaleCoupon:R,successAlertSubstitutionCallback:P?T=>l(T):void 0,infoAlertSubstitutionCallback:P?T=>r(T):void 0,questionAlertSubstitutionCallback:P?T=>i(T):void 0,errorAlertSubstitutionCallback:P?T=>o(T):void 0})};return bt(()=>N()),Bt(()=>W()),{coupons:d,saleInfos:_,gamesInfo:b,isLoadingCoupons:g,isLoadingSaleInfos:x,isLoading:h,updateCouponsWithSaleInfos:V,updateCoupons:y,updateSaleInfos:v,onUpdateCouponSaleInfo:D,onSaleCoupon:R,onInsure:Z,saleCoupon:k}});export{Ut as OpenedCoupon,U as saleCoupon,se as useOpenedCouponsStore};