"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.enrichGroupSearchResult = enrichGroupSearchResult;
const enrichments_1 = require("./_internal/enrichments");
const parseInclude_1 = require("../search/_internal/parseInclude");
const util_1 = require("../util");
const computeLinks_1 = require("./_internal/computeLinks");
const map_by_1 = require("../utils/map-by");
const get_prop_1 = require("../objects/get-prop");
/**
 * Enrich a generic search result
 * @param group
 * @param includes
 * @param requestOptions
 * @returns
 */
async function enrichGroupSearchResult(group, include, requestOptions) {
    // Create the basic structure
    const result = {
        access: group.access,
        id: group.id,
        type: "Group",
        name: group.title,
        owner: group.owner,
        summary: group.snippet || group.description,
        createdDate: new Date(group.created),
        createdDateSource: "group.created",
        updatedDate: new Date(group.modified),
        updatedDateSource: "group.modified",
        family: "team",
        links: {
            self: "not-implemented",
            siteRelative: "not-implemented",
            thumbnail: "not-implemented",
        },
        rawResult: group,
    };
    // Informal Enrichments - basically adding type-specific props
    // derived directly from the entity
    result.isSharedUpdate = (group.capabilities || []).includes("updateitemcontrol");
    result.membershipAccess = group.membershipAccess;
    result.isOpenData = !!group.isOpenData;
    // default includes
    const DEFAULTS = [];
    // merge includes
    include = [...DEFAULTS, ...include].filter(util_1.unique);
    // Parse the includes into a valid set of enrichments
    const specs = include.map(parseInclude_1.parseInclude);
    // Extract out the low-level enrichments needed
    const enrichments = (0, map_by_1.mapBy)("enrichment", specs).filter(util_1.unique);
    // fetch the enrichments
    let enriched = {};
    if (enrichments.length) {
        enriched = await (0, enrichments_1.fetchGroupEnrichments)(group, enrichments, requestOptions);
    }
    // map the enriched props onto the result
    specs.forEach((spec) => {
        result[spec.prop] = (0, get_prop_1.getProp)(enriched, spec.path);
    });
    // Handle links
    result.links = (0, computeLinks_1.computeLinks)(group, requestOptions);
    return result;
}
//# sourceMappingURL=enrichGroupSearchResult.js.map