import { resolveAsArrayOrUndefined } from "../generate/utils";
import { isStringOrURL, resolveUrl } from "./resolve-url";
const OgTypFields = {
    article: [
        "authors",
        "tags"
    ],
    song: [
        "albums",
        "musicians"
    ],
    playlist: [
        "albums",
        "musicians"
    ],
    radio: [
        "creators"
    ],
    video: [
        "actors",
        "directors",
        "writers",
        "tags"
    ],
    basic: [
        "emails",
        "phoneNumbers",
        "faxNumbers",
        "alternateLocale",
        "images",
        "audio",
        "videos", 
    ]
};
function getFieldsByOgType(ogType) {
    switch(ogType){
        case "article":
        case "book":
            return OgTypFields.article;
        case "music.song":
        case "music.album":
            return OgTypFields.song;
        case "music.playlist":
            return OgTypFields.playlist;
        case "music.radio_station":
            return OgTypFields.radio;
        case "video.movie":
        case "video.episode":
            return OgTypFields.video;
        default:
            return OgTypFields.basic;
    }
}
export function resolveOpenGraph(openGraph, metadataBase) {
    if (!openGraph) return null;
    const url = resolveUrl(openGraph.url, metadataBase);
    const resolved = {
        ...openGraph
    };
    function assignProps(og) {
        const ogType = og && "type" in og ? og.type : undefined;
        const keys = getFieldsByOgType(ogType);
        for (const k of keys){
            const key = k;
            if (key in og && key !== "url") {
                const value = og[key];
                if (value) {
                    const arrayValue = resolveAsArrayOrUndefined(value);
                    resolved[key] = arrayValue;
                }
            }
        }
    }
    assignProps(openGraph);
    resolved.url = url;
    return resolved;
}
const TwitterBasicInfoKeys = [
    "site",
    "siteId",
    "creator",
    "creatorId",
    "description", 
];
export const resolveTwitter = (twitter, metadataBase)=>{
    var ref;
    if (!twitter) return null;
    const resolved = {
        title: twitter.title
    };
    for (const infoKey of TwitterBasicInfoKeys){
        resolved[infoKey] = twitter[infoKey] || null;
    }
    resolved.images = (ref = resolveAsArrayOrUndefined(twitter.images)) == null ? void 0 : ref.map((item)=>{
        if (isStringOrURL(item)) return {
            url: metadataBase ? resolveUrl(item, metadataBase) : item
        };
        else {
            return {
                url: metadataBase ? resolveUrl(item.url, metadataBase) : item.url,
                alt: item.alt
            };
        }
    });
    if ("card" in twitter) {
        resolved.card = twitter.card;
        switch(twitter.card){
            case "player":
                {
                    // @ts-ignore
                    resolved.players = resolveAsArrayOrUndefined(twitter.players) || [];
                    break;
                }
            case "app":
                {
                    // @ts-ignore
                    resolved.app = twitter.app || {};
                    break;
                }
            default:
                break;
        }
    } else {
        resolved.card = "summary";
    }
    return resolved;
};

//# sourceMappingURL=resolve-opengraph.js.map