import { dK as defineContentEventHandler, c as createError, dL as optionalAuth, bT as requirePermission, bU as isWorkflowEnabled, bW as performTransition, G as createContentManager, ed as emitBeforePublish, ee as triggerWebhook, ef as emitAfterPublish, el as emitBeforeUnpublish, em as emitAfterUnpublish } from '../../../../../../nitro/nitro.mjs';
import { m as mapContentApiError } from '../../../../../../_/api-helpers.mjs';
import { g as getAuditActorFromEvent } from '../../../../../../_/audit-log-actor.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 transition_put = defineContentEventHandler("update", async (event, input) => {
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
  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"
      });
    }
    await optionalAuth(event);
    await requirePermission(
      event.context.user,
      contentType,
      "workflow",
      event.context.adminUser,
      event.context.apiToken
    );
    if (!isWorkflowEnabled(contentType)) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: `Workflow is not enabled for content type '${contentType}'`
      });
    }
    const numericId = parseInt(id);
    if (isNaN(numericId)) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "ID must be a valid number"
      });
    }
    const body = input;
    if (!(body == null ? void 0 : body.to)) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: 'Target state "to" is required'
      });
    }
    const adminUser = event.context.adminUser;
    const userId = (_d = adminUser == null ? void 0 : adminUser.id) != null ? _d : (_c = event.context.user) == null ? void 0 : _c.id;
    if (!userId) {
      throw createError({
        statusCode: 401,
        statusMessage: "Unauthorized",
        message: "Authentication required for workflow transitions"
      });
    }
    const userRole = (adminUser == null ? void 0 : adminUser.role) || ((_e = event.context.user) == null ? void 0 : _e.role);
    const result = await performTransition(
      contentType,
      numericId,
      body.to,
      userId,
      userRole,
      body.comment
    );
    if (!result.success) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: result.error || "Transition failed"
      });
    }
    const actor = getAuditActorFromEvent(event);
    const contentManager = createContentManager(contentType);
    if (result.publishAction === "publish") {
      await emitBeforePublish(contentType, numericId, null);
      await contentManager.update(
        numericId,
        { published_at: (/* @__PURE__ */ new Date()).toISOString() },
        "publish",
        { actor, adminUserId: (_g = (_f = event.context.adminUser) == null ? void 0 : _f.id) != null ? _g : null }
      );
      const entry2 = await contentManager.findOne(numericId);
      triggerWebhook("entry.publish", contentType, entry2);
      triggerWebhook("entry.preview.invalidate", contentType, entry2);
      await emitAfterPublish(contentType, numericId, entry2, actor);
    } else if (result.publishAction === "unpublish") {
      const entry2 = await contentManager.findOne(numericId);
      await emitBeforeUnpublish(contentType, numericId, entry2);
      await contentManager.update(numericId, { published_at: null }, "unpublish", {
        actor,
        adminUserId: (_i = (_h = event.context.adminUser) == null ? void 0 : _h.id) != null ? _i : null
      });
      const updated = await contentManager.findOne(numericId);
      triggerWebhook("entry.unpublish", contentType, updated);
      triggerWebhook("entry.preview.invalidate", contentType, updated);
      await emitAfterUnpublish(contentType, numericId, updated, actor);
    }
    const entry = await contentManager.findOne(numericId);
    return {
      success: true,
      message: `Transitioned to '${body.to}'`,
      data: entry
    };
  } catch (error) {
    mapContentApiError(error, {
      routeLabel: `PUT /api/content/${(_j = event.context.params) == null ? void 0 : _j.contentType}/${(_k = event.context.params) == null ? void 0 : _k.id}/workflow/transition`
    });
  }
});

export { transition_put as default };
//# sourceMappingURL=transition.put.mjs.map
