import { f as defineAdminEventHandler, h as requireAdminRole, q as readBody, c as createError, cH as createPreviewToken } from '../../../nitro/nitro.mjs';
import { a as auditFromEvent } from '../../../_/audit-log-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 '../../../_/audit-log-actor.mjs';

const index_post = defineAdminEventHandler("admin:preview-tokens:create", async (event) => {
  var _a, _b, _c, _d, _e, _f;
  try {
    await requireAdminRole(event, ["super-admin", "admin", "editor"]);
    const body = (_a = await readBody(event)) != null ? _a : {};
    const normalizedContentType = ((_b = body.contentType) == null ? void 0 : _b.trim()) || null;
    if (body.contentType !== void 0 && body.contentType !== null) {
      if (typeof body.contentType !== "string" || normalizedContentType === null) {
        throw createError({
          statusCode: 400,
          statusMessage: "Bad Request",
          message: "contentType must be a non-empty string or null"
        });
      }
    }
    if (body.entryId !== void 0 && body.entryId !== null && normalizedContentType === null) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "contentType is required when entryId is provided"
      });
    }
    if (body.entryId !== void 0 && body.entryId !== null) {
      if (!Number.isInteger(body.entryId) || body.entryId <= 0) {
        throw createError({
          statusCode: 400,
          statusMessage: "Bad Request",
          message: "entryId must be a positive integer or null"
        });
      }
    }
    if (body.ttl !== void 0) {
      if (!Number.isInteger(body.ttl) || body.ttl < 60) {
        throw createError({
          statusCode: 400,
          statusMessage: "Bad Request",
          message: "ttl must be an integer >= 60 (seconds)"
        });
      }
    }
    const adminUser = event.context.adminUser;
    if (!adminUser) {
      throw createError({
        statusCode: 401,
        statusMessage: "Unauthorized",
        message: "Admin user required"
      });
    }
    const result = await createPreviewToken(adminUser.id, {
      contentType: normalizedContentType,
      entryId: (_c = body.entryId) != null ? _c : null,
      locale: (_e = (_d = body.locale) == null ? void 0 : _d.trim()) != null ? _e : null,
      ttl: body.ttl
    });
    auditFromEvent(event, "preview_token.create", {
      target: {
        entityType: "preview_token",
        entityId: result.token.id,
        displayName: (_f = result.token.contentType) != null ? _f : "all"
      },
      metadata: {
        contentType: result.token.contentType,
        entryId: result.token.entryId,
        locale: result.token.locale,
        expiresAt: result.token.expiresAt
      }
    });
    return {
      success: true,
      data: result.token,
      fullToken: result.fullToken
    };
  } catch (error) {
    if (error && typeof error === "object" && "statusCode" in error) {
      throw error;
    }
    console.error("[POST /api/admin/preview-tokens] Error:", error);
    throw createError({
      statusCode: 500,
      statusMessage: "Failed to create preview token",
      message: error instanceof Error ? error.message : "Unknown error"
    });
  }
});

export { index_post as default };
//# sourceMappingURL=index.post5.mjs.map
