import { d as defineEventHandler, cu as requirePlatformAdmin, q as readBody, c as createError, cE as createTenant } 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 index_post = defineEventHandler(async (event) => {
  var _a, _b, _c;
  try {
    await requirePlatformAdmin(event);
    const body = await readBody(event);
    if (!body.slug || !body.name) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "slug and name are required"
      });
    }
    if (!/^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$/.test(body.slug)) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "slug must be lowercase alphanumeric with optional hyphens (max 64 chars)"
      });
    }
    const tenant = await createTenant({
      slug: body.slug,
      name: body.name,
      isolationMode: (_a = body.isolationMode) != null ? _a : "shared",
      databaseUrl: (_b = body.databaseUrl) != null ? _b : null,
      storageConfig: (_c = body.storageConfig) != null ? _c : null
    });
    return { success: true, data: tenant };
  } catch (error) {
    if (error && typeof error === "object" && "statusCode" in error) {
      throw error;
    }
    const msg = error instanceof Error ? error.message : "Unknown error";
    if (/UNIQUE|duplicate/i.test(msg)) {
      throw createError({
        statusCode: 409,
        statusMessage: "Conflict",
        message: "Tenant with this slug already exists"
      });
    }
    throw createError({
      statusCode: 500,
      statusMessage: "Failed to create tenant",
      message: msg
    });
  }
});

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