import { dK as defineContentEventHandler, c as createError, dL as optionalAuth, bT as requirePermission, G as createContentManager, ev as clampLimit, ew as clampOffset, ex as validateSortField, ey as validateSearchFields, et as validateFilterOperators, dQ as validatePopulateFields, dR as calculateQueryCost } from '../../../nitro/nitro.mjs';
import { a as parseQueryFilters, p as parsePopulateParam, s as setQueryCostResponseHeaders, m as mapContentApiError } from '../../../_/api-helpers.mjs';
import 'node:crypto';
import 'jsonwebtoken';
import 'node:fs';
import 'node:module';
import 'node:path';
import 'node:url';
import 'node:http';
import 'node:https';
import 'node:events';
import 'node:buffer';
import 'pino';
import 'drizzle-orm/better-sqlite3';
import 'drizzle-orm/node-postgres';
import 'drizzle-orm/mysql2';
import 'better-sqlite3';
import 'pg';
import 'mysql2/promise';
import 'drizzle-orm';
import 'fs';
import 'path';
import 'prom-client';
import 'bcryptjs';
import '@node-rs/argon2';
import 'node:async_hooks';
import 'crypto';
import 'node:dns';
import 'node:util';
import 'node:net';
import 'fs/promises';
import 'url';
import 'semver';
import '../../../_/delivery-metrics.mjs';

const index_get = defineContentEventHandler("list", async (event, input) => {
  var _a, _b, _c;
  try {
    const contentType = (_a = event.context.params) == null ? void 0 : _a.contentType;
    if (!contentType) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "Content type is required"
      });
    }
    await optionalAuth(event);
    await requirePermission(
      event.context.user,
      contentType,
      "read",
      event.context.adminUser,
      event.context.apiToken
    );
    const query = input || {};
    const contentManager = createContentManager(contentType);
    const schema = contentManager.getSchema();
    const guardrailWarnings = [];
    const limit = clampLimit(query.limit ? parseInt(String(query.limit)) : 25);
    const offset = clampOffset(query.offset ? parseInt(String(query.offset)) : 0);
    const sort = validateSortField(query.sort ? String(query.sort) : "id", schema);
    const sortOrder = query.sortOrder === "asc" ? "asc" : "desc";
    const published = query.published === "true" ? true : query.published === "false" ? false : void 0;
    const search = query.search ? String(query.search) : void 0;
    let searchFields;
    if (query.searchFields) {
      const rawSearchFields = typeof query.searchFields === "string" ? query.searchFields.split(",").map((f) => f.trim()) : query.searchFields;
      const searchValidation = validateSearchFields(rawSearchFields, schema);
      searchFields = searchValidation.valid.length > 0 ? searchValidation.valid : void 0;
      guardrailWarnings.push(...searchValidation.violations);
    }
    const locale = query.locale ? String(query.locale) : void 0;
    const filters = parseQueryFilters(query);
    const filterViolations = validateFilterOperators(filters);
    guardrailWarnings.push(...filterViolations);
    const result = await contentManager.findMany({
      limit,
      offset,
      sort,
      sortOrder,
      published,
      filters,
      search,
      searchFields,
      locale
    });
    let populateCount = 0;
    const rawPopulateFields = parsePopulateParam(query);
    if (rawPopulateFields && result.data && result.data.length > 0) {
      const populateValidation = validatePopulateFields(rawPopulateFields, schema);
      guardrailWarnings.push(...populateValidation.violations);
      if (populateValidation.valid.length > 0) {
        populateCount = populateValidation.valid.length;
        const populated = await contentManager.populateRecords(
          result.data,
          populateValidation.valid
        );
        result.data = Array.isArray(populated) ? populated : [populated];
      }
    }
    const relationJoins = Object.entries(filters).filter(([key]) => {
      const fieldDef = schema.attributes[key];
      return fieldDef && fieldDef.type === "relation" && typeof filters[key] === "object";
    }).length;
    const cost = calculateQueryCost({
      limit,
      hasFilters: Object.keys(filters).length > 0,
      filterConditions: Object.keys(filters).length,
      relationJoins,
      populateFields: populateCount,
      searchFields: (_b = searchFields == null ? void 0 : searchFields.length) != null ? _b : 0,
      hasAggregate: false,
      groupByFields: 0
    });
    setQueryCostResponseHeaders(event, cost, guardrailWarnings);
    return {
      success: true,
      ...result
    };
  } catch (error) {
    mapContentApiError(error, {
      routeLabel: `GET /api/content/${(_c = event.context.params) == null ? void 0 : _c.contentType}`
    });
  }
});

export { index_get as default };
//# sourceMappingURL=index.get.mjs.map
