import { y as defineEventHandler, H as enableCors, b0 as useLogger, aa as getRouterParam, ac as getValidatedQuery, E as Event, T as Tag, d as Place, U as User, v as createError, s as canModifyEvent, ao as mapEvent, aW as setHeader, N as eventToAP } from '../../../../nitro/nitro.mjs';
import { g as getUser } from '../../../../_/getUser.mjs';
import { Op } from '@sequelize/core';
import { f as formatSchema, e as exportICS } from '../../../../_/ics.mjs';
import 'luxon';
import 'html-to-text';
import 'zod';
import 'crypto';
import 'node:fs';
import 'sharp';
import 'path';
import 'node:stream';
import 'jsdom';
import 'dompurify';
import 'mime-types';
import 'fs';
import 'fs/promises';
import 'linkify-html';
import '@peertube/http-signature';
import 'jsonld';
import 'lodash.intersection';
import 'email-templates';
import 'node:http';
import 'node:https';
import 'node:crypto';
import 'node:events';
import 'node:buffer';
import 'node:path';
import 'vue';
import 'vue-router';
import 'dotenv';
import 'url';
import 'bcryptjs';
import 'slugify';
import 'umzug';
import 'consola';
import '@sequelize/core/decorators-legacy';
import 'util';
import 'mitt';
import 'ical.js';
import 'node:url';
import 'fast-xml-parser';
import '../../../../_/jwt.mjs';
import 'jose';
import 'ics';

const _slug_or_id_ = defineEventHandler(async (evt) => {
  enableCors(evt);
  const log = useLogger("event details!");
  const user = await getUser(evt);
  const slug_or_id = getRouterParam(evt, "slug_or_id");
  const { format } = await getValidatedQuery(evt, formatSchema.parse);
  const id = Number(slug_or_id);
  const where = isNaN(id) || !Number.isInteger(id) ? { slug: slug_or_id } : { [Op.or]: [{ slug: slug_or_id }, { id }] };
  const event = await Event.findOne({
    where,
    attributes: {
      exclude: ["createdAt", "placeId", "apUserApId"]
    },
    include: [
      { model: Tag, required: false, attributes: ["tag"], through: { attributes: [] } },
      { model: Place, attributes: ["name", "address", "latitude", "longitude", "id", "ap_id"] },
      { model: User, required: false, attributes: ["is_active"] },
      { model: Event, required: false, as: "parent", attributes: ["id", "recurrent", "is_visible", "start_datetime"] }
    ]
  });
  if (!event) {
    log.warn(`Event not found: ${slug_or_id}`);
    throw createError({ statusCode: 404, statusMessage: "Event not found" });
  }
  if (!event.is_visible && !canModifyEvent(event, user)) {
    throw createError({ statusCode: 404, statusMessage: "Event not found" });
  }
  if (format === "json") {
    return mapEvent(event, { user });
  } else if (format === "json-ld") {
    setHeader(evt, "Content-Type", "application/activity+json");
    return eventToAP(event);
  } else if (format === "ics") {
    setHeader(evt, "Content-Type", "text/calendar");
    return exportICS(evt, [event], [{
      action: "display",
      description: event.title,
      trigger: { hours: 1, before: true }
    }]);
  }
});

export { _slug_or_id_ as default };
