import { d1 as p, d2 as m, d3 as w, d4 as k, d5 as b, d6 as L, d7 as O, d8 as R, d9 as E, da as x, db as y, dc as M } from "./index-DjOvnyAu.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(d => `    ${m(d)}`)], `  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: ${m(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 A = "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 D(i, {
  blockNumber: s,
  blockTag: e,
  data: o,
  to: n
}) {
  const {
      args: c
    } = k({
      data: o,
      abi: [T]
    }),
    [t, d, l, a, r] = c,
    {
      ccipRead: f
    } = i,
    h = f && typeof f?.request == "function" ? f.request : C;
  try {
    if (!b(n, t)) throw new q({
      sender: t,
      to: n
    });
    const u = d.includes(L) ? await O({
        data: l,
        ccipRequest: h
      }) : await h({
        data: l,
        sender: t,
        urls: d
      }),
      {
        data: g
      } = await R(i, {
        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: d
    });
  }
}
async function C({
  data: i,
  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",
      d = t === "POST" ? {
        data: i,
        sender: s
      } : void 0,
      l = t === "POST" ? {
        "Content-Type": "application/json"
      } : {};
    try {
      const a = await fetch(c.replace("{sender}", s.toLowerCase()).replace("{data}", i), {
        body: JSON.stringify(d),
        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 y({
          body: d,
          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 y({
        body: d,
        details: a.message,
        url: c
      });
    }
  }
  throw o;
}
export { C as ccipRequest, D as offchainLookup, T as offchainLookupAbiItem, A as offchainLookupSignature };
//# sourceMappingURL=ccip-BXh_TvW9.js.map