import { d as defineEventHandler, cu as requirePlatformAdmin, bc as getRouterParam, c as createError, cv as findTenantById, q as readBody, cA as addTenantMembership } 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__memberships_post = defineEventHandler(async (event) => {
  var _a, _b;
  try {
    await requirePlatformAdmin(event);
    const id = getRouterParam(event, "id");
    if (!id) {
      throw createError({ statusCode: 400, statusMessage: "Bad Request", message: "id required" });
    }
    const tenant = await findTenantById(id);
    if (!tenant) {
      throw createError({
        statusCode: 404,
        statusMessage: "Not Found",
        message: "Tenant not found"
      });
    }
    const body = await readBody(event);
    if (!body.userId) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "userId required"
      });
    }
    const adminUser = event.context.adminUser;
    await addTenantMembership(
      body.userId,
      id,
      body.role || "editor",
      (_b = (_a = body.invitedBy) != null ? _a : adminUser == null ? void 0 : adminUser.id) != null ? _b : null
    );
    return { success: true };
  } catch (error) {
    if (error && typeof error === "object" && "statusCode" in error) {
      throw error;
    }
    throw createError({
      statusCode: 500,
      statusMessage: "Failed to add membership",
      message: error instanceof Error ? error.message : "Unknown error"
    });
  }
});

export { _id__memberships_post as default };
//# sourceMappingURL=_id_.memberships.post.mjs.map
