"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.computeProps = computeProps;
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 internalContentUtils_1 = require("./internalContentUtils");
const getRelativeWorkspaceUrl_1 = require("../../core/getRelativeWorkspaceUrl");
const hostedServiceUtils_1 = require("../hostedServiceUtils");
const serviceCapabilities_1 = require("../enums/serviceCapabilities");
const computeItemProps_1 = require("../../core/_internal/computeItemProps");
const get_prop_1 = require("../../objects/get-prop");
const is_service_1 = require("../../resources/is-service");
function computeProps(model, content, requestOptions, enrichments = {}) {
    let token;
    if (requestOptions.authentication) {
        const session = requestOptions.authentication;
        token = session.token;
    }
    // compute base properties on content
    content = (0, computeItemProps_1.computeItemProps)(model.item, content);
    // thumbnail url
    const thumbnailUrl = (0, get_item_thumbnail_url_1.getItemThumbnailUrl)(model.item, requestOptions, token);
    content.thumbnailUrl = thumbnailUrl;
    // NOTE: other entities encapsulate this in a computeLinks function
    content.links = {
        self: (0, get_item_home_url_1.getItemHomeUrl)(content.id, requestOptions),
        siteRelative: (0, internalContentUtils_1.getHubRelativeUrl)(content.type, content.slug || content.id, content.typeKeywords),
        siteRelativeEntityType: (0, internalContentUtils_1.getHubRelativeUrl)(content.type),
        workspaceRelative: (0, getRelativeWorkspaceUrl_1.getRelativeWorkspaceUrl)("content", content.id),
        thumbnail: thumbnailUrl,
        contentEditUrl: (0, internalContentUtils_1.getContentEditUrl)(model.item, requestOptions),
    };
    // cannot be null otherwise we'd get a validation
    // error that doesn't let us save the form
    content.licenseInfo = model.item.licenseInfo || "";
    // when we receive a schedule from the enrichments, we want to use it, otherwise default to automatic
    content.schedule = enrichments.schedule;
    // calculate extendedProps
    content.extendedProps = (0, is_service_1.isService)(content.url)
        ? getServiceExtendedProps(model.item, enrichments, requestOptions)
        : getContentExtendedProps(model.item, enrichments, requestOptions);
    // TODO: Remove once .serverQueryCapability, .serverExtractCapability, and .serverExtractFormats are removed
    if (enrichments.server) {
        content.serverQueryCapability = (0, hostedServiceUtils_1.hasServiceCapability)(serviceCapabilities_1.ServiceCapabilities.QUERY, enrichments.server);
        content.serverExtractCapability = (0, hostedServiceUtils_1.hasServiceCapability)(serviceCapabilities_1.ServiceCapabilities.EXTRACT, enrichments.server);
        const extractFormatsList = (0, get_prop_1.getProp)(enrichments, "server.supportedExportFormats");
        content.serverExtractFormats =
            extractFormatsList && extractFormatsList.split(",");
    }
    // TODO: remove once .additionalResources is removed
    if (enrichments.metadata) {
        content.additionalResources = (0, internalContentUtils_1.getAdditionalResources)(model.item, enrichments.metadata, requestOptions);
    }
    return content;
}
/**
 * @private
 *
 * Compute the extended props for a service-backed item (i.e., feature, map, or image service)
 *
 * @param item
 * @param enrichments
 * @param requestOptions
 * @returns extended props for a service-backed item
 */
function getServiceExtendedProps(item, enrichments, requestOptions) {
    const baseProps = getBaseExtendedProps(item, enrichments, requestOptions);
    const result = Object.assign(Object.assign({}, baseProps), { kind: "service" });
    if (enrichments.server) {
        result.server = enrichments.server;
        result.serverQueryCapability = (0, hostedServiceUtils_1.hasServiceCapability)(serviceCapabilities_1.ServiceCapabilities.QUERY, enrichments.server);
        result.serverExtractCapability = (0, hostedServiceUtils_1.hasServiceCapability)(serviceCapabilities_1.ServiceCapabilities.EXTRACT, enrichments.server);
        const extractFormatsList = (0, get_prop_1.getProp)(enrichments, "server.supportedExportFormats");
        result.serverExtractFormats =
            extractFormatsList && extractFormatsList.split(",");
    }
    return result;
}
/**
 * @private
 *
 * Compute the extended props for content items not backed by a service
 *
 * @param item
 * @param enrichments
 * @param requestOptions
 * @returns
 */
function getContentExtendedProps(item, enrichments, requestOptions) {
    const baseProps = getBaseExtendedProps(item, enrichments, requestOptions);
    return Object.assign(Object.assign({}, baseProps), { kind: "content" });
}
/**
 * @private
 *
 * Compute the extended props common to all content items.
 *
 * @param item
 * @param enrichments
 * @param requestOptions
 * @returns
 */
function getBaseExtendedProps(item, enrichments, requestOptions) {
    return {
        kind: null, // To be populated by the specific extended props function
        metadata: enrichments.metadata,
        additionalResources: (0, internalContentUtils_1.getAdditionalResources)(item, enrichments.metadata, requestOptions),
        downloads: (0, get_prop_1.getProp)(item, "properties.downloads"),
    };
}
//# sourceMappingURL=computeProps.js.map