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