"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("./evm-C_kkwF2N.js"),n=require("./index-C_DPPfkk.js");class k extends n.BaseError{constructor({callbackSelector:s,cause:t,data:c,extraData:u,sender:d,urls:a}){var o;super(t.shortMessage||"An error occurred while fetching for an offchain result.",{cause:t,metaMessages:[...t.metaMessages||[],(o=t.metaMessages)!=null&&o.length?"":[],"Offchain Gateway Call:",a&&["  Gateway URL(s):",...a.map(f=>`    ${n.getUrl(f)}`)],`  Sender: ${d}`,`  Data: ${c}`,`  Callback selector: ${s}`,`  Extra data: ${u}`].flat(),name:"OffchainLookupError"})}}class b extends n.BaseError{constructor({result:s,url:t}){super("Offchain gateway response is malformed. Response data must be a hex value.",{metaMessages:[`Gateway URL: ${n.getUrl(t)}`,`Response: ${n.stringify(s)}`],name:"OffchainLookupResponseMalformedError"})}}class L extends n.BaseError{constructor({sender:s,to:t}){super("Reverted sender address does not match target contract address (`to`).",{metaMessages:[`Contract address: ${t}`,`OffchainLookup sender address: ${s}`],name:"OffchainLookupSenderMismatchError"})}}const E="0x556f1830",m={name:"OffchainLookup",type:"error",inputs:[{name:"sender",type:"address"},{name:"urls",type:"string[]"},{name:"callData",type:"bytes"},{name:"callbackFunction",type:"bytes4"},{name:"extraData",type:"bytes"}]};async function O(i,{blockNumber:s,blockTag:t,data:c,to:u}){const{args:d}=l.decodeErrorResult({data:c,abi:[m]}),[a,o,f,p,r]=d,{ccipRead:e}=i,y=e&&typeof(e==null?void 0:e.request)=="function"?e.request:g;try{if(!l.isAddressEqual(u,a))throw new L({sender:a,to:u});const h=o.includes(l.localBatchGatewayUrl)?await l.localBatchGatewayRequest({data:f,ccipRequest:y}):await y({data:f,sender:a,urls:o}),{data:w}=await l.call(i,{blockNumber:s,blockTag:t,data:l.concat([p,l.encodeAbiParameters([{type:"bytes"},{type:"bytes"}],[h,r])]),to:u});return w}catch(h){throw new k({callbackSelector:p,cause:h,data:c,extraData:r,sender:a,urls:o})}}async function g({data:i,sender:s,urls:t}){var u;let c=new Error("An unknown error occurred.");for(let d=0;d<t.length;d++){const a=t[d],o=a.includes("{data}")?"GET":"POST",f=o==="POST"?{data:i,sender:s}:void 0,p=o==="POST"?{"Content-Type":"application/json"}:{};try{const r=await fetch(a.replace("{sender}",s.toLowerCase()).replace("{data}",i),{body:JSON.stringify(f),headers:p,method:o});let e;if((u=r.headers.get("Content-Type"))!=null&&u.startsWith("application/json")?e=(await r.json()).data:e=await r.text(),!r.ok){c=new n.HttpRequestError({body:f,details:e!=null&&e.error?n.stringify(e.error):r.statusText,headers:r.headers,status:r.status,url:a});continue}if(!n.isHex(e)){c=new b({result:e,url:a});continue}return e}catch(r){c=new n.HttpRequestError({body:f,details:r.message,url:a})}}throw c}exports.ccipRequest=g;exports.offchainLookup=O;exports.offchainLookupAbiItem=m;exports.offchainLookupSignature=E;
