import { E as ErrorCode } from "./errorCodes-E8kmN1h5.js";
class ErrorWithCode extends Error {
  constructor(code, message, data) {
    super(message);
    this.code = code;
    this.data = data;
  }
  static get Factory() {
    return new Proxy(ErrorWithCode, {
      get(_, prop) {
        if (prop in ErrorCode) {
          const code = ErrorCode[prop];
          return (message, data) => new ErrorWithCode(code, message, data);
        }
        throw new Error(`Unknown error code: ${prop}`);
      }
    });
  }
}
function getErrorFromUnknown(cause) {
  if (cause instanceof Error) {
    return cause;
  }
  if (typeof cause === "string") {
    return new Error(cause, { cause });
  }
  return new Error(`Unhandled error of type '${typeof cause}''`);
}
async function handleErrorsJson(response) {
  if (response.headers.get("content-encoding") === "gzip") {
    const responseText = await response.text();
    return new Promise((resolve) => resolve(JSON.parse(responseText)));
  }
  if (response.status === 204) {
    return new Promise((resolve) => resolve({}));
  }
  if (!response.ok && (response.status < 200 || response.status >= 300)) {
    response.json().then(console.log);
    throw Error(response.statusText);
  }
  return response.json();
}
function handleErrorsRaw(response) {
  if (response.status === 204) {
    console.error({ response });
    return "{}";
  }
  if (!response.ok || response.status < 200 || response.status >= 300) {
    response.text().then(console.log);
    throw Error(response.statusText);
  }
  return response.text();
}
export {
  ErrorWithCode as E,
  handleErrorsRaw as a,
  getErrorFromUnknown as g,
  handleErrorsJson as h
};
