require(`../../../../_virtual/_rolldown/runtime.cjs`);const e=require(`../../constants/protocol.constants.cjs`),t=require(`../../types/ethereum-network.types.cjs`),n=require(`../../types/external-action.types.cjs`),r=require(`../../constants/token-data/ERC20Registry.cjs`);require(`../../constants/token-data/index.cjs`);const i=require(`../utils/caseInsensitive.utils.cjs`),a=require(`../utils/getContractAddress.cjs`),o=require(`../../error-handling/logger.cjs`),s=require(`../utils/string.utils.cjs`),c=require(`../../constants/addresses.constants.cjs`),l=require(`./emporium.helpers.cjs`),u=require(`../pre-transaction/getExternalSwapAddress.cjs`);let d=require(`ethers`);var f=(e,t,n,r,i,a,o=!1)=>{let s={tokenIn:t,tokenOut:r,fee:BigInt(a),recipient:i,amountIn:n,amountOutMinimum:0,sqrtPriceLimitX96:0};return l.emporiumOp({contract:new d.ethers.Contract(e,[`function exactInputSingle(tuple(address tokenIn,address tokenOut,uint24 fee,address recipient,uint256 amountIn,uint256 amountOutMinimum,uint160 sqrtPriceLimitX96)) external payable returns (uint256 amountOut)`]),func:`exactInputSingle`,args:[s],invokeWallet:o})},p=(e,t,n,r,i,a,o=!1)=>l.emporiumOp({contract:n,callDataString:s.replaceAllInstances(r,u.getExternalSwapAddress(e,t).slice(2),i.slice(2)),value:a,invokeWallet:o}),m=(e,t)=>{let n=r.getERC20Registry(t).find(e=>e.symbol===`USDT`);return n?i.caseInsensitiveEqual(e,n.erc20TokenAddress):!1},h=async(e,t,n,r,i,s)=>{if(m(a.getContractAddress(n),e))try{await n.allowance(r,i)>0n&&t.push(l.createApproveEmporiumOp(n,i,0n,s))}catch(e){o.Logger.error(`Error in nullifyUsdtApprovalIfNeeded`,e)}},g=async(n,r,i,a,o,s,u,f)=>{if(i!==e.zeroAddress){let f=c.SWAP_FEE_TAKER_ADDRESSES[r],p=n.getContractWithFetcher(r,t.ContractType.ERC20Contract,i),m=await p.balanceOf(a);await h(r,s,p,a,f,u),s.push(l.createApproveEmporiumOp(p,f,2n**256n-1n,u));let g=new d.ethers.Contract(f,[`function takeSwapFee(address token, uint256 beforeBalance, uint256 variableRate, address relay) external payable`]),_=o;s.push(l.emporiumOp({contract:g,func:`takeSwapFee`,args:[i,m,e.HINKAL_SWAP_VARIABLE_RATE,_],invokeWallet:u}))}else{let t=o;s.push(l.emporiumOp({contract:t,value:f*e.HINKAL_SWAP_VARIABLE_RATE/10000n,invokeWallet:u}))}},_=async(r,i,a,o,s,u,m,_,v,y,b,x,S=!1)=>{let C=m<0n?-1n*m:m,w=[],T=c.NATIVE_TOKEN_WRAPPER_ADDRESSES[i],E=!1,D;if(a===n.ExternalActionId.OneInch||a===n.ExternalActionId.Lifi){if(u!==e.zeroAddress){let e=r.getContractWithFetcher(i,t.ContractType.ERC20Contract,u);w.push(l.createApproveEmporiumOp(e,o,C,S))}let c=0n;return u===e.zeroAddress&&(c+=C),_&&v===e.zeroAddress&&_<0n&&(c+=-1n*_),D=p(i,a,o,s,b,c>0n?c:void 0,S),w.push(D),(a===n.ExternalActionId.OneInch||y&&a===n.ExternalActionId.Lifi)&&v&&_!==void 0&&await g(r,i,v,b,x,w,S,_),w}if(!v||!_)throw Error(`There should be output token for UniSwap and Odos`);if(u===e.zeroAddress){let e=r.getContractWithFetcher(i,t.ContractType.ERC20Contract,T);w.push(l.emporiumOp({contract:e,func:`deposit`,value:C,invokeWallet:S})),u=T}let O=0n;if(v===e.zeroAddress&&(v=T,E=!0,O=await r.getContractWithFetcher(i,t.ContractType.ERC20Contract,T).balanceOf(b)),u!==e.zeroAddress){let e=r.getContractWithFetcher(i,t.ContractType.ERC20Contract,u);await h(i,w,e,b,o,S),w.push(l.createApproveEmporiumOp(e,o,C,S))}if(D=a===n.ExternalActionId.Uniswap?f(o,u,C,v,b,s,S):p(i,a,o,s,b,void 0,S),w.push(D),await g(r,i,v,b,x,w,S,_),E){let e=c.WRAPPER_TOKEN_EXCHANGE_ADDRESSES[i],n=r.getContractWithFetcher(i,t.ContractType.ERC20Contract,T);w.push(l.createApproveEmporiumOp(n,e,2n**256n-1n,S));let a=new d.ethers.Contract(e,[`function withdrawBalanceDifference(uint256 beforeBalance) external`]);w.push(l.emporiumOp({contract:a,func:`withdrawBalanceDifference`,args:[O],invokeWallet:S}))}return w};exports.createSwapToEmporiumOpForUniswap=f,exports.createSwapToEmporiumOps=_;