import { DIRECT_MEDIA_EXTENSIONS, inferDirectMediaKind, isDirectMediaExtension, isDirectMediaUrl, isDirectVideoInput, } from "./direct-media.js";
import { isPodcastHost } from "./link-preview/content/podcast-utils.js";
import { isTwitterBroadcastUrl, isTwitterStatusUrl } from "./link-preview/content/twitter-utils.js";
export const isYouTubeUrl = (rawUrl) => {
    try {
        const hostname = new URL(rawUrl).hostname.toLowerCase();
        return (hostname === "youtube.com" || hostname.endsWith(".youtube.com") || hostname === "youtu.be");
    }
    catch {
        const lower = rawUrl.toLowerCase();
        return lower.includes("youtube.com") || lower.includes("youtu.be");
    }
};
const YOUTUBE_VIDEO_ID_PATTERN = /^[a-zA-Z0-9_-]{11}$/;
export function isYouTubeVideoUrl(rawUrl) {
    try {
        const url = new URL(rawUrl);
        const hostname = url.hostname.toLowerCase();
        if (hostname === "youtu.be") {
            return Boolean(url.pathname.split("/").filter(Boolean)[0]);
        }
        if (hostname !== "youtube.com" && !hostname.endsWith(".youtube.com")) {
            return false;
        }
        if (url.pathname === "/watch") {
            return Boolean(url.searchParams.get("v")?.trim());
        }
        return (url.pathname.startsWith("/shorts/") ||
            url.pathname.startsWith("/live/") ||
            url.pathname.startsWith("/embed/") ||
            url.pathname.startsWith("/v/"));
    }
    catch {
        return false;
    }
}
export function extractYouTubeVideoId(rawUrl) {
    try {
        const url = new URL(rawUrl);
        const hostname = url.hostname.toLowerCase();
        let candidate = null;
        if (hostname === "youtu.be") {
            candidate = url.pathname.split("/")[1] ?? null;
        }
        if (hostname === "youtube.com" || hostname.endsWith(".youtube.com")) {
            if (url.pathname.startsWith("/watch")) {
                candidate = url.searchParams.get("v");
            }
            else if (url.pathname.startsWith("/shorts/")) {
                candidate = url.pathname.split("/")[2] ?? null;
            }
            else if (url.pathname.startsWith("/live/")) {
                candidate = url.pathname.split("/")[2] ?? null;
            }
            else if (url.pathname.startsWith("/embed/")) {
                candidate = url.pathname.split("/")[2] ?? null;
            }
            else if (url.pathname.startsWith("/v/")) {
                candidate = url.pathname.split("/")[2] ?? null;
            }
        }
        const trimmed = candidate?.trim() ?? "";
        if (!trimmed) {
            return null;
        }
        return YOUTUBE_VIDEO_ID_PATTERN.test(trimmed) ? trimmed : null;
    }
    catch {
        // ignore parsing errors
    }
    return null;
}
const LOOM_VIDEO_ID_PATTERN = /^[a-f0-9]{32}$/;
export function isLoomVideoUrl(rawUrl) {
    try {
        const url = new URL(rawUrl);
        if ((url.protocol !== "http:" && url.protocol !== "https:") ||
            url.port !== "" ||
            url.username !== "" ||
            url.password !== "") {
            return false;
        }
        const hostname = url.hostname.toLowerCase();
        if (hostname !== "loom.com" && hostname !== "www.loom.com") {
            return false;
        }
        const parts = url.pathname.split("/").filter(Boolean);
        return (parts.length === 2 &&
            (parts[0] === "share" || parts[0] === "embed") &&
            LOOM_VIDEO_ID_PATTERN.test(parts[1] ?? ""));
    }
    catch {
        return false;
    }
}
export function shouldPreferUrlMode(url) {
    return (isYouTubeVideoUrl(url) ||
        isTwitterStatusUrl(url) ||
        isTwitterBroadcastUrl(url) ||
        isDirectMediaUrl(url) ||
        isPodcastHost(url));
}
export { isTwitterBroadcastUrl, isTwitterStatusUrl, isPodcastHost };
export { DIRECT_MEDIA_EXTENSIONS, inferDirectMediaKind, isDirectMediaExtension, isDirectMediaUrl, isDirectVideoInput, };
//# sourceMappingURL=url.js.map