var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var errors_exports = {};
__export(errors_exports, {
  handleApiError: () => handleApiError,
  parseUnsupportedError: () => parseUnsupportedError
});
module.exports = __toCommonJS(errors_exports);
var import_api = require("@playdrop/types/api");
function parseUnsupportedError(status, payload) {
  if (status !== 426 && status !== 400) {
    return null;
  }
  const data = payload && typeof payload === "object" ? payload : null;
  const code = typeof data?.["error"] === "string" ? data["error"] : void 0;
  const unsupportedCodes = /* @__PURE__ */ new Set([
    "unsupported_client",
    "unsupported_client_version",
    "unsupported_client_build",
    "unsupported_platform_version",
    "missing_client_metadata"
  ]);
  const isGuardResponse = status === 426 || status === 400 && code !== void 0 && unsupportedCodes.has(code);
  if (!isGuardResponse) {
    return null;
  }
  const message = typeof data?.["message"] === "string" ? data["message"] : "Unsupported client version";
  return new import_api.UnsupportedClientError(code ?? "unsupported_client", message, data ?? null);
}
function handleApiError(response, context) {
  const unsupported = parseUnsupportedError(response.status, response.body);
  if (unsupported) {
    throw unsupported;
  }
  const payload = response.body && typeof response.body === "object" ? response.body : {};
  const details = response.body && typeof response.body === "object" ? response.body : null;
  const code = typeof payload["error"] === "string" ? payload["error"] : context;
  let message;
  if (typeof payload["message"] === "string") {
    message = payload["message"];
  } else if (typeof payload["error"] === "string") {
    message = payload["error"];
    if (typeof payload["errorPath"] === "string") {
      message = `${payload["error"]}: ${payload["errorPath"]}`;
    }
    if (Array.isArray(payload["missing"]) && payload["missing"].length > 0) {
      message = `${payload["error"]}: missing exports [${payload["missing"].join(", ")}]`;
    }
  } else {
    message = `${context} failed`;
  }
  throw new import_api.ApiError(message, response.status, code, details);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  handleApiError,
  parseUnsupportedError
});
