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