import { y as defineEventHandler, b0 as useLogger, aC as publicSettings, v as createError, a4 as getQuery, aW as setHeader } 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 webfinger_get = defineEventHandler(async (event) => {
  const log = useLogger("webfinger");
  if (!publicSettings.enable_federation) {
    throw createError({ statusCode: 404, statusMessage: "Federation disabled" });
  }
  const { resource } = getQuery(event);
  const match = resource?.match(/^acct:([^@]+)@([^@]+)$/);
  if (!match) {
    log.info({ resource }, "Bad webfinger request");
    throw createError({ statusCode: 400, statusMessage: 'Bad request. Please make sure "acct:USER@DOMAIN" is what you are sending as the "resource" query parameter.' });
  }
  const [, name, req_domain] = match;
  const domain = new URL(publicSettings.baseurl).host;
  if (domain !== req_domain) {
    log.warn(`Bad webfinger request, requested domain "${req_domain}" instead of "${domain}"`);
    throw createError({ statusCode: 400, statusMessage: 'Bad request. Please make sure "acct:USER@DOMAIN" is what you are sending as the "resource" query parameter.' });
  }
  if (name !== publicSettings.instance_name) {
    log.warn(`User not found: ${name} (instance name is ${publicSettings.instance_name}`);
    throw createError({ statusCode: 404, statusMessage: `No record found for ${name}, are you searching for ${publicSettings.instance_name} ?` });
  }
  log.info(`webfinger ${resource} ${domain}`);
  setHeader(event, "Content-Type", "application/jrd+json");
  return {
    subject: `acct:${name}@${domain}`,
    links: [
      {
        rel: "self",
        type: "application/activity+json",
        href: `${publicSettings.baseurl}/federation/u/${name}`
      },
      {
        href: publicSettings.baseurl + "/logo.png",
        rel: "http://webfinger.net/rel/avatar",
        type: "image/png"
      }
    ]
  };
});

export { webfinger_get as default };
