import { getApiVersion as v, getRequestObject as w, getResponseContentType as R, getResponseDownloadFilename as E, getErrorType as x, isAdyenErrorResponse as C, ErrorTypes as T, handleFetchError as b } from "./utils.js";
import { normalizeLoadingContext as $, normalizeUrl as H } from "../utils.js";
const l = (r, n) => {
  try {
    r?.(n);
  } catch {
    throw n;
  }
};
async function j(r) {
  const { errorLevel: n, loadingContext: d = "", path: u } = r, f = r.versionless || !1, h = v(r), p = w(r), m = $(d), g = f ? "" : h, s = new URL(`${m}${g}${H(u)}`);
  return s.pathname = s.pathname.replace(/\/+/g, "/"), r.params && r.params.forEach((o, t) => {
    const e = decodeURIComponent(o);
    e && s.searchParams.append(t, e);
  }), (async () => {
    let o = !1;
    const t = { level: n };
    try {
      const e = await fetch(s, p);
      if (e.ok) {
        if (e.status === 204)
          return null;
        try {
          switch (R(e)) {
            case "application/json":
              return await e.clone().text() ? await e.json() : null;
            default: {
              const i = await e.blob(), y = E(e);
              return { blob: i, filename: y };
            }
          }
        } catch (c) {
          throw o = !0, c;
        }
      }
      t.type = x(e.status);
      const a = await e.json();
      t.message = r.errorMessage || `Service at ${s} not available`, t.errorCode = a?.status == null ? void 0 : String(a.status), t.requestId = a?.requestId, C(a) && (t.message = a.detail, t.errorCode = a.errorCode, t.status = a.status, t.invalidFields = a.invalidFields), l(r.errorHandler, t);
    } catch (e) {
      if (o)
        throw l(r.errorHandler, e), e;
      t.type || (t.type = T.NETWORK_ERROR), l(r.errorHandler, e), t.message = r.errorMessage || `Call to ${s} failed. Error: ${e}`;
    }
    b(t);
  })();
}
function U(r) {
  return j({ ...r, method: "GET" });
}
export {
  j as http,
  U as httpGet
};
