import { f as defineAdminEventHandler, h as requireAdminRole, dm as requireTenantContext, c as createError, cx as getTenantQuota, cy as getTenantUsage, ds as createWorkspace, dt as incrementUsage } 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 = defineAdminEventHandler(
  "admin:workspaces:create",
  async (event, body) => {
    var _a, _b, _c, _d, _e;
    try {
      await requireAdminRole(event, ["super-admin", "admin"]);
      const tenant = requireTenantContext(event);
      const slug = (_b = (_a = body.slug) == null ? void 0 : _a.trim().toLowerCase()) != null ? _b : "";
      const name = (_d = (_c = body.name) == null ? void 0 : _c.trim()) != null ? _d : "";
      if (!slug || !name) {
        throw createError({
          statusCode: 400,
          statusMessage: "Bad Request",
          message: "slug and name are required",
          data: { code: "workspace.validation.required" }
        });
      }
      if (!/^[a-z0-9-]+$/.test(slug)) {
        throw createError({
          statusCode: 400,
          statusMessage: "Bad Request",
          message: "slug may contain only lowercase letters, numbers and hyphens",
          data: { code: "workspace.validation.invalidSlug" }
        });
      }
      const quota = await getTenantQuota(tenant.id);
      if ((quota == null ? void 0 : quota.workspacesLimit) !== null && (quota == null ? void 0 : quota.workspacesLimit) !== void 0) {
        const usage = await getTenantUsage(tenant.id);
        if (usage.workspacesCount + 1 > quota.workspacesLimit) {
          throw createError({
            statusCode: 429,
            statusMessage: "Quota exceeded",
            message: "Workspaces quota exceeded for this tenant",
            data: { code: "quota.exceeded", quota: "workspaces" }
          });
        }
      }
      const workspace = await createWorkspace({
        tenantId: tenant.id,
        slug,
        name,
        settings: (_e = body.settings) != null ? _e : null
      });
      await incrementUsage(tenant.id, "workspaces", 1);
      return { success: true, data: workspace };
    } 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: "Workspace with this slug already exists in tenant",
          data: { code: "workspace.slug.exists" }
        });
      }
      throw createError({
        statusCode: 500,
        statusMessage: "Failed to create workspace",
        message: msg,
        data: { code: "workspace.createFailed" }
      });
    }
  }
);

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