import { dK as defineContentEventHandler, c as createError, dL as optionalAuth, bT as requirePermission, G as createContentManager, bU as isWorkflowEnabled, bV as getWorkflowConfig, bW as performTransition, el as emitBeforeUnpublish, ee as triggerWebhook, 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 unpublish_put = defineContentEventHandler("unpublish", async (event) => {
  var _a, _b, _c, _d, _e, _f, _g, _h, _i;
  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,
      "publish",
      event.context.adminUser,
      event.context.apiToken
    );
    const numericId = parseInt(id);
    if (isNaN(numericId)) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "ID must be a valid number"
      });
    }
    const contentManager = createContentManager(contentType);
    if (isWorkflowEnabled(contentType)) {
      const config = getWorkflowConfig(contentType);
      if (!config) {
        throw createError({
          statusCode: 400,
          statusMessage: "Bad Request",
          message: "Workflow configuration error"
        });
      }
      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"
        });
      }
      const userRole = (adminUser == null ? void 0 : adminUser.role) || ((_e = event.context.user) == null ? void 0 : _e.role);
      const wfResult = await performTransition(
        contentType,
        numericId,
        config.initialState,
        userId,
        userRole
      );
      if (!wfResult.success) {
        throw createError({
          statusCode: 400,
          statusMessage: "Bad Request",
          message: wfResult.error || "Cannot unpublish via workflow"
        });
      }
      if (wfResult.pendingApproval) {
        const entry = await contentManager.findOne(numericId);
        return {
          success: true,
          approvalRequired: true,
          message: `${contentType} unpublish request submitted for approval`,
          data: entry
        };
      }
    }
    const actor = getAuditActorFromEvent(event);
    const currentRecord = await contentManager.findOne(numericId);
    if (currentRecord) {
      await emitBeforeUnpublish(contentType, numericId, currentRecord);
    }
    const result = await contentManager.update(numericId, { published_at: null }, "unpublish", {
      actor,
      adminUserId: (_g = (_f = event.context.adminUser) == null ? void 0 : _f.id) != null ? _g : null
    });
    triggerWebhook("entry.unpublish", contentType, result);
    triggerWebhook("entry.preview.invalidate", contentType, result);
    await emitAfterUnpublish(contentType, numericId, result, actor);
    return {
      success: true,
      message: `${contentType} unpublished successfully`,
      data: result
    };
  } catch (error) {
    mapContentApiError(error, {
      routeLabel: `PUT /api/content/${(_h = event.context.params) == null ? void 0 : _h.contentType}/${(_i = event.context.params) == null ? void 0 : _i.id}/unpublish`
    });
  }
});

export { unpublish_put as default };
//# sourceMappingURL=unpublish.put.mjs.map
