import { d as defineEventHandler, g as getHeader, j as getQuery, h as requireAdminRole, i as requireApiTokenScopes, c as createError, l as createEventStream, m as disconnectClient, n as connectClient, A as ADMIN_ACTIVITY_CHANNEL, o as generateClientId } 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 stream_get = defineEventHandler(async (event) => {
  var _a, _b;
  event.context.adminRouteName = "admin:activity:stream";
  const authHeader = getHeader(event, "authorization");
  const queryToken = getQuery(event).token;
  const token = Array.isArray(queryToken) ? queryToken[0] : queryToken;
  if (!authHeader && typeof token === "string" && token.trim().length > 0) {
    event.node.req.headers.authorization = `Bearer ${token.trim()}`;
  }
  await requireAdminRole(event, ["super-admin", "admin", "editor"]);
  if (event.context.apiToken) {
    requireApiTokenScopes(event.context.apiToken, {
      scopes: ["activity:read", "realtime:read"],
      match: "all"
    });
  }
  const adminUser = event.context.adminUser;
  if (!adminUser) {
    throw createError({
      statusCode: 401,
      statusMessage: "Unauthorized",
      message: "Admin authentication required"
    });
  }
  const eventStream = createEventStream(event);
  const clientId = generateClientId();
  const client = {
    id: clientId,
    channel: ADMIN_ACTIVITY_CHANNEL,
    presence: {
      clientId,
      userType: "admin",
      userId: adminUser.id,
      displayName: adminUser.username || adminUser.email || `admin:${adminUser.id}`,
      email: (_a = adminUser.email) != null ? _a : null,
      role: (_b = adminUser.role) != null ? _b : null,
      connectedAt: (/* @__PURE__ */ new Date()).toISOString()
    },
    send: (realtimeEvent) => {
      eventStream.push({
        event: realtimeEvent.type,
        data: JSON.stringify(realtimeEvent)
      }).catch(() => {
      });
    },
    close: () => {
      eventStream.close().catch(() => {
      });
    }
  };
  eventStream.onClosed(() => {
    disconnectClient(clientId, ADMIN_ACTIVITY_CHANNEL);
  });
  connectClient(client);
  return eventStream.send();
});

export { stream_get as default };
//# sourceMappingURL=stream.get.mjs.map
