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