import { getHtml } from "../misc/fetch.js";
import { JSDOM } from "jsdom";
import config from "../config/index.js";
async function getRelMeLinks(url) {
    try {
        const html = await getHtml(url);
        const dom = new JSDOM(html);
        const relMeLinks = [
            ...dom.window.document.querySelectorAll("a[rel='me']"),
            ...dom.window.document.querySelectorAll("link[rel='me']")
        ].map((a)=>a.href);
        return relMeLinks;
    } catch  {
        return [];
    }
}
export async function verifyLink(link, username) {
    let verified = false;
    if (link.startsWith("http")) {
        const relMeLinks = await getRelMeLinks(link);
        verified = relMeLinks.some((href)=>new RegExp(`^https?:\/\/${config.host.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&")}\/@${username.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&")}$`).test(href));
    }
    return verified;
}
