import sanitizeHtml from "sanitize-html";
const ABSOLUTE_PROTOCOLS = new Set(["http:", "https:"]);
const NAVIGATION_PROTOCOLS = new Set(["http:", "https:", "mailto:", "tel:"]);
function sanitizeUrl(value, allowedProtocols, allowRelative) {
    if (!value)
        return undefined;
    const normalized = value.trim();
    if (!normalized)
        return undefined;
    if (allowRelative &&
        (normalized.startsWith("/") ||
            normalized.startsWith("#") ||
            normalized.startsWith("?"))) {
        return normalized;
    }
    try {
        const parsed = new URL(normalized);
        return allowedProtocols.has(parsed.protocol) ? normalized : undefined;
    }
    catch {
        return undefined;
    }
}
export function sanitizeInlineHtml(value) {
    if (!value)
        return undefined;
    return sanitizeHtml(value, {
        allowedTags: [
            "b",
            "br",
            "em",
            "i",
            "small",
            "span",
            "strong",
            "sub",
            "sup",
        ],
        allowedAttributes: {
            span: ["class"],
        },
        allowedSchemes: ["http", "https", "mailto", "tel"],
        disallowedTagsMode: "discard",
    });
}
export function sanitizeNavigationUrl(value) {
    return sanitizeUrl(value, NAVIGATION_PROTOCOLS, true);
}
export function sanitizeAbsoluteUrl(value) {
    return sanitizeUrl(value, ABSOLUTE_PROTOCOLS, false);
}
