import{p as e,g as t,C as n,E as s,a,S as r,b as o,f as c,c as i,d as u,O as p,N as h,e as d,B as l,h as w,s as y,i as g}from"./p-BxEPsDRj.js";const f=0;const m={paymentAsset:null,amount:null,tokenAmount:0,priceLoading:false,error:null,exchanges:[],isLoading:false,currentPayment:undefined,isPaymentInProgress:false,paymentId:"",assets:[]};const E=e(m);const x={state:E,subscribe(e){return g(E,(()=>e(E)))},subscribeKey(e,t){return y(E,e,t)},resetState(){Object.assign(E,{...m})},async getAssetsForNetwork(e){const t=w(e);const n=await x.getAssetsImageAndPrice(t);const s=t.map((e=>{const t=e.asset==="native"?d():`${e.network}:${e.asset}`;const s=n.find((e=>e.fungibles?.[0]?.address?.toLowerCase()===t.toLowerCase()));return{...e,price:s?.fungibles?.[0]?.price||1,metadata:{...e.metadata,iconUrl:s?.fungibles?.[0]?.iconUrl}}}));E.assets=s;return s},async getAssetsImageAndPrice(e){const t=e.map((e=>e.asset==="native"?d():`${e.network}:${e.asset}`));const n=await Promise.all(t.map((e=>l.fetchTokenPrice({addresses:[e]}))));return n},getTokenAmount(){if(!E?.paymentAsset?.price){throw new Error("Cannot get token price")}const e=h.bigNumber(E.amount??0).round(8);const t=h.bigNumber(E.paymentAsset.price).round(8);return e.div(t).round(8).toNumber()},setAmount(e){E.amount=e;if(E.paymentAsset?.price){E.tokenAmount=x.getTokenAmount()}},setPaymentAsset(e){E.paymentAsset=e},isPayWithExchangeEnabled(){return p.state.remoteFeatures?.payWithExchange},isPayWithExchangeSupported(){return x.isPayWithExchangeEnabled()&&n.state.activeCaipNetwork&&u.PAY_WITH_EXCHANGE_SUPPORTED_CHAIN_NAMESPACES.includes(n.state.activeCaipNetwork.chainNamespace)},async fetchExchanges(){try{const e=x.isPayWithExchangeSupported();if(!E.paymentAsset||!e){E.exchanges=[];E.isLoading=false;return}E.isLoading=true;const t=await i({page:f,asset:c(E.paymentAsset.network,E.paymentAsset.asset),amount:E.amount?.toString()??"0"});E.exchanges=t.exchanges.slice(0,2)}catch(e){r.showError("Unable to get exchanges");throw new Error("Unable to get exchanges")}finally{E.isLoading=false}},async getPayUrl(e,t){try{const n=Number(t.amount);const a=await o({exchangeId:e,asset:c(t.network,t.asset),amount:n.toString(),recipient:`${t.network}:${t.recipient}`});s.sendEvent({type:"track",event:"PAY_EXCHANGE_SELECTED",properties:{exchange:{id:e},configuration:{network:t.network,asset:t.asset,recipient:t.recipient,amount:n},currentPayment:{type:"exchange",exchangeId:e},source:"fund-from-exchange",headless:false}});return a}catch(e){if(e instanceof Error&&e.message.includes("is not supported")){throw new Error("Asset not supported")}throw new Error(e.message)}},async handlePayWithExchange(e){try{const t=n.getAccountData()?.address;if(!t){throw new Error("No account connected")}if(!E.paymentAsset){throw new Error("No payment asset selected")}const s=a.returnOpenHref("","popupWindow","scrollbar=yes,width=480,height=720");if(!s){throw new Error("Could not create popup window")}E.isPaymentInProgress=true;E.paymentId=crypto.randomUUID();E.currentPayment={type:"exchange",exchangeId:e};const{network:r,asset:o}=E.paymentAsset;const c={network:r,asset:o,amount:E.tokenAmount,recipient:t};const i=await x.getPayUrl(e,c);if(!i){try{s.close()}catch(e){console.error("Unable to close popup window",e)}throw new Error("Unable to initiate payment")}E.currentPayment.sessionId=i.sessionId;E.currentPayment.status="IN_PROGRESS";E.currentPayment.exchangeId=e;s.location.href=i.url}catch(e){E.error="Unable to initiate payment";r.showError(E.error)}},async waitUntilComplete({exchangeId:e,sessionId:t,paymentId:n,retries:s=20}){const a=await x.getBuyStatus(e,t,n);if(a.status==="SUCCESS"||a.status==="FAILED"){return a}if(s===0){throw new Error("Unable to get deposit status")}await new Promise((e=>{setTimeout(e,5e3)}));return x.waitUntilComplete({exchangeId:e,sessionId:t,paymentId:n,retries:s-1})},async getBuyStatus(e,r,o){try{if(!E.currentPayment){throw new Error("No current payment")}const c=await t({sessionId:r,exchangeId:e});E.currentPayment.status=c.status;if(c.status==="SUCCESS"||c.status==="FAILED"){const e=n.getAccountData()?.address;E.currentPayment.result=c.txHash;E.isPaymentInProgress=false;s.sendEvent({type:"track",event:c.status==="SUCCESS"?"PAY_SUCCESS":"PAY_ERROR",properties:{message:c.status==="FAILED"?a.parseError(E.error):undefined,source:"fund-from-exchange",paymentId:o,configuration:{network:E.paymentAsset?.network||"",asset:E.paymentAsset?.asset||"",recipient:e||"",amount:E.amount??0},currentPayment:{type:"exchange",exchangeId:E.currentPayment?.exchangeId,sessionId:E.currentPayment?.sessionId,result:c.txHash}}})}return c}catch(e){return{status:"UNKNOWN",txHash:""}}},reset(){E.currentPayment=undefined;E.isPaymentInProgress=false;E.paymentId="";E.paymentAsset=null;E.amount=0;E.tokenAmount=0;E.priceLoading=false;E.error=null;E.exchanges=[];E.isLoading=false}};export{x as E};
//# sourceMappingURL=p-BHv1WOIk.js.map