import { d as defineEventHandler, c as createError, j as getQuery, eI as isPreviewToken, eJ as verifyPreviewToken, G as createContentManager } from '../../../../nitro/nitro.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';

const _id__get = defineEventHandler(async (event) => {
  var _a, _b, _c, _d, _e;
  try {
    const contentType = (_a = event.context.params) == null ? void 0 : _a.contentType;
    const id = (_b = event.context.params) == null ? void 0 : _b.id;
    if (!contentType || !id) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "Content type and ID are required"
      });
    }
    const numericId = parseInt(id);
    if (isNaN(numericId)) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "ID must be a valid number"
      });
    }
    const query = getQuery(event);
    const token = query.token;
    if (!token || !isPreviewToken(token)) {
      throw createError({
        statusCode: 401,
        statusMessage: "Unauthorized",
        message: "Valid preview token is required. Pass ?token=pv_..."
      });
    }
    const payload = await verifyPreviewToken(token, contentType, numericId);
    if (!payload) {
      throw createError({
        statusCode: 401,
        statusMessage: "Unauthorized",
        message: "Preview token is invalid, expired, or does not match this content"
      });
    }
    const contentManager = createContentManager(contentType);
    const locale = (_c = payload.locale) != null ? _c : query.locale;
    let result = await contentManager.findOne(numericId, { locale });
    if (!result) {
      throw createError({
        statusCode: 404,
        statusMessage: "Not Found",
        message: `${contentType} with id ${id} not found`
      });
    }
    if (query.populate) {
      const populateFields = query.populate.split(",").map((f) => f.trim());
      const populated = await contentManager.populateRecords(result, populateFields);
      result = Array.isArray(populated) ? populated[0] : populated;
    }
    return {
      success: true,
      preview: true,
      data: result
    };
  } catch (error) {
    if (error && typeof error === "object" && "statusCode" in error) {
      throw error;
    }
    if (error instanceof Error && error.message.includes("not found")) {
      throw createError({
        statusCode: 404,
        statusMessage: "Not Found",
        message: error.message
      });
    }
    console.error(
      `[GET /api/preview/${(_d = event.context.params) == null ? void 0 : _d.contentType}/${(_e = event.context.params) == null ? void 0 : _e.id}] Error:`,
      error
    );
    throw createError({
      statusCode: 500,
      statusMessage: "Failed to fetch preview content",
      message: error instanceof Error ? error.message : "Unknown error"
    });
  }
});

export { _id__get as default };
//# sourceMappingURL=_id_.get.mjs.map
