import { d as defineEventHandler, c as createError, g as getHeader, j as getQuery, dL as optionalAuth, i as requireApiTokenScopes, bT as requirePermission, l as createEventStream, m as disconnectClient, n as connectClient, o as generateClientId, ek as contentChannel } 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';

function getDisplayName(event) {
  if (event.context.adminUser) {
    return event.context.adminUser.username || event.context.adminUser.email || `admin:${event.context.adminUser.id}`;
  }
  if (event.context.user) {
    return event.context.user.username || event.context.user.email || `user:${event.context.user.id}`;
  }
  return "anonymous";
}
const stream_get = defineEventHandler(async (event) => {
  var _a, _b, _c, _d, _e, _f, _g, _h;
  const contentType = (_a = event.context.params) == null ? void 0 : _a.contentType;
  const id = (_b = event.context.params) == null ? void 0 : _b.id;
  if (!contentType || !id) {
    throw createError({
      statusCode: 400,
      statusMessage: "Bad Request",
      message: "Content type and ID are required"
    });
  }
  const numericId = parseInt(id);
  if (isNaN(numericId)) {
    throw createError({
      statusCode: 400,
      statusMessage: "Bad Request",
      message: "ID must be a valid number"
    });
  }
  const authHeader = getHeader(event, "authorization");
  const queryToken = getQuery(event).token;
  const tokenFromQuery = Array.isArray(queryToken) ? queryToken[0] : queryToken;
  if (!authHeader && typeof tokenFromQuery === "string" && tokenFromQuery.trim().length > 0) {
    event.node.req.headers.authorization = `Bearer ${tokenFromQuery.trim()}`;
  }
  await optionalAuth(event);
  if (!event.context.adminUser && !event.context.user && !event.context.apiToken) {
    throw createError({
      statusCode: 401,
      statusMessage: "Unauthorized",
      message: "Authentication required"
    });
  }
  if (event.context.apiToken) {
    requireApiTokenScopes(event.context.apiToken, { scopes: ["realtime:read"] });
  }
  await requirePermission(
    event.context.user,
    contentType,
    "read",
    event.context.adminUser,
    event.context.apiToken
  );
  const adminUser = event.context.adminUser;
  const contentUser = event.context.user;
  const userType = adminUser ? "admin" : "user";
  const userId = (_d = (_c = adminUser == null ? void 0 : adminUser.id) != null ? _c : contentUser == null ? void 0 : contentUser.id) != null ? _d : 0;
  const displayName = getDisplayName(event);
  const email = (_f = (_e = adminUser == null ? void 0 : adminUser.email) != null ? _e : contentUser == null ? void 0 : contentUser.email) != null ? _f : null;
  const role = (_h = (_g = adminUser == null ? void 0 : adminUser.role) != null ? _g : contentUser == null ? void 0 : contentUser.role) != null ? _h : null;
  const eventStream = createEventStream(event);
  const channel = contentChannel(contentType, numericId);
  const clientId = generateClientId();
  const client = {
    id: clientId,
    channel,
    presence: {
      clientId,
      userType,
      userId,
      displayName,
      email,
      role,
      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, channel);
  });
  connectClient(client);
  return eventStream.send();
});

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