import { isGuid } from "../utils/is-guid";
import { mapBy } from "../utils/map-by";
import { getItem } from "@esri/arcgis-rest-portal";
import { getItemBySlug } from "../items/slugs";
import { fetchModelFromItem } from "../models/fetchModelFromItem";
import { PropertyMapper } from "../core/_internal/PropertyMapper";
import { getPropertyMap } from "./_internal/getPropertyMap";
import { computeProps } from "./_internal/computeProps";
import { getFamily } from "../content/get-family";
import { unique } from "../util";
import { parseInclude } from "../search/_internal/parseInclude";
import { fetchItemEnrichments } from "../items/_enrichments";
import { getProp } from "../objects/get-prop";
import { computeLinks } from "./_internal/computeLinks";
import { getDeployedTemplateType } from "./utils";
import { deriveLocationFromItem } from "../content/_internal/internalContentUtils";
/**
 * @private
 * Fetch a Hub Template backing item by id or slug
 * @param identifier item id or slug
 * @param requestOptions
 */
export async function fetchTemplate(identifier, requestOptions) {
    let getPrms;
    if (isGuid(identifier)) {
        getPrms = getItem(identifier, requestOptions);
    }
    else {
        getPrms = getItemBySlug(identifier, requestOptions);
    }
    return getPrms.then((item) => {
        if (!item)
            return null;
        return convertItemToTemplate(item, requestOptions);
    });
}
/**
 * @private
 * Convert a Solution Item into a Hub Template, fetching
 * any additional information that may be required
 * @param item
 * @param auth
 */
export async function convertItemToTemplate(item, requestOptions) {
    const model = await fetchModelFromItem(item, requestOptions);
    // 1. Create a property mapper between the the template
    // object and item model
    const mapper = new PropertyMapper(getPropertyMap());
    // 2. Map the item into an IHubTemplate
    const template = mapper.storeToEntity(model, {});
    // 3. Compute + set various properties on the IHubTemplate
    // that cannot be directly mapped from the item
    return computeProps(model, template, requestOptions);
}
/**
 * @private
 * Fetch template specific enrichments
 * @param item
 * @param include
 * @param requestOptions
 */
export async function enrichTemplateSearchResult(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,
    };
    // 1. optionally enrich the template item with
    // well-known item enrichments
    const DEFAULTS = [];
    // merge default and provided "include" enrichments
    include = [...DEFAULTS, ...include].filter(unique);
    // Parse the includes into a valid set of enrichments
    const specs = include.map(parseInclude);
    // Extract out the low-level enrichments needed
    const enrichments = mapBy("enrichment", specs).filter(unique);
    // fetch the enrichments
    let enriched = {};
    if (enrichments.length) {
        enriched = await fetchItemEnrichments(item, enrichments, requestOptions);
    }
    // map the enriched props onto the result
    specs.forEach((spec) => {
        result[spec.prop] = getProp(enriched, spec.path);
    });
    // 2. append relevant links onto the search result - these
    // are the same links that get appended to the template entity
    result.links = computeLinks(item, requestOptions);
    // 3. append additional template-specific properties
    result.deployedType = getDeployedTemplateType(item);
    return result;
}
//# sourceMappingURL=fetch.js.map