import{d as k,i as b,l as L,a as O,b as E,e as x,f as R}from"./evm-CaU1lKeB.js";import{B as u,ai as y,o as w,aj as m,i as M}from"./index-Czf_KRcW.js";import"../main.js";import"./provider-jsonrpc-DTnJKzTS.js";import"./contract-bGGxC9_m.js";class S extends u{constructor({callbackSelector:r,cause:a,data:n,extraData:i,sender:f,urls:t}){var o;super(a.shortMessage||"An error occurred while fetching for an offchain result.",{cause:a,metaMessages:[...a.metaMessages||[],(o=a.metaMessages)!=null&&o.length?"":[],"Offchain Gateway Call:",t&&["  Gateway URL(s):",...t.map(d=>`    ${y(d)}`)],`  Sender: ${f}`,`  Data: ${n}`,`  Callback selector: ${r}`,`  Extra data: ${i}`].flat(),name:"OffchainLookupError"})}}class $ extends u{constructor({result:r,url:a}){super("Offchain gateway response is malformed. Response data must be a hex value.",{metaMessages:[`Gateway URL: ${y(a)}`,`Response: ${w(r)}`],name:"OffchainLookupResponseMalformedError"})}}class q extends u{constructor({sender:r,to:a}){super("Reverted sender address does not match target contract address (`to`).",{metaMessages:[`Contract address: ${a}`,`OffchainLookup sender address: ${r}`],name:"OffchainLookupSenderMismatchError"})}}const P="0x556f1830",T={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 U(c,{blockNumber:r,blockTag:a,data:n,to:i}){const{args:f}=k({data:n,abi:[T]}),[t,o,d,l,s]=f,{ccipRead:e}=c,h=e&&typeof(e==null?void 0:e.request)=="function"?e.request:C;try{if(!b(i,t))throw new q({sender:t,to:i});const p=o.includes(L)?await O({data:d,ccipRequest:h}):await h({data:d,sender:t,urls:o}),{data:g}=await E(c,{blockNumber:r,blockTag:a,data:x([l,R([{type:"bytes"},{type:"bytes"}],[p,s])]),to:i});return g}catch(p){throw new S({callbackSelector:l,cause:p,data:n,extraData:s,sender:t,urls:o})}}async function C({data:c,sender:r,urls:a}){var i;let n=new Error("An unknown error occurred.");for(let f=0;f<a.length;f++){const t=a[f],o=t.includes("{data}")?"GET":"POST",d=o==="POST"?{data:c,sender:r}:void 0,l=o==="POST"?{"Content-Type":"application/json"}:{};try{const s=await fetch(t.replace("{sender}",r.toLowerCase()).replace("{data}",c),{body:JSON.stringify(d),headers:l,method:o});let e;if((i=s.headers.get("Content-Type"))!=null&&i.startsWith("application/json")?e=(await s.json()).data:e=await s.text(),!s.ok){n=new m({body:d,details:e!=null&&e.error?w(e.error):s.statusText,headers:s.headers,status:s.status,url:t});continue}if(!M(e)){n=new $({result:e,url:t});continue}return e}catch(s){n=new m({body:d,details:s.message,url:t})}}throw n}export{C as ccipRequest,U as offchainLookup,T as offchainLookupAbiItem,P as offchainLookupSignature};
