import { userAgent as t } from "fast-meta-tags"
import n from "timeout-abort-signal"
import e from "./oembed.json" with { type: "json" }
import { matchDomain as r } from "./utils.js"
export async function fetchOembed(e, r) {
const l = o(e)
if (!l) return null
const s = n(r?.signal, r?.timeout)
try {
const n = await fetch(l, { signal: s.signal, headers: { "User-Agent": r?.userAgent || t } })
if (!n.ok) return null
const e = await n.json(),
o = e.description || null
return {
title: e.title || null,
name: e.provider_name || null,
description: o && o.length > 500 ? o.slice(0, 500) : o,
image: e.thumbnail_url || null,
}
} catch {
return null
} finally {
s.destroy()
}
}
function o(t) {
const n = l(t)
return n ? `${n.replace("{format}", "json")}?url=${encodeURIComponent(t)}` : null
}
function l(t) {
for (const n of e)
for (const e of n.endpoints)
if (e.schemes) {
for (const n of e.schemes) if (match(t, n)) return e.url
} else if (r(t, n.url)) return e.url
return null
}
export function match(t, n) {
const e = n.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, "[^.]*")
return new RegExp(`^${e}$`).test(t)
}