import { dK as defineContentEventHandler, c as createError, dL as optionalAuth, bT as requirePermission, et as validateFilterOperators, G as createContentManager, ad as logAuditEventAsync } 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 bulkUpdate_patch = defineContentEventHandler("bulk-update", async (event, input) => {
  var _a, _b;
  try {
    const contentType = (_a = event.context.params) == null ? void 0 : _a.contentType;
    if (!contentType) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "Content type is required"
      });
    }
    await optionalAuth(event);
    await requirePermission(
      event.context.user,
      contentType,
      "update",
      event.context.adminUser,
      event.context.apiToken
    );
    const body = input || {};
    const { filters = {}, data } = body;
    if (!data || Object.keys(data).length === 0) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "Update data is required"
      });
    }
    const filterViolations = validateFilterOperators(filters);
    if (filterViolations.length > 0) {
      const errors = filterViolations.map((v) => v.message).join("; ");
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: `Invalid filters: ${errors}`
      });
    }
    const contentManager = createContentManager(contentType);
    const updatedCount = await contentManager.bulkUpdate(filters, data);
    logAuditEventAsync({
      action: "content.bulk_update",
      actor: getAuditActorFromEvent(event),
      target: {
        entityType: contentType,
        entityId: null,
        displayName: contentType
      },
      metadata: {
        filters,
        data,
        count: updatedCount
      }
    });
    return {
      success: true,
      updated: updatedCount,
      message: `Successfully updated ${updatedCount} records`
    };
  } catch (error) {
    mapContentApiError(error, {
      routeLabel: `PATCH /api/content/${(_b = event.context.params) == null ? void 0 : _b.contentType}/bulk-update`
    });
  }
});

export { bulkUpdate_patch as default };
//# sourceMappingURL=bulk-update.patch.mjs.map
