import { d as defineEventHandler, u as useLogger, ad as getQuery, g as getRouterParam, c as createError, w as publicSettings, t as setHeader, E as Event, T as Tag, P as Place, v as eventToAP } 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 outbox_get = defineEventHandler(async (event) => {
  const EVENTS_PER_PAGE = 10;
  const log = useLogger("federation outbox");
  const page = parseInt(getQuery(event).page);
  const name = getRouterParam(event, "name");
  if (!name) {
    log.warn(`[FEDI] Bad /outbox request`);
    throw createError({ statusCode: 400, statusMessage: "Bad request" });
  }
  if (name !== publicSettings.instance_name) {
    log.debug(`[FEDI] Get unknown actor ${name} (should be ${publicSettings.instance_name}`);
    throw createError({
      statusCode: 404,
      statusMessage: `No record found for ${name} (are you searching for ${publicSettings.instance_name} ?)`
    });
  }
  let events = [];
  setHeader(event, "Content-Type", "application/activity+json; charset=utf-8");
  const n_events = await Event.count({ where: { is_visible: true, ap_id: null } });
  const last_page = Math.ceil(n_events / 10);
  log.debug(`[FEDI] GET /outbox, should return all events from this instance: ${n_events}`);
  if (page > 0) {
    events = await Event.findAll({
      where: { is_visible: true, ap_id: null },
      include: [{ model: Tag, required: false }, Place],
      limit: EVENTS_PER_PAGE,
      offset: (page - 1) * EVENTS_PER_PAGE,
      order: [["start_datetime", "DESC"]]
    });
    return {
      // '@context': Helpers['@context'],
      id: `${publicSettings.baseurl}/federation/u/${name}/outbox?page=${page}`,
      type: "OrderedCollectionPage",
      totalItems: n_events,
      partOf: `${publicSettings.baseurl}/federation/u/${name}/outbox`,
      ...page > 1 && last_page > 0 && {
        prev: `${publicSettings.baseurl}/federation/u/${name}/outbox?page=${Math.min(page - 1, last_page)}`
      },
      ...page < last_page && { next: `${publicSettings.baseurl}/federation/u/${name}/outbox?page=${page + 1}` },
      orderedItems: events.map((e) => ({
        id: `${publicSettings.baseurl}/federation/m/${e.id}#create`,
        type: "Create",
        to: "https://www.w3.org/ns/activitystreams#Public",
        published: e.createdAt.toISOString(),
        actor: `${publicSettings.baseurl}/federation/u/${name}`,
        object: eventToAP(e)
      }))
    };
  } else {
    return {
      "@context": "https://www.w3.org/ns/activitystreams",
      "id": `${publicSettings.baseurl}/federation/u/${name}/outbox`,
      "type": "OrderedCollection",
      "totalItems": n_events,
      ...last_page > 0 && {
        first: `${publicSettings.baseurl}/federation/u/${name}/outbox?page=1`,
        last: `${publicSettings.baseurl}/federation/u/${name}/outbox?page=${last_page}`
      }
    };
  }
});

export { outbox_get as default };
