import { i as b, t as F, j as O, A as C, k as I, l as M, m as E, n as D, o as w, q as G, u as A, v as T, I as v, w as m, x as H, y as R, z as k, C as y, D as N, E as P, F as j, G as U, H as B, J as L, K as J } from "./index-BgODs1bn.js";
function _(e) {
  const { abi: s, data: a } = e, r = b(a, 0, 4), t = s.find((o) => o.type === "function" && r === F(O(o)));
  if (!t)
    throw new C(r, {
      docsPath: "/docs/contract/decodeFunctionData"
    });
  return {
    functionName: t.name,
    args: "inputs" in t && t.inputs && t.inputs.length > 0 ? I(t.inputs, b(a, 4)) : void 0
  };
}
const p = "/docs/contract/encodeErrorResult";
function x(e) {
  const { abi: s, errorName: a, args: r } = e;
  let t = s[0];
  if (a) {
    const u = M({ abi: s, args: r, name: a });
    if (!u)
      throw new E(a, { docsPath: p });
    t = u;
  }
  if (t.type !== "error")
    throw new E(void 0, { docsPath: p });
  const o = O(t), n = F(o);
  let c = "0x";
  if (r && r.length > 0) {
    if (!t.inputs)
      throw new D(t.name, { docsPath: p });
    c = w(t.inputs, r);
  }
  return G([n, c]);
}
const h = "/docs/contract/encodeFunctionResult";
function z(e) {
  const { abi: s, functionName: a, result: r } = e;
  let t = s[0];
  if (a) {
    const n = M({ abi: s, name: a });
    if (!n)
      throw new A(a, { docsPath: h });
    t = n;
  }
  if (t.type !== "function")
    throw new A(void 0, { docsPath: h });
  if (!t.outputs)
    throw new T(t.name, { docsPath: h });
  const o = (() => {
    if (t.outputs.length === 0)
      return [];
    if (t.outputs.length === 1)
      return [r];
    if (Array.isArray(r))
      return r;
    throw new v(r);
  })();
  return w(t.outputs, o);
}
const q = "x-batch-gateway:true";
async function S(e) {
  const { data: s, ccipRequest: a } = e, { args: [r] } = _({ abi: m, data: s }), t = [], o = [];
  return await Promise.all(r.map(async (n, c) => {
    try {
      o[c] = n.urls.includes(q) ? await S({ data: n.data, ccipRequest: a }) : await a(n), t[c] = !1;
    } catch (u) {
      t[c] = !0, o[c] = K(u);
    }
  })), z({
    abi: m,
    functionName: "query",
    result: [t, o]
  });
}
function K(e) {
  return e.name === "HttpRequestError" && e.status ? x({
    abi: m,
    errorName: "HttpError",
    args: [e.status, e.shortMessage]
  }) : x({
    abi: [H],
    errorName: "Error",
    args: ["shortMessage" in e ? e.shortMessage : e.message]
  });
}
function W(e, s) {
  if (!R(e, { strict: !1 }))
    throw new k({ address: e });
  if (!R(s, { strict: !1 }))
    throw new k({ address: s });
  return e.toLowerCase() === s.toLowerCase();
}
class Q extends y {
  constructor({ callbackSelector: s, cause: a, data: r, extraData: t, sender: o, urls: n }) {
    var c;
    super(a.shortMessage || "An error occurred while fetching for an offchain result.", {
      cause: a,
      metaMessages: [
        ...a.metaMessages || [],
        (c = a.metaMessages) != null && c.length ? "" : [],
        "Offchain Gateway Call:",
        n && [
          "  Gateway URL(s):",
          ...n.map((u) => `    ${N(u)}`)
        ],
        `  Sender: ${o}`,
        `  Data: ${r}`,
        `  Callback selector: ${s}`,
        `  Extra data: ${t}`
      ].flat(),
      name: "OffchainLookupError"
    });
  }
}
class V extends y {
  constructor({ result: s, url: a }) {
    super("Offchain gateway response is malformed. Response data must be a hex value.", {
      metaMessages: [
        `Gateway URL: ${N(a)}`,
        `Response: ${P(s)}`
      ],
      name: "OffchainLookupResponseMalformedError"
    });
  }
}
class X extends y {
  constructor({ sender: s, to: a }) {
    super("Reverted sender address does not match target contract address (`to`).", {
      metaMessages: [
        `Contract address: ${a}`,
        `OffchainLookup sender address: ${s}`
      ],
      name: "OffchainLookupSenderMismatchError"
    });
  }
}
const et = "0x556f1830", Y = {
  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 at(e, { blockNumber: s, blockTag: a, data: r, to: t }) {
  const { args: o } = j({
    data: r,
    abi: [Y]
  }), [n, c, u, f, d] = o, { ccipRead: i } = e, g = i && typeof (i == null ? void 0 : i.request) == "function" ? i.request : Z;
  try {
    if (!W(t, n))
      throw new X({ sender: n, to: t });
    const l = c.includes(q) ? await S({
      data: u,
      ccipRequest: g
    }) : await g({ data: u, sender: n, urls: c }), { data: $ } = await U(e, {
      blockNumber: s,
      blockTag: a,
      data: B([
        f,
        w([{ type: "bytes" }, { type: "bytes" }], [l, d])
      ]),
      to: t
    });
    return $;
  } catch (l) {
    throw new Q({
      callbackSelector: f,
      cause: l,
      data: r,
      extraData: d,
      sender: n,
      urls: c
    });
  }
}
async function Z({ data: e, sender: s, urls: a }) {
  var t;
  let r = new Error("An unknown error occurred.");
  for (let o = 0; o < a.length; o++) {
    const n = a[o], c = n.includes("{data}") ? "GET" : "POST", u = c === "POST" ? { data: e, sender: s } : void 0, f = c === "POST" ? { "Content-Type": "application/json" } : {};
    try {
      const d = await fetch(n.replace("{sender}", s.toLowerCase()).replace("{data}", e), {
        body: JSON.stringify(u),
        headers: f,
        method: c
      });
      let i;
      if ((t = d.headers.get("Content-Type")) != null && t.startsWith("application/json") ? i = (await d.json()).data : i = await d.text(), !d.ok) {
        r = new L({
          body: u,
          details: i != null && i.error ? P(i.error) : d.statusText,
          headers: d.headers,
          status: d.status,
          url: n
        });
        continue;
      }
      if (!J(i)) {
        r = new V({
          result: i,
          url: n
        });
        continue;
      }
      return i;
    } catch (d) {
      r = new L({
        body: u,
        details: d.message,
        url: n
      });
    }
  }
  throw r;
}
export {
  Z as ccipRequest,
  at as offchainLookup,
  Y as offchainLookupAbiItem,
  et as offchainLookupSignature
};
//# sourceMappingURL=ccip-CY8I6sp6.js.map
