import { f as defineAdminEventHandler, h as requireAdminRole, q as readBody, c as createError, G as createContentManager, ee as triggerWebhook } 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 revalidate_post = defineAdminEventHandler("admin:preview:revalidate", async (event) => {
  try {
    await requireAdminRole(event, ["super-admin", "admin", "editor"]);
    const body = await readBody(event);
    if (!body.contentType || typeof body.contentType !== "string") {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "contentType is required"
      });
    }
    if (body.entryId) {
      const numericId = Number(body.entryId);
      if (isNaN(numericId) || numericId <= 0) {
        throw createError({
          statusCode: 400,
          statusMessage: "Bad Request",
          message: "entryId must be a positive integer"
        });
      }
      const contentManager = createContentManager(body.contentType);
      const entry = await contentManager.findOne(numericId);
      if (!entry) {
        throw createError({
          statusCode: 404,
          statusMessage: "Not Found",
          message: `${body.contentType} with id ${numericId} not found`
        });
      }
      triggerWebhook("entry.preview.invalidate", body.contentType, entry);
      return {
        success: true,
        message: `Preview invalidation triggered for ${body.contentType}#${numericId}`
      };
    }
    triggerWebhook("entry.preview.invalidate", body.contentType, {
      _revalidateAll: true,
      contentType: body.contentType
    });
    return {
      success: true,
      message: `Preview invalidation triggered for all ${body.contentType} entries`
    };
  } 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("[POST /api/preview/revalidate] Error:", error);
    throw createError({
      statusCode: 500,
      statusMessage: "Failed to trigger preview invalidation",
      message: error instanceof Error ? error.message : "Unknown error"
    });
  }
});

export { revalidate_post as default };
//# sourceMappingURL=revalidate.post.mjs.map
