import { dK as defineContentEventHandler, c as createError, dL as optionalAuth, bT as requirePermission, a as getSchemaManager, ea as isLocaleAllowedForSchema, G as createContentManager } 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 index_post = defineContentEventHandler("update", async (event, input) => {
  var _a, _b, _c, _d, _e, _f, _g, _h;
  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,
      "create",
      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 body = input;
    if (!body || typeof body !== "object") {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "Request body must be a valid JSON object"
      });
    }
    const targetLocale = body.locale;
    if (!targetLocale) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "locale is required in request body"
      });
    }
    const schema = getSchemaManager().getSchema(contentType);
    if (!((_d = (_c = schema == null ? void 0 : schema.options) == null ? void 0 : _c.i18n) == null ? void 0 : _d.enabled)) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: `Schema '${contentType}' does not support i18n`
      });
    }
    if (!await isLocaleAllowedForSchema(schema, targetLocale)) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: `Locale '${targetLocale}' is not enabled for schema '${contentType}'`
      });
    }
    const contentManager = createContentManager(contentType);
    const sourceRecord = await contentManager.findOne(numericId);
    if (!sourceRecord) {
      throw createError({
        statusCode: 404,
        statusMessage: "Not Found",
        message: `${contentType} with id ${id} not found`
      });
    }
    const documentId = sourceRecord.documentId;
    if (!documentId) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: `Schema '${contentType}' does not support i18n or record has no documentId`
      });
    }
    const existingLocales = await contentManager.getAvailableLocales(documentId);
    if (existingLocales.includes(targetLocale)) {
      throw createError({
        statusCode: 409,
        statusMessage: "Conflict",
        message: `Localization for locale '${targetLocale}' already exists`
      });
    }
    const createData = {
      ...body,
      documentId,
      locale: targetLocale
    };
    const result = await contentManager.create(createData, {
      actor: getAuditActorFromEvent(event),
      adminUserId: (_f = (_e = event.context.adminUser) == null ? void 0 : _e.id) != null ? _f : null
    });
    return {
      success: true,
      message: `Localization for '${targetLocale}' created successfully`,
      data: result
    };
  } catch (error) {
    mapContentApiError(error, {
      routeLabel: `POST /api/content/${(_g = event.context.params) == null ? void 0 : _g.contentType}/${(_h = event.context.params) == null ? void 0 : _h.id}/localizations`
    });
  }
});

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