"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchProject = fetchProject;
exports.convertItemToProject = convertItemToProject;
exports.enrichProjectSearchResult = enrichProjectSearchResult;
const arcgis_rest_portal_1 = require("@esri/arcgis-rest-portal");
const get_family_1 = require("../content/get-family");
const PropertyMapper_1 = require("../core/_internal/PropertyMapper");
const slugs_1 = require("../items/slugs");
const _enrichments_1 = require("../items/_enrichments");
const parseInclude_1 = require("../search/_internal/parseInclude");
const computeProps_1 = require("./_internal/computeProps");
const getPropertyMap_1 = require("./_internal/getPropertyMap");
const util_1 = require("../util");
const get_prop_1 = require("../objects/get-prop");
const computeLinks_1 = require("./_internal/computeLinks");
const internalContentUtils_1 = require("../content/_internal/internalContentUtils");
const fetchModelFromItem_1 = require("../models/fetchModelFromItem");
const is_guid_1 = require("../utils/is-guid");
const map_by_1 = require("../utils/map-by");
/**
 * @private
 * Get a Hub Project by id or slug
 * @param identifier item id or slug
 * @param requestOptions
 */
async function fetchProject(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 convertItemToProject(item, requestOptions);
    });
}
/**
 * @private
 * Convert an Hub Project Item into a Hub Project, fetching any additional
 * information that may be required
 * @param item
 * @param auth
 * @returns
 */
async function convertItemToProject(item, requestOptions) {
    const model = await (0, fetchModelFromItem_1.fetchModelFromItem)(item, requestOptions);
    // TODO: In the future we will handle the boundary fetching from resource
    const mapper = new PropertyMapper_1.PropertyMapper((0, getPropertyMap_1.getPropertyMap)());
    const prj = mapper.storeToEntity(model, {});
    return (0, computeProps_1.computeProps)(model, prj, requestOptions);
}
/**
 * @private
 * Fetch project specific enrichments
 * @param item
 * @param include
 * @param requestOptions
 * @returns
 */
async function enrichProjectSearchResult(item, include, requestOptions) {
    // Create the basic structure
    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,
    };
    // default includes
    const DEFAULTS = [];
    // merge includes
    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) {
        // TODO: Look into caching for the requests in fetchItemEnrichments
        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);
    });
    // Handle links
    // TODO: Link handling should be an enrichment
    result.links = (0, computeLinks_1.computeLinks)(item, requestOptions);
    return result;
}
//# sourceMappingURL=fetch.js.map