require(`../../_virtual/_rolldown/runtime.cjs`);const e=require(`../../types/ethereum-network.types.cjs`),t=require(`../../constants/chains.constants.cjs`),n=require(`../../constants/protocol.constants.cjs`),r=require(`../utils/caseInsensitive.utils.cjs`),i=require(`../../error-handling/logger.cjs`),a=require(`../utils/create-provider.cjs`),o=require(`../../data-structures/AccountActions/AccountActions.cjs`),s=require(`../utils/getContractAddress.cjs`);require(`../utils/index.cjs`);let c=require(`ethers`);var l=e=>{if(typeof e.target==`string`)return e.target;if(e.address)return e.address;throw Error(`Contract address not found`)},u=({contract:e,func:t,args:n,callDataString:r,invokeWallet:i,value:a})=>{let o=new Uint8Array,s=r?c.ethers.getBytesCopy(r):new Uint8Array;if(typeof e==`object`){if(o=c.ethers.getBytesCopy(l(e)),r?.length&&r?.length>0)throw Error(`If function and args are providedm no calldata is needed`);t&&n&&!r&&(s=c.ethers.getBytesCopy(e.interface.encodeFunctionData(t,n)))}else o=c.ethers.getBytesCopy(e);let u=c.ethers.getBytesCopy(new Uint8Array([+!!i])),d=c.ethers.zeroPadValue(c.ethers.toBeHex(a??0n),16),f=c.ethers.concat([o,u,d,s]);return c.ethers.hexlify(f)},d=async(e,r,i,a,s,l)=>{let u=t.getOnchainChainId(e),d=i?await o.AccountActions.signTypedDataFromPrivateKey(u,i,{name:`Emporium`,version:`1.0.0`,chainId:u,verifyingContract:r},{EmporiumSignature:[{name:`message`,type:`uint256`},{name:`ops`,type:`bytes[]`}]},{message:s,ops:a}):void 0,{v:f,r:p,s:m}=d?c.ethers.Signature.from(d):{v:0,r:c.ethers.ZeroHash,s:c.ethers.ZeroHash};return c.ethers.AbiCoder.defaultAbiCoder().encode([`tuple(uint8 v,bytes32 r, bytes32 s, uint256 message, address signerAddress, bytes[] ops)`],[{v:f,r:p,s:m,message:s??0n,signerAddress:l??n.zeroAddress,ops:a}])},f=e=>{let[t]=c.ethers.AbiCoder.defaultAbiCoder().decode([`tuple(uint8 v,bytes32 r, bytes32 s, uint256 message, address signerAddress, bytes[] ops)`],e),{v:n,r,s:i,message:a,signerAddress:o,ops:s}=t;return{v:n,r,s:i,message:a,signerAddress:o,ops:s}},p=(e,t,n,r)=>u(e?{contract:e,func:`transfer`,args:[t,n],invokeWallet:r}:{contract:t,value:n,invokeWallet:r}),m=(t,i,a,o,c)=>{let l=t.getContractWithFetcher(i,e.ContractType.HinkalContract);return a.map((a,u)=>p(r.caseInsensitiveEqual(a,n.zeroAddress)?void 0:t.getContractWithFetcher(i,e.ContractType.ERC20Contract,a),c??s.getContractAddress(l),o[u],!0))},h=(e,t,n,r)=>u({contract:e,func:`approve`,args:[t,n],invokeWallet:r}),g=(t,i,a,o,s,c,l,u)=>{let d=[];for(let s=0;s<a.length;s+=1){let c=a[s],u=r.caseInsensitiveEqual(c,n.zeroAddress)?void 0:t.getContractWithFetcher(i,e.ContractType.ERC20Contract,c);d.push(p(u,l,o[s],!1))}if(u)for(let a=0;a<s.length;a+=1){let o=s[a];if(!r.caseInsensitiveEqual(o,n.zeroAddress)){let n=t.getContractWithFetcher(i,e.ContractType.ERC20Contract,o);d.push(h(n,u,c[a],!0))}}return d},_=(e,t,i,a,o,s,c,l)=>{let d=[],f=[o],p=[s];l.nativeFee>0n&&!r.caseInsensitiveEqual(o,n.zeroAddress)&&(f.push(n.zeroAddress),p.push(l.nativeFee));let m=g(e,t,f,p,[o],[c],i,a);d.push(...m);let h=l.nativeFee;return r.caseInsensitiveEqual(o,n.zeroAddress)&&(h+=c),d.push(u({contract:a,callDataString:l.calldata,value:h>0n?h:void 0,invokeWallet:!0})),d},v=`0xef0100`,y=async(e,t)=>{try{let n=a.createJsonRpcProvider(e),r=await n.getCode(t);if(r===`0x`||!r)return{delegated:!1,implementation:void 0,nonce:void 0};if(r.toLowerCase().startsWith(v)&&r.length===48){let e=`0x${r.slice(8)}`,i=await n.getTransactionCount(t);return{delegated:!0,implementation:c.ethers.getAddress(e),nonce:i}}else throw Error(`checkEOAWithDelegation: It is a contract`)}catch(e){throw i.Logger.error(`checkEOAWithDelegation error`,{error:e}),e}},b=e=>{let{hinkalWalletAddress:i}=t.networkRegistry[e].contractData;if(!i||r.caseInsensitiveEqual(i,n.zeroAddress))throw Error(`Hinkal Wallet Address is not set`);return i},x=async(e,n)=>{if(e===t.chainIds.localhost||!n)return;let i=t.getOnchainChainId(e),{delegated:a,implementation:s}=await y(e,o.AccountActions.getSignerAddressFromPrivateKey(e,n)),c=b(e);if(!a||!r.caseInsensitiveEqual(s,c)){let e=await o.AccountActions.authorizeDelegation(i,n,c);return{v:e.signature.v.toString(),r:e.signature.r,s:e.signature.s,nonce:e.nonce.toString(),address:c,chainId:String(i)}}},S=e=>{if(!e)return;let{v:t,r:n,s:r,nonce:i,chainId:a,address:o}=e;return{signature:c.ethers.Signature.from({v:t,r:n,s:r}),nonce:BigInt(i),chainId:BigInt(a),address:o}};exports.checkEOAWithDelegation=y,exports.createApproveEmporiumOp=h,exports.createLifiBridgeOps=_,exports.createTransaferEmporiumOpsBatch=m,exports.createTransferToEmporiumOp=p,exports.decodeEmporiumMetadata=f,exports.emporiumOp=u,exports.encodeEmporiumMetadata=d,exports.generateFundAndApproveOps=g,exports.getAuthorizationDataIfNeeded=x,exports.recoverAuthorizationData=S;