import { y as defineEventHandler, b0 as useLogger, aa as getRouterParam, v as createError, ar as normalizeTag, T as Tag, a0 as getEvents, r as calculateEtag, af as handleCache, ap as mapEvents } from '../../../../nitro/nitro.mjs';
import 'luxon';
import '@sequelize/core';
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';

const _tag_ = defineEventHandler(async (evt) => {
  const log = useLogger("events/tag");
  const tagName = getRouterParam(evt, "tag", { decode: true });
  if (!tagName) {
    log.warn("Tag not specified");
    throw createError({ statusCode: 404, statusMessage: "Tag not specified" });
  }
  const normalizedTag = normalizeTag(tagName);
  const tags = await Tag.findAll({ where: { normalizedTag }, raw: true });
  if (!tags) {
    log.warn("Tag not found", tagName);
    throw createError({ statusCode: 404, message: "Tag not found" });
  }
  const events = await getEvents({ tags: [tagName], show_federated: true });
  const pastEvents = await getEvents({ tags: [tagName], show_federated: true, older: true, reverse: true });
  const etag = calculateEtag([...events, ...pastEvents]);
  if (await handleCache(evt, etag)) return;
  return {
    events: mapEvents(events),
    pastEvents: mapEvents(pastEvents)
  };
});

export { _tag_ as default };
