"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchEditableContentEnrichments = fetchEditableContentEnrichments;
const _enrichments_1 = require("../../items/_enrichments");
const is_service_1 = require("../../resources/is-service");
const fetchItemScheduleEnrichment_1 = require("./fetchItemScheduleEnrichment");
/**
 * @private
 *
 * Fetches the enrichments for a content item to be converted into an IHubEditableContent object.
 * If no enrichment keys are provided, the default enrichments will be fetched. Default enrichments
 * vary by item type.
 *
 * @param item item to fetch enrichments for
 * @param requestOptions
 * @param enrichments optional override for the enrichments to fetch
 * @returns a hash of enrichments
 */
async function fetchEditableContentEnrichments(item, requestOptions, enrichments) {
    const result = {};
    if (!enrichments) {
        enrichments = getDefaultEnrichmentKeys(item);
    }
    // NOTE: Enrichments for IHubEditableContent can be fetched one of two ways:
    //
    // 1: Via `fetchItemEnrichments()`. This was the old way that we used to fetch enrichments
    //    in the `fetchContent()` era. Since the code still works and has great error handling,
    //    there's no reason to re-invent the wheel.
    // 2. Via ad-hoc fetch functions. We typically use this for new enrichments that are not
    //   supported by `fetchItemEnrichments()` (e.g. schedule)
    //
    // Eventually we'll want to move all new enrichments to the `fetchItemEnrichments()` subsystem,
    // but before we do we'll need to evaluate any impacts that would have on existing code, since
    // `fetchContent()` is still widely used in the codebase.
    const adHocEnrichments = ["schedule"];
    const fetchItemEnrichmentKeys = enrichments.filter((e) => !adHocEnrichments.includes(e));
    if (fetchItemEnrichmentKeys.length) {
        // TODO: Abstract this into a helper function that can be used by enrichContentSearchResult()
        const itemOrServerEnrichments = await (0, _enrichments_1.fetchItemEnrichments)(item, fetchItemEnrichmentKeys, requestOptions);
        fetchItemEnrichmentKeys.forEach((key) => {
            result[key] = itemOrServerEnrichments[key];
        });
    }
    // Fetch the schedule separately if it's requested
    // TODO: should we add scheduling to the fetchItemEnrichments() subsystem?
    if (enrichments.includes("schedule")) {
        result.schedule = await (0, fetchItemScheduleEnrichment_1.fetchItemScheduleEnrichment)(item, requestOptions);
    }
    return result;
}
function getDefaultEnrichmentKeys(item) {
    const enrichments = ["metadata", "schedule"];
    if ((0, is_service_1.isService)(item.url)) {
        enrichments.push("server");
    }
    return enrichments;
}
//# sourceMappingURL=fetchEditableContentEnrichments.js.map