"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.templateResultToCardModel = exports.templateToCardModel = void 0;
const get_family_1 = require("../content/get-family");
const internalContentUtils_1 = require("../content/_internal/internalContentUtils");
const getCardModelUrl_1 = require("../urls/getCardModelUrl");
/**
 * Convert a template entity into a card view model that can
 * be consumed by the suite of hub gallery components
 *
 * @param template template entity
 * @param context auth & portal information
 * @param opts view model options
 */
const templateToCardModel = (template, context, opts) => {
    const { actionLinks = [], baseUrl = "", locale = "en-US", target = "self", } = opts || {};
    const titleUrl = (0, getCardModelUrl_1.getCardModelUrlFromEntity)(template, context, target, baseUrl);
    return Object.assign(Object.assign(Object.assign({}, getSharedTemplateCardModel(template, locale)), { actionLinks,
        titleUrl }), (template.thumbnailUrl && { thumbnailUrl: template.thumbnailUrl }));
};
exports.templateToCardModel = templateToCardModel;
/**
 * Convert a template hub search result into a card view model
 * that can be consumed by the suite of hub gallery components
 *
 * @param searchResult hub template search result
 * @param opts view model options
 */
const templateResultToCardModel = (searchResult, opts) => {
    const { actionLinks = [], baseUrl = "", locale = "en-US", target = "self", } = opts || {};
    const titleUrl = (0, getCardModelUrl_1.getCardModelUrlFromResult)(searchResult, target, baseUrl);
    return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, getSharedTemplateCardModel(searchResult, locale)), { actionLinks }), (!isNaN(searchResult.index) && { index: searchResult.index })), { titleUrl }), (searchResult.links.thumbnail && {
        thumbnailUrl: searchResult.links.thumbnail,
    }));
};
exports.templateResultToCardModel = templateResultToCardModel;
/**
 * Given a template entity OR hub serach result, construct the
 * template's shared card view model properties
 *
 * @param entityOrSearchResult template entity or hub search result
 * @param locale internationalization locale
 */
const getSharedTemplateCardModel = (entityOrSearchResult, locale) => {
    var _a, _b;
    const additionalInfo = [
        {
            i18nKey: "type",
            value: entityOrSearchResult.type,
        },
        {
            i18nKey: "dateUpdated",
            value: entityOrSearchResult.updatedDate.toLocaleDateString(locale),
        },
        ...(((_a = entityOrSearchResult.tags) === null || _a === void 0 ? void 0 : _a.length)
            ? [
                {
                    i18nKey: "tags",
                    value: entityOrSearchResult.tags.join(", "),
                },
            ]
            : []),
        ...(((_b = entityOrSearchResult.categories) === null || _b === void 0 ? void 0 : _b.length)
            ? [
                {
                    i18nKey: "categories",
                    value: (0, internalContentUtils_1.getShortenedCategories)(entityOrSearchResult.categories).join(", "),
                },
            ]
            : []),
        {
            i18nKey: "dateCreated",
            value: entityOrSearchResult.createdDate.toLocaleDateString(locale),
        },
    ];
    return {
        access: entityOrSearchResult.access,
        badges: [],
        id: entityOrSearchResult.id,
        family: (0, get_family_1.getFamily)(entityOrSearchResult.type),
        source: entityOrSearchResult.owner,
        summary: entityOrSearchResult.summary,
        title: entityOrSearchResult.name,
        type: entityOrSearchResult.type,
        additionalInfo,
    };
};
//# sourceMappingURL=view.js.map