export const normalizeMediaFileType = (value) => {
    const type = value?.trim().replace(/^\./, "").toLowerCase();
    if (!type)
        return undefined;
    if (type === "jpeg")
        return "jpg";
    if (type === "svg+xml")
        return "svg";
    return type;
};
const getFileTypeFromName = (name) => {
    const match = name?.match(/\.([a-z0-9]{1,10})$/i);
    return normalizeMediaFileType(match?.[1]);
};
const getFileTypeFromMimeType = (mimeType) => {
    const normalized = mimeType?.split(";")[0]?.trim().toLowerCase();
    if (!normalized)
        return undefined;
    const knownTypes = {
        "application/pdf": "pdf",
        "image/jpeg": "jpg",
        "image/svg+xml": "svg",
        "text/plain": "txt",
        "video/quicktime": "mov",
    };
    if (knownTypes[normalized])
        return knownTypes[normalized];
    const subtype = normalized.split("/")[1];
    if (!subtype)
        return undefined;
    return normalizeMediaFileType(subtype.split("+")[0]);
};
const getFileTypeFromUrl = (url) => {
    if (!url)
        return undefined;
    try {
        const parsed = new URL(url, typeof window === "undefined"
            ? "http://localhost"
            : window.location.origin);
        const pathType = getFileTypeFromName(parsed.pathname);
        if (pathType && !["ashx", "aspx"].includes(pathType))
            return pathType;
        const queryFileName = parsed.searchParams.get("file") ||
            parsed.searchParams.get("filename") ||
            parsed.searchParams.get("download");
        return getFileTypeFromName(queryFileName || undefined);
    }
    catch {
        const [path] = url.split(/[?#]/);
        const pathType = getFileTypeFromName(path);
        return pathType && !["ashx", "aspx"].includes(pathType)
            ? pathType
            : undefined;
    }
};
const getFileTypeFromTemplateName = (templateName) => {
    const normalized = templateName?.trim().toLowerCase();
    if (!normalized)
        return undefined;
    const templateTypes = {
        jpeg: "jpg",
        jpg: "jpg",
        png: "png",
        gif: "gif",
        svg: "svg",
        webp: "webp",
        avif: "avif",
        bmp: "bmp",
        bitmap: "bmp",
        pdf: "pdf",
    };
    for (const [templateType, label] of Object.entries(templateTypes)) {
        if (normalized.includes(templateType))
            return label;
    }
    return undefined;
};
const getFileTypeFromTemplateId = (templateId) => {
    const normalized = templateId?.replace(/[{}]/g, "").toLowerCase();
    if (!normalized)
        return undefined;
    const templateTypes = {
        "daf085e8-602e-43a6-8299-038ff171349f": "jpg",
    };
    return templateTypes[normalized];
};
export const getMediaFileTypeLabel = (media) => normalizeMediaFileType(media.extension) ||
    getFileTypeFromMimeType(media.mimeType) ||
    getFileTypeFromName(media.name) ||
    getFileTypeFromUrl(media.downloadUrl) ||
    getFileTypeFromUrl(media.previewUrl) ||
    getFileTypeFromUrl(media.thumbUrl) ||
    getFileTypeFromTemplateName(media.templateName) ||
    getFileTypeFromTemplateId(media.templateId);
//# sourceMappingURL=mediaFileType.js.map