"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchInitiativeTemplate = fetchInitiativeTemplate;
exports.convertItemToInitiativeTemplate = convertItemToInitiativeTemplate;
exports.enrichInitiativeTemplateSearchResult = enrichInitiativeTemplateSearchResult;
const arcgis_rest_portal_1 = require("@esri/arcgis-rest-portal");
const get_family_1 = require("../content/get-family");
const internalContentUtils_1 = require("../content/_internal/internalContentUtils");
const PropertyMapper_1 = require("../core/_internal/PropertyMapper");
const slugs_1 = require("../items/slugs");
const computeProps_1 = require("./_internal/computeProps");
const getPropertyMap_1 = require("./_internal/getPropertyMap");
const computeLinks_1 = require("./_internal/computeLinks");
const fetchModelFromItem_1 = require("../models/fetchModelFromItem");
const is_guid_1 = require("../utils/is-guid");
async function fetchInitiativeTemplate(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 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
 */
async function convertItemToInitiativeTemplate(item, requestOptions) {
    const model = await (0, fetchModelFromItem_1.fetchModelFromItem)(item, requestOptions);
    const mapper = new PropertyMapper_1.PropertyMapper((0, getPropertyMap_1.getPropertyMap)());
    const it = mapper.storeToEntity(model, {});
    return (0, computeProps_1.computeProps)(model, it, requestOptions);
}
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: (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,
    };
    // TODO: reimplement enrichment fetching when we know what enrichments we're looking for
    // Handle links
    // TODO: links should be an enrichment
    result.links = (0, computeLinks_1.computeLinks)(item, requestOptions);
    return result;
}
//# sourceMappingURL=fetch.js.map