"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDownloadConfigurationDisplayFormats = getDownloadConfigurationDisplayFormats;
const get_prop_1 = require("../../objects/get-prop");
const getDownloadConfiguration_1 = require("../getDownloadConfiguration");
const getCreateReplicaFormats_1 = require("./format-fetchers/getCreateReplicaFormats");
const getPagingJobFormats_1 = require("./format-fetchers/getPagingJobFormats");
const utils_1 = require("./additional-resources/utils");
const hostedServiceUtils_1 = require("../../content/hostedServiceUtils");
/**
 * @private
 * Returns configuration objects for each download format that should be displayed
 * in the editing experience of a content entity. This function should not be used
 * to calculate the download formats that should be present on the live UI.
 * @param entity entity to get download format configurations for
 * @returns download format configurations to display
 */
function getDownloadConfigurationDisplayFormats(entity) {
    const configuration = (0, getDownloadConfiguration_1.getDownloadConfiguration)(entity);
    const flowType = configuration.flowType;
    let formats = configuration.formats;
    // For feature or map services that don't meet the criteria to be downloaded (i.e., no flowType)
    // Product wants to show paging formats as a preview of what _could_ be downloaded should
    // the criteria be met.
    if (!flowType && ["Feature Service", "Map Service"].includes(entity.type)) {
        const pagingFormats = (0, getPagingJobFormats_1.getPagingJobFormats)().map((f) => {
            return {
                key: f.format,
                hidden: false,
            };
        });
        // TODO: Should we just show paging formats as a preview or include additional resources?
        formats = pagingFormats;
    }
    // For main entities of a hosted feature service with extract disabled, we want to display
    // the list of createReplica formats as a preview of what _could_ be downloaded should
    // the extract capability be enabled
    if ((0, hostedServiceUtils_1.isHostedFeatureServiceMainEntity)(entity) &&
        flowType !== "createReplica") {
        const createReplicaFormats = (0, getCreateReplicaFormats_1.getCreateReplicaFormats)(entity).map((f) => {
            return {
                key: f.format,
                hidden: false,
            };
        });
        formats = createReplicaFormats;
    }
    const additionalResources = (0, get_prop_1.getProp)(entity, "extendedProps.additionalResources") || [];
    return formats.map((f) => (0, utils_1.isAdditionalResourceConfiguration)(f)
        ? toAdditionalResourceConfigurationDisplay(f, additionalResources)
        : toDownloadFormatConfigurationDisplay(f));
}
// Converts a download format configuration storage object to a display object
// (i.e., adds appropriate labels, etc.)
function toDownloadFormatConfigurationDisplay(config) {
    return Object.assign({ label: `{{shared.fields.download.format.${config.key}:translate}}` }, config);
}
// Converts an additional resource configuration storage object to a display object
// (i.e., adds appropriate labels, etc.)
function toAdditionalResourceConfigurationDisplay(config, additionalResources) {
    const resourceIndex = (0, utils_1.getAdditionalResourceIndex)(config);
    const { name, isDataSource } = additionalResources[resourceIndex];
    let label;
    // Prefer the name of the additional resource if it exists
    if (name) {
        label = `{{shared.fields.download.additionalResource|resourceName=${encodeURIComponent(name)}:translate}}`;
        // If the additional resource is the data source...
    }
    else if (isDataSource) {
        label = "{{shared.fields.download.dataSourceResource:translate}}";
        // If the additional resource has no name...
    }
    else {
        label = "{{shared.fields.download.noTitleResource:translate}}";
    }
    return Object.assign({ label }, config);
}
//# sourceMappingURL=getDownloadConfigurationDisplayFormats.js.map