import { f as defineAdminEventHandler, h as requireAdminRole, dm as requireTenantContext, bc as getRouterParam, c as createError, dn as findWorkspaceById, dp as deleteWorkspace, dq as decrementUsage } 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 _id__delete = defineAdminEventHandler("admin:workspaces:delete", async (event) => {
  try {
    await requireAdminRole(event, ["super-admin", "admin"]);
    const tenant = requireTenantContext(event);
    const id = getRouterParam(event, "id");
    if (!id) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "id required",
        data: { code: "workspace.id.required" }
      });
    }
    const existing = await findWorkspaceById(id);
    if (!existing || existing.tenantId !== tenant.id) {
      throw createError({
        statusCode: 404,
        statusMessage: "Not Found",
        message: "Workspace not found",
        data: { code: "workspace.notFound" }
      });
    }
    await deleteWorkspace(id);
    await decrementUsage(tenant.id, "workspaces", 1);
    return { success: true };
  } catch (error) {
    if (error && typeof error === "object" && "statusCode" in error) {
      throw error;
    }
    const message = error instanceof Error ? error.message : "Unknown error";
    const code = /default workspace/i.test(message) || /at least one workspace/i.test(message) ? "workspace.deleteProtected" : "workspace.deleteFailed";
    throw createError({
      statusCode: code === "workspace.deleteProtected" ? 400 : 500,
      statusMessage: code === "workspace.deleteProtected" ? "Bad Request" : "Failed to delete workspace",
      message,
      data: { code }
    });
  }
});

export { _id__delete as default };
//# sourceMappingURL=_id_.delete.mjs.map
