import { getItem } from "@esri/arcgis-rest-portal";
import { getItemBySlug } from "../items/slugs";
import { PropertyMapper } from "../core/_internal/PropertyMapper";
import { getPropertyMap } from "./_internal/getPropertyMap";
import { computeProps } from "./_internal/computeProps";
import { getDefaultEntitySettings } from "./api/settings/getDefaultEntitySettings";
import { isGuid } from "../utils/is-guid";
import { fetchSettingV2 } from "./api/settings/settings";
import { fetchModelFromItem } from "../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
 */
export async function fetchDiscussion(identifier, requestOptions) {
    let getPrms;
    if (isGuid(identifier)) {
        // get item by id
        getPrms = getItem(identifier, requestOptions);
    }
    else {
        getPrms = 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
 */
export async function convertItemToDiscussion(item, requestOptions) {
    const [model, entitySettings] = await Promise.all([
        fetchModelFromItem(item, requestOptions),
        fetchSettingV2(Object.assign({ id: item.id }, requestOptions)).catch(() => (Object.assign({ id: null }, getDefaultEntitySettings("discussion")))),
    ]);
    model.entitySettings = entitySettings;
    const mapper = new PropertyMapper(getPropertyMap());
    const discussion = mapper.storeToEntity(model, {});
    return computeProps(model, discussion, requestOptions);
}
//# sourceMappingURL=fetch.js.map