"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchDiscussion = fetchDiscussion;
exports.convertItemToDiscussion = convertItemToDiscussion;
const arcgis_rest_portal_1 = require("@esri/arcgis-rest-portal");
const slugs_1 = require("../items/slugs");
const PropertyMapper_1 = require("../core/_internal/PropertyMapper");
const getPropertyMap_1 = require("./_internal/getPropertyMap");
const computeProps_1 = require("./_internal/computeProps");
const getDefaultEntitySettings_1 = require("./api/settings/getDefaultEntitySettings");
const is_guid_1 = require("../utils/is-guid");
const settings_1 = require("./api/settings/settings");
const fetchModelFromItem_1 = require("../models/fetchModelFromItem");
/**
 * @private
 * Get a Discussion by id or slug
 * @param identifier item id or slug
 * @param requestOptions request options
 * @returns a promise that resolves a IHubDiscussion
 */
async function fetchDiscussion(identifier, requestOptions) {
    let getPrms;
    if ((0, is_guid_1.isGuid)(identifier)) {
        // get item by id
        getPrms = (0, arcgis_rest_portal_1.getItem)(identifier, requestOptions);
    }
    else {
        getPrms = (0, slugs_1.getItemBySlug)(identifier, requestOptions);
    }
    return getPrms.then((item) => {
        if (!item)
            return null;
        return convertItemToDiscussion(item, requestOptions);
    });
}
/**
 * @private
 * Convert a Hub Discussion Item into a Hub Discussion, fetching any additional
 * information that may be required
 * @param item the discussion item
 * @param auth request options
 * @returns a promise that resolves a IHubDiscussion
 */
async function convertItemToDiscussion(item, requestOptions) {
    const [model, entitySettings] = await Promise.all([
        (0, fetchModelFromItem_1.fetchModelFromItem)(item, requestOptions),
        (0, settings_1.fetchSettingV2)(Object.assign({ id: item.id }, requestOptions)).catch(() => (Object.assign({ id: null }, (0, getDefaultEntitySettings_1.getDefaultEntitySettings)("discussion")))),
    ]);
    model.entitySettings = entitySettings;
    const mapper = new PropertyMapper_1.PropertyMapper((0, getPropertyMap_1.getPropertyMap)());
    const discussion = mapper.storeToEntity(model, {});
    return (0, computeProps_1.computeProps)(model, discussion, requestOptions);
}
//# sourceMappingURL=fetch.js.map