import { fetchContent } from "./fetchContent";
import { fetchEditableContentEnrichments } from "./_internal/fetchEditableContentEnrichments";
import { normalizeItemType } from "./compose";
import { setProp } from "../objects/set-prop";
import { modelToHubEditableContent } from "./modelToHubEditableContent";
import { fetchSettingV2 } from "../discussions/api/settings/settings";
import { getDefaultEntitySettings } from "../discussions/api/settings/getDefaultEntitySettings";
import { fetchModelFromItem } from "../models/fetchModelFromItem";
/**
 * fetch a content entity by identifier
 * @param identifier
 * @param requestOptions
 * @returns content entity
 */
export 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 fetchContent(identifier, options);
    const editableContentEnrichments = await 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 = normalizeItemType(item);
    setProp("type", type, item);
    return convertItemToContent(item, requestOptions, editableContentEnrichments);
};
/**
 * Convert an Item to an editable Hub Content object,
 * fetch any additional information that may be required
 */
export const convertItemToContent = async (item, requestOptions, enrichments) => {
    const [model, entitySettings] = await Promise.all([
        fetchModelFromItem(item, requestOptions),
        !requestOptions.isPortal
            ? fetchSettingV2(Object.assign({ id: item.id }, requestOptions)).catch(() => (Object.assign({ id: null }, getDefaultEntitySettings("content"))))
            : null,
    ]);
    model.entitySettings = entitySettings;
    const content = modelToHubEditableContent(model, requestOptions, enrichments);
    return content;
};
//# sourceMappingURL=fetchHubContent.js.map