import { f as defineAdminEventHandler, h as requireAdminRole, i as requireApiTokenScopes, c as createError, q as readBody, bx as addReleaseItem } 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 VALID_ACTIONS = /* @__PURE__ */ new Set(["publish", "unpublish"]);
const index_post = defineAdminEventHandler("admin:releases:add-item", async (event) => {
  var _a;
  try {
    await requireAdminRole(event, ["super-admin", "admin"]);
    requireApiTokenScopes(event.context.apiToken, { scopes: ["releases:write"] });
    const releaseId = Number((_a = event.context.params) == null ? void 0 : _a.id);
    if (!releaseId || isNaN(releaseId)) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "Valid release ID is required"
      });
    }
    const body = await readBody(event);
    if (!(body == null ? void 0 : body.contentType) || typeof body.contentType !== "string") {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "contentType is required"
      });
    }
    const entryId = Number(body == null ? void 0 : body.entryId);
    if (!entryId || isNaN(entryId)) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "entryId must be a valid number"
      });
    }
    const action = String((body == null ? void 0 : body.action) || "publish");
    if (!VALID_ACTIONS.has(action)) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: `action must be one of: ${[...VALID_ACTIONS].join(", ")}`
      });
    }
    const item = await addReleaseItem(releaseId, {
      contentType: body.contentType,
      entryId,
      action
    });
    return {
      success: true,
      data: item
    };
  } 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 });
    }
    if (error instanceof Error && (error.message.includes("Cannot add") || error.message.includes("already in"))) {
      throw createError({ statusCode: 400, statusMessage: "Bad Request", message: error.message });
    }
    console.error("[POST /api/admin/content-releases/:id/items] Error:", error);
    throw createError({
      statusCode: 500,
      statusMessage: "Failed to add release item",
      message: error instanceof Error ? error.message : "Unknown error"
    });
  }
});

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