"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchTemplate = fetchTemplate;
exports.convertItemToTemplate = convertItemToTemplate;
exports.enrichTemplateSearchResult = enrichTemplateSearchResult;
const is_guid_1 = require("../utils/is-guid");
const map_by_1 = require("../utils/map-by");
const arcgis_rest_portal_1 = require("@esri/arcgis-rest-portal");
const slugs_1 = require("../items/slugs");
const fetchModelFromItem_1 = require("../models/fetchModelFromItem");
const PropertyMapper_1 = require("../core/_internal/PropertyMapper");
const getPropertyMap_1 = require("./_internal/getPropertyMap");
const computeProps_1 = require("./_internal/computeProps");
const get_family_1 = require("../content/get-family");
const util_1 = require("../util");
const parseInclude_1 = require("../search/_internal/parseInclude");
const _enrichments_1 = require("../items/_enrichments");
const get_prop_1 = require("../objects/get-prop");
const computeLinks_1 = require("./_internal/computeLinks");
const utils_1 = require("./utils");
const internalContentUtils_1 = require("../content/_internal/internalContentUtils");
/**
 * @private
 * Fetch a Hub Template backing item by id or slug
 * @param identifier item id or slug
 * @param requestOptions
 */
async function fetchTemplate(identifier, requestOptions) {
    let getPrms;
    if ((0, is_guid_1.isGuid)(identifier)) {
        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 convertItemToTemplate(item, requestOptions);
    });
}
/**
 * @private
 * Convert a Solution Item into a Hub Template, fetching
 * any additional information that may be required
 * @param item
 * @param auth
 */
async function convertItemToTemplate(item, requestOptions) {
    const model = await (0, fetchModelFromItem_1.fetchModelFromItem)(item, requestOptions);
    // 1. Create a property mapper between the the template
    // object and item model
    const mapper = new PropertyMapper_1.PropertyMapper((0, getPropertyMap_1.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 (0, computeProps_1.computeProps)(model, template, requestOptions);
}
/**
 * @private
 * Fetch template specific enrichments
 * @param item
 * @param include
 * @param requestOptions
 */
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: (0, get_family_1.getFamily)(item.type),
        links: {
            self: "not-implemented",
            siteRelative: "not-implemented",
            thumbnail: "not-implemented",
            workspaceRelative: "not-implemented",
        },
        location: (0, internalContentUtils_1.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(util_1.unique);
    // Parse the includes into a valid set of enrichments
    const specs = include.map(parseInclude_1.parseInclude);
    // Extract out the low-level enrichments needed
    const enrichments = (0, map_by_1.mapBy)("enrichment", specs).filter(util_1.unique);
    // fetch the enrichments
    let enriched = {};
    if (enrichments.length) {
        enriched = await (0, _enrichments_1.fetchItemEnrichments)(item, enrichments, requestOptions);
    }
    // map the enriched props onto the result
    specs.forEach((spec) => {
        result[spec.prop] = (0, get_prop_1.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 = (0, computeLinks_1.computeLinks)(item, requestOptions);
    // 3. append additional template-specific properties
    result.deployedType = (0, utils_1.getDeployedTemplateType)(item);
    return result;
}
//# sourceMappingURL=fetch.js.map