import { getItem } from "@esri/arcgis-rest-portal";
import { getFamily } from "../content/get-family";
import { deriveLocationFromItem } from "../content/_internal/internalContentUtils";
import { PropertyMapper } from "../core/_internal/PropertyMapper";
import { getItemBySlug } from "../items/slugs";
import { computeProps } from "./_internal/computeProps";
import { getPropertyMap } from "./_internal/getPropertyMap";
import { computeLinks } from "./_internal/computeLinks";
import { fetchModelFromItem } from "../models/fetchModelFromItem";
import { isGuid } from "../utils/is-guid";
export async function fetchInitiativeTemplate(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 convertItemToInitiativeTemplate(item, requestOptions);
    });
}
/**
 * @private
 * Convert a Hub Initiative Template Item into a Hub Initiative Template, feching any additional
 * information that may be required
 * @param item
 * @param requestOptions
 * @returns
 */
export async function convertItemToInitiativeTemplate(item, requestOptions) {
    const model = await fetchModelFromItem(item, requestOptions);
    const mapper = new PropertyMapper(getPropertyMap());
    const it = mapper.storeToEntity(model, {});
    return computeProps(model, it, requestOptions);
}
export function enrichInitiativeTemplateSearchResult(item, include, requestOptions) {
    const result = {
        access: item.access,
        id: item.id,
        type: item.type,
        name: item.title,
        owner: item.owner,
        typeKeywords: item.typeKeywords,
        tags: item.tags,
        categories: item.categories,
        summary: item.snippet || item.description,
        createdDate: new Date(item.created),
        createdDateSource: "item.created",
        updatedDate: new Date(item.modified),
        updatedDateSource: "item.modified",
        family: getFamily(item.type),
        links: {
            self: "not-implemented",
            siteRelative: "not-implemented",
            thumbnail: "not-implemented",
            workspaceRelative: "not-implemented",
        },
        location: deriveLocationFromItem(item),
        rawResult: item,
    };
    // TODO: reimplement enrichment fetching when we know what enrichments we're looking for
    // Handle links
    // TODO: links should be an enrichment
    result.links = computeLinks(item, requestOptions);
    return result;
}
//# sourceMappingURL=fetch.js.map