import { _ as _$1, Xa, D, S as S$1, oa, $, c, b, a, C, s } from './chunk-LPJ7UHFQ.js';

var u=class extends b{constructor({callbackSelector:e,cause:r,data:n,extraData:i,sender:c,urls:o}){super(r.shortMessage||"An error occurred while fetching for an offchain result.",{cause:r,metaMessages:[...r.metaMessages||[],r.metaMessages?.length?"":[],"Offchain Gateway Call:",o&&["  Gateway URL(s):",...o.map(f=>`    ${a(f)}`)],`  Sender: ${c}`,`  Data: ${n}`,`  Callback selector: ${e}`,`  Extra data: ${i}`].flat()}),Object.defineProperty(this,"name",{enumerable:!0,configurable:!0,writable:!0,value:"OffchainLookupError"});}},m=class extends b{constructor({result:e,url:r}){super("Offchain gateway response is malformed. Response data must be a hex value.",{metaMessages:[`Gateway URL: ${a(r)}`,`Response: ${$(e)}`]}),Object.defineProperty(this,"name",{enumerable:!0,configurable:!0,writable:!0,value:"OffchainLookupResponseMalformedError"});}},l=class extends b{constructor({sender:e,to:r}){super("Reverted sender address does not match target contract address (`to`).",{metaMessages:[`Contract address: ${r}`,`OffchainLookup sender address: ${e}`]}),Object.defineProperty(this,"name",{enumerable:!0,configurable:!0,writable:!0,value:"OffchainLookupSenderMismatchError"});}};function A(t,e){if(!C(t))throw new s({address:t});if(!C(e))throw new s({address:e});return t.toLowerCase()===e.toLowerCase()}var _="0x556f1830",R={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 z(t,{blockNumber:e,blockTag:r,data:n,to:i}){let{args:c}=_$1({data:n,abi:[R]}),[o,f,a,s,x]=c;try{if(!A(i,o))throw new l({sender:o,to:i});let h=await S({data:a,sender:o,urls:f}),{data:M}=await Xa(t,{blockNumber:e,blockTag:r,data:D([s,S$1([{type:"bytes"},{type:"bytes"}],[h,x])]),to:i});return M}catch(h){throw new u({callbackSelector:s,cause:h,data:n,extraData:x,sender:o,urls:f})}}async function S({data:t,sender:e,urls:r}){let n=new Error("An unknown error occurred.");for(let i=0;i<r.length;i++){let c$1=r[i],o=c$1.includes("{data}")?"GET":"POST",f=o==="POST"?{data:t,sender:e}:void 0;try{let a=await fetch(c$1.replace("{sender}",e).replace("{data}",t),{body:JSON.stringify(f),method:o}),s;if(a.headers.get("Content-Type")?.startsWith("application/json")?s=(await a.json()).data:s=await a.text(),!a.ok){n=new oa({body:f,details:s?.error?$(s.error):a.statusText,headers:a.headers,status:a.status,url:c$1});continue}if(!c(s)){n=new m({result:s,url:c$1});continue}return s}catch(a){n=new oa({body:f,details:a.message,url:c$1});}}throw n}

export { _ as a, R as b, z as c, S as d };
