import{B as u,bz as y,ac as w,bd as b,aj as k,bA as L,aa as O,Q as R,l as E,bB as m,aI as x}from"./page-BLXZLgnU.js";import{i as M}from"./isAddressEqual-P8HBf8Ji.js";import"./api-client-DYa9W_bB.js";import"./api-CJCsP-f4.js";import"./notifications-B4lW5TKj.js";class S extends u{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 u{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 u{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 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(d,{blockNumber:s,blockTag:e,data:o,to:n}){const{args:c}=b({data:o,abi:[T]}),[t,i,l,a,r]=c,{ccipRead:f}=d,h=f&&typeof f?.request=="function"?f.request:A;try{if(!M(n,t))throw new q({sender:t,to:n});const p=i.includes(k)?await L({data:l,ccipRequest:h}):await h({data:l,sender:t,urls:i}),{data:g}=await O(d,{blockNumber:s,blockTag:e,data:R([a,E([{type:"bytes"},{type:"bytes"}],[p,r])]),to:n});return g}catch(p){throw new S({callbackSelector:a,cause:p,data:o,extraData:r,sender:t,urls:i})}}async function A({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,l=t==="POST"?{"Content-Type":"application/json"}:{};try{const a=await fetch(c.replace("{sender}",s.toLowerCase()).replace("{data}",d),{body:JSON.stringify(i),headers:l,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(!x(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{A as ccipRequest,U as offchainLookup,T as offchainLookupAbiItem,P as offchainLookupSignature};
