import{B as u,cY as p,fP as k,c0 as x,de as M,dl as S,dk as $,bK as A,bR as T,cd as C,cJ as w,dc as E,a0 as b,dg as G}from"./index-DE_m8aQ1.js";class D extends u{constructor({callbackSelector:a,cause:t,data:f,extraData:e,sender:d,urls:i}){var o;super(t.shortMessage||"An error occurred while fetching for an offchain result.",{cause:t,metaMessages:[...t.metaMessages||[],(o=t.metaMessages)!=null&&o.length?"":[],"Offchain Gateway Call:",i&&["  Gateway URL(s):",...i.map(n=>`    ${p(n)}`)],`  Sender: ${d}`,`  Data: ${f}`,`  Callback selector: ${a}`,`  Extra data: ${e}`].flat(),name:"OffchainLookupError"})}}class P extends u{constructor({result:a,url:t}){super("Offchain gateway response is malformed. Response data must be a hex value.",{metaMessages:[`Gateway URL: ${p(t)}`,`Response: ${k(a)}`],name:"OffchainLookupResponseMalformedError"})}}class B extends u{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 H="0x556f1830",U={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 J(c,{blockNumber:a,blockTag:t,data:f,requestOptions:e,to:d}){var s;const{args:i}=x({data:f,abi:[U]}),[o,n,l,g,y]=i,{ccipRead:h}=c,r=h&&typeof(h==null?void 0:h.request)=="function"?h.request:j;try{if(!M(d,o))throw new B({sender:o,to:d});const m=n.includes(S)?await $({data:l,ccipRequest:R=>r({...R,requestOptions:e})}):await r({data:l,requestOptions:e,sender:o,urls:n}),{data:L}=await A(c,{blockNumber:a,blockTag:t,data:T([g,C([{type:"bytes"},{type:"bytes"}],[m,y])]),requestOptions:e,to:d});return L}catch(m){throw(s=e==null?void 0:e.signal)!=null&&s.aborted?w(e.signal):E(m)?m:new D({callbackSelector:g,cause:m,data:f,extraData:y,sender:o,urls:n})}}async function j({data:c,requestOptions:a,sender:t,urls:f}){var d,i,o;let e=new Error("An unknown error occurred.");for(let n=0;n<f.length;n++){if((d=a==null?void 0:a.signal)!=null&&d.aborted)throw w(a.signal);const l=f[n],g=l.includes("{data}")?"GET":"POST",y=g==="POST"?{data:c,sender:t}:void 0,h=g==="POST"?{"Content-Type":"application/json"}:{};try{const r=await fetch(l.replace("{sender}",t.toLowerCase()).replace("{data}",c),{body:JSON.stringify(y),headers:h,method:g,...a!=null&&a.signal?{signal:a.signal}:{}});let s;if((i=r.headers.get("Content-Type"))!=null&&i.startsWith("application/json")?s=(await r.json()).data:s=await r.text(),!r.ok){e=new b({body:y,details:s!=null&&s.error?k(s.error):r.statusText,headers:r.headers,status:r.status,url:l});continue}if(!G(s)){e=new P({result:s,url:l});continue}return s}catch(r){if((o=a==null?void 0:a.signal)!=null&&o.aborted)throw w(a.signal);if(E(r))throw r;e=new b({body:y,details:r.message,url:l})}}throw e}export{j as ccipRequest,J as offchainLookup,U as offchainLookupAbiItem,H as offchainLookupSignature};
