"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.resolveOpenGraph = resolveOpenGraph;
exports.resolveTwitter = void 0;
var _utils = require("../generate/utils");
var _resolveUrl = require("./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;
    }
}
function resolveOpenGraph(openGraph, metadataBase) {
    if (!openGraph) return null;
    const url = (0, _resolveUrl).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 = (0, _utils).resolveAsArrayOrUndefined(value);
                    resolved[key] = arrayValue;
                }
            }
        }
    }
    assignProps(openGraph);
    resolved.url = url;
    return resolved;
}
const TwitterBasicInfoKeys = [
    "site",
    "siteId",
    "creator",
    "creatorId",
    "description", 
];
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 = (0, _utils).resolveAsArrayOrUndefined(twitter.images)) == null ? void 0 : ref.map((item)=>{
        if ((0, _resolveUrl).isStringOrURL(item)) return {
            url: (0, _resolveUrl).resolveUrl(item, metadataBase)
        };
        else {
            return {
                url: (0, _resolveUrl).resolveUrl(item.url, metadataBase),
                alt: item.alt
            };
        }
    });
    if ("card" in twitter) {
        resolved.card = twitter.card;
        switch(twitter.card){
            case "player":
                {
                    // @ts-ignore
                    resolved.players = (0, _utils).resolveAsArrayOrUndefined(twitter.players) || [];
                    break;
                }
            case "app":
                {
                    // @ts-ignore
                    resolved.app = twitter.app || {};
                    break;
                }
            default:
                break;
        }
    } else {
        resolved.card = "summary";
    }
    return resolved;
};
exports.resolveTwitter = resolveTwitter;

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