import { aC as publicSettings, aW as setHeader, aI as runtimeResolve, y as defineEventHandler, b0 as useLogger, ad as getValidatedRouterParams, ac as getValidatedQuery, L as eventQuerySchema, af as handleCache, a0 as getEvents, ap as mapEvents, v as createError, E as Event } from '../../nitro/nitro.mjs';
import { f as formatSchema, e as exportICS } from '../../_/ics.mjs';
import { DateTime } from 'luxon';
import fs from 'fs';
import pug from 'pug';
import { g as getCollectionEvents } from '../../_/collections.mjs';
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/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 'ics';

const rssTemplateStr = fs.readFileSync(runtimeResolve("./static/rss.pug"), "utf-8");
const rssTemplate = pug.compile(rssTemplateStr);
function exportFeed(evt, events, title = publicSettings.title, link = `${publicSettings.baseurl}/feed/rss`) {
  const opt = {
    zone: publicSettings.instance_timezone,
    locale: publicSettings.instance_locale
  };
  function unixFormat(timestamp, format = "EEEE d MMMM HH:mm") {
    return DateTime.fromSeconds(timestamp, opt).toFormat(format);
  }
  setHeader(evt, "Content-Type", "application/rss+xml; charset=UTF-8");
  return rssTemplate({ events, settings: publicSettings, unixFormat, title, link });
}

const _format_ = defineEventHandler(async (evt) => {
  const log = useLogger("feed events");
  const { format } = await getValidatedRouterParams(evt, formatSchema.parse);
  const opt = await getValidatedQuery(evt, eventQuerySchema.parse);
  if (await handleCache(evt, Event)) return;
  let events;
  if (publicSettings.collection_in_home && !(opt.tags || opt.places || opt.query)) {
    log.debug("get collection events", publicSettings.collection_in_home);
    events = await getCollectionEvents({ name: publicSettings.collection_in_home, ...opt });
  } else {
    events = await getEvents(opt);
  }
  switch (format) {
    case "rss":
      return exportFeed(evt, events);
    case "ics":
      return exportICS(evt, events);
    case "json":
      return mapEvents(events);
  }
  throw createError({ status: 400, message: "Please specify a valid format: rss, feed, ics or json" });
});

export { _format_ as default };
