import { f as defineAdminEventHandler, r as requireAuth, c as createError, ag as findAdminUserById, a3 as tryGetTenantContext, a4 as getDefaultTenantId, aa as generateAdminJWTForTenant, $ as getAuthRequestContext, az as emitAdminTokenRefresh } 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 refresh_post = defineAdminEventHandler("admin:auth:refresh", async (event) => {
  var _a;
  await requireAuth(event);
  if (event.context.apiToken) {
    throw createError({
      statusCode: 403,
      statusMessage: "Forbidden",
      message: "API tokens cannot be exchanged for admin sessions"
    });
  }
  const adminUserCtx = event.context.adminUser;
  if (!adminUserCtx) {
    throw createError({
      statusCode: 401,
      statusMessage: "Unauthorized",
      message: "Admin authentication required"
    });
  }
  const adminUser = await findAdminUserById(adminUserCtx.id);
  if (!adminUser) {
    throw createError({
      statusCode: 401,
      statusMessage: "Unauthorized",
      message: "Admin user not found"
    });
  }
  const tenant = tryGetTenantContext(event);
  const tenantId = (_a = tenant == null ? void 0 : tenant.id) != null ? _a : await getDefaultTenantId();
  const token = generateAdminJWTForTenant(
    {
      id: adminUser.id,
      username: adminUser.username,
      email: adminUser.email,
      role: adminUser.role
    },
    tenantId
  );
  const reqCtx = getAuthRequestContext(event);
  await emitAdminTokenRefresh(adminUser.id, {
    ...reqCtx,
    username: adminUser.username,
    email: adminUser.email,
    role: adminUser.role
  });
  return {
    success: true,
    data: {
      token,
      user: {
        id: adminUser.id,
        username: adminUser.username,
        email: adminUser.email,
        firstName: adminUser.firstName,
        lastName: adminUser.lastName,
        role: adminUser.role
      }
    }
  };
});

export { refresh_post as default };
//# sourceMappingURL=refresh.post.mjs.map
