import { c as createError, dO as getGuardrailWarningHeaders, dP as getQueryCostHeaders, s as setResponseHeader } from '../nitro/nitro.mjs';
import { r as recordDeliveryQuery } from './delivery-metrics.mjs';

function parseQueryFilters(query) {
  const filters = {};
  for (const [key, value] of Object.entries(query)) {
    const match = key.match(/^filters\[([^\]]+)\](?:\[([^\]]+)\])?(?:\[([^\]]+)\])?$/);
    if (!match) {
      continue;
    }
    const field1 = match[1];
    const field2 = match[2];
    const field3 = match[3];
    if (field3) {
      if (!filters[field1] || typeof filters[field1] !== "object") {
        filters[field1] = {};
      }
      const field1Obj = filters[field1];
      if (!field1Obj[field2] || typeof field1Obj[field2] !== "object") {
        field1Obj[field2] = {};
      }
      field1Obj[field2][field3] = value;
    } else if (field2) {
      if (!filters[field1] || typeof filters[field1] !== "object") {
        filters[field1] = {};
      }
      filters[field1][field2] = value;
    } else {
      filters[field1] = value;
    }
  }
  return filters;
}
function parsePopulateParam(query) {
  const raw = query.populate;
  if (!raw) {
    return null;
  }
  if (typeof raw === "string") {
    return raw.split(",").map((f) => f.trim()).filter(Boolean);
  }
  if (Array.isArray(raw)) {
    return raw.map(String).filter(Boolean);
  }
  return [String(raw)];
}
function mapContentApiError(error, ctx) {
  if (error && typeof error === "object" && "statusCode" in error) {
    throw error;
  }
  if (isErrorWithMessage(error, "Authentication required")) {
    throw createError({
      statusCode: 401,
      statusMessage: "Unauthorized",
      message: error.message
    });
  }
  if (isErrorWithMessage(error, "Permission denied")) {
    throw createError({
      statusCode: 403,
      statusMessage: "Forbidden",
      message: error.message
    });
  }
  if (error instanceof Error && error.message.includes("Schema") && error.message.includes("not found")) {
    throw createError({
      statusCode: 404,
      statusMessage: "Not Found",
      message: error.message
    });
  }
  if (isErrorWithMessage(error, "not found")) {
    throw createError({
      statusCode: 404,
      statusMessage: "Not Found",
      message: error.message
    });
  }
  if (error && typeof error === "object" && "validationErrors" in error) {
    const validationErrors = error.validationErrors;
    throw createError({
      statusCode: 400,
      statusMessage: "Validation Error",
      message: "Validation failed",
      data: { validationErrors }
    });
  }
  if (error instanceof Error && error.message === "Validation failed") {
    throw createError({
      statusCode: 400,
      statusMessage: "Validation Error",
      message: error.message
    });
  }
  console.error(`[${ctx.routeLabel}] Error:`, error);
  throw createError({
    statusCode: 500,
    statusMessage: "Internal Server Error",
    message: error instanceof Error ? error.message : "Unknown error"
  });
}
function isErrorWithMessage(error, substring) {
  return error instanceof Error && error.message.includes(substring);
}
function setQueryCostResponseHeaders(event, cost, warnings) {
  var _a, _b, _c, _d;
  const costHeaders = getQueryCostHeaders(cost);
  const warningHeaders = getGuardrailWarningHeaders(warnings);
  for (const [key, value] of Object.entries({ ...costHeaders, ...warningHeaders })) {
    setResponseHeader(event, key, value);
  }
  try {
    const path = (_b = (_a = event.path) != null ? _a : event.node.req.url) != null ? _b : "unknown";
    const method = (_d = (_c = event.method) != null ? _c : event.node.req.method) != null ? _d : "GET";
    recordDeliveryQuery({
      route: typeof path === "string" ? path : "unknown",
      method: typeof method === "string" ? method : "GET",
      cost,
      warnings
    });
  } catch {
  }
}

export { parseQueryFilters as a, mapContentApiError as m, parsePopulateParam as p, setQueryCostResponseHeaders as s };
//# sourceMappingURL=api-helpers.mjs.map
