import { V as getHeader, d as defineEventHandler, u as useLogger, ae as sendRedirect, g as getRouterParam, c as createError, w as publicSettings, t as setHeader } from '../../../nitro/nitro.mjs';
import Negotiator from 'negotiator';
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';

function negotiate(event) {
  const supported = ["text/html", "application/rss+xml", "text/calendar", "application/json"];
  const negotiator = new Negotiator({
    headers: {
      accept: getHeader(event, "accept") || ""
    }
  });
  return negotiator.mediaType(supported);
}
function preferHTML(event) {
  return negotiate(event) === "text/html";
}

const index_get = defineEventHandler(async (event) => {
  const log = useLogger("federation actor");
  if (preferHTML(event)) {
    log.debug("[FEDI] Get actor but prefer text/html, redirect to homepage");
    sendRedirect(event, "/", 302);
  }
  const name = getRouterParam(event, "name");
  if (!name) {
    log.warn(`[FEDI] Bad 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} ?)`
    });
  }
  setHeader(event, "Content-Type", "application/activity+json; charset=utf-8");
  return {
    "@context": [
      "https://www.w3.org/ns/activitystreams",
      "https://w3id.org/security/v1",
      {
        toot: "http://joinmastodon.org/ns#",
        // A property-value pair, e.g. representing a feature of a product or place. We use this to publish this very same instance
        // https://docs.joinmastodon.org/spec/activitypub/#PropertyValue
        schema: "http://schema.org#",
        PropertyValue: "schema:PropertyValue",
        value: "schema:value",
        // https://docs.joinmastodon.org/spec/activitypub/#discoverable
        discoverable: "toot:discoverable",
        indexable: "toot:indexable"
      }
    ],
    "id": `${publicSettings.baseurl}/federation/u/${name}`,
    "type": "Application",
    "summary": publicSettings.description,
    "name": publicSettings.title,
    "preferredUsername": name,
    "inbox": `${publicSettings.baseurl}/federation/u/${name}/inbox`,
    "outbox": `${publicSettings.baseurl}/federation/u/${name}/outbox`,
    "manuallyApprovesFollowers": false,
    "upcomingEvents": `${publicSettings.baseurl}/federation/u/${name}/events`,
    "endpoints": { sharedInbox: `${publicSettings.baseurl}/federation/u/${name}/inbox` },
    "discoverable": true,
    "indexable": true,
    "attachment": [
      {
        type: "PropertyValue",
        name: "Website",
        value: `<a href='${publicSettings.baseurl}'>${publicSettings.baseurl}</a>`
      }
    ],
    "icon": {
      type: "Image",
      mediaType: "image/png",
      url: publicSettings.baseurl + "/logo.png"
    },
    "image": {
      type: "Image",
      mediaType: "image/png",
      url: publicSettings.baseurl + "/headerimage.png"
    },
    "publicKey": {
      id: `${publicSettings.baseurl}/federation/u/${name}#main-key`,
      owner: `${publicSettings.baseurl}/federation/u/${name}`,
      publicKeyPem: publicSettings.publicKey
    },
    "url": `${publicSettings.baseurl}/federation/u/${name}`
  };
});

export { index_get as default };
