"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertItemToContent = exports.fetchHubContent = void 0;
const fetchContent_1 = require("./fetchContent");
const fetchEditableContentEnrichments_1 = require("./_internal/fetchEditableContentEnrichments");
const compose_1 = require("./compose");
const set_prop_1 = require("../objects/set-prop");
const modelToHubEditableContent_1 = require("./modelToHubEditableContent");
const settings_1 = require("../discussions/api/settings/settings");
const getDefaultEntitySettings_1 = require("../discussions/api/settings/getDefaultEntitySettings");
const fetchModelFromItem_1 = require("../models/fetchModelFromItem");
/**
 * fetch a content entity by identifier
 * @param identifier
 * @param requestOptions
 * @returns content entity
 */
const fetchHubContent = async (identifier, requestOptions, enrichments) => {
    // NOTE: b/c we have to support slugs, we use fetchContent() to get the item
    // by telling it to not fetch any enrichments. We then can fetch enrichments
    // as needed after we have the item
    const options = Object.assign(Object.assign({}, requestOptions), { enrichments: [] });
    const { item } = await (0, fetchContent_1.fetchContent)(identifier, options);
    const editableContentEnrichments = await (0, fetchEditableContentEnrichments_1.fetchEditableContentEnrichments)(item, requestOptions, enrichments);
    // we must normalize the underlying item type to account
    // for older items (e.g. sites that are type "Web Mapping
    // Application") before we map the model to a Hub Entity
    const type = (0, compose_1.normalizeItemType)(item);
    (0, set_prop_1.setProp)("type", type, item);
    return (0, exports.convertItemToContent)(item, requestOptions, editableContentEnrichments);
};
exports.fetchHubContent = fetchHubContent;
/**
 * Convert an Item to an editable Hub Content object,
 * fetch any additional information that may be required
 */
const convertItemToContent = async (item, requestOptions, enrichments) => {
    const [model, entitySettings] = await Promise.all([
        (0, fetchModelFromItem_1.fetchModelFromItem)(item, requestOptions),
        !requestOptions.isPortal
            ? (0, settings_1.fetchSettingV2)(Object.assign({ id: item.id }, requestOptions)).catch(() => (Object.assign({ id: null }, (0, getDefaultEntitySettings_1.getDefaultEntitySettings)("content"))))
            : null,
    ]);
    model.entitySettings = entitySettings;
    const content = (0, modelToHubEditableContent_1.modelToHubEditableContent)(model, requestOptions, enrichments);
    return content;
};
exports.convertItemToContent = convertItemToContent;
//# sourceMappingURL=fetchHubContent.js.map