"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.enrichContentSearchResult = enrichContentSearchResult;
exports.enrichImageSearchResult = enrichImageSearchResult;
const _enrichments_1 = require("../items/_enrichments");
const parseInclude_1 = require("../search/_internal/parseInclude");
const get_item_data_url_1 = require("../urls/get-item-data-url");
const get_family_1 = require("./get-family");
const internalContentUtils_1 = require("./_internal/internalContentUtils");
const getRelativeWorkspaceUrl_1 = require("../core/getRelativeWorkspaceUrl");
const get_prop_1 = require("../objects/get-prop");
const get_item_thumbnail_url_1 = require("../resources/get-item-thumbnail-url");
const get_item_home_url_1 = require("../urls/get-item-home-url");
const util_1 = require("../util");
const map_by_1 = require("../utils/map-by");
/**
 * Enrich a generic search result
 * @param item
 * @param includes
 * @param requestOptions
 * @returns
 */
async function enrichContentSearchResult(item, include, requestOptions) {
    // Create the basic structure
    const result = {
        access: item.access,
        id: item.id,
        type: item.type,
        name: item.title,
        owner: item.owner,
        tags: item.tags,
        typeKeywords: item.typeKeywords,
        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",
        },
        location: (0, internalContentUtils_1.deriveLocationFromItem)(item),
        rawResult: item,
    };
    // default includes
    const DEFAULTS = [];
    // Add any type-specific defaults here
    // if (["Map Service", "Feature Service"].includes(item.type)) {
    //   DEFAULTS = ["server.layers.length AS layerCount"];
    // }
    // if (item.type === "Web Map") {
    //   DEFAULTS = ["data.operationalLayers.length AS layerCount"];
    // }
    // 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) {
        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.thumbnail = (0, get_item_thumbnail_url_1.getItemThumbnailUrl)(item, requestOptions);
    result.links.self = (0, get_item_home_url_1.getItemHomeUrl)(result.id, requestOptions);
    result.links.siteRelative = (0, internalContentUtils_1.getHubRelativeUrl)(result.type, result.id, item.typeKeywords);
    result.links.workspaceRelative = (0, getRelativeWorkspaceUrl_1.getRelativeWorkspaceUrl)(result.type, result.id);
    return result;
}
/**
 * Enrich a search result for an Image item
 * @param item
 * @param include
 * @param requestOptions
 * @returns
 */
async function enrichImageSearchResult(item, include, requestOptions) {
    // use all of the same enrichments as content
    const result = await enrichContentSearchResult(item, include, requestOptions);
    // but if we're missing a thumbnail, use the data url as a fallback for search results
    const dataUrl = (0, get_item_data_url_1.getItemDataUrl)(item, requestOptions);
    if (!result.links.thumbnail) {
        result.links.thumbnail = dataUrl;
    }
    return result;
}
//# sourceMappingURL=search.js.map