"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDownloadsSection = getDownloadsSection;
const uiSchemaRuleEffects_1 = require("../../core/enums/uiSchemaRuleEffects");
const getDownloadFlow_1 = require("../../downloads/_internal/getDownloadFlow");
const hostedServiceUtils_1 = require("../hostedServiceUtils");
const uiSchemaMessageTypes_1 = require("../../core/enums/uiSchemaMessageTypes");
/**
 * @private
 * Constructs the downloads section for an IHubEditableContent entity.
 * Due to numerous product requirements, the content and state of this section
 * can vary greatly depending on the entity type and configuration.
 *
 * @param i18nScope translation scope
 * @param entity entity to get the downloads section for
 * @returns ui schema elements for the downloads section
 */
function getDownloadsSection(i18nScope, entity) {
    const downloadSectionElements = [];
    if (shouldShowDownloadSystemElement(entity)) {
        const downloadSystemElement = getDownloadSystemElement(i18nScope);
        downloadSectionElements.push(downloadSystemElement);
    }
    const downloadFormatsElement = getDownloadFormatsElement(i18nScope, entity);
    downloadSectionElements.push(downloadFormatsElement);
    return {
        type: "Section",
        labelKey: `${i18nScope}.sections.downloads.label`,
        options: {},
        elements: downloadSectionElements,
    };
}
/**
 * NOTE: we only show the download system toggle for main entities of a hosted feature service
 * since we can guarantee that the user will have the necessary permissions to enable
 * extract capabilities on the service.
 */
function shouldShowDownloadSystemElement(entity) {
    return (0, hostedServiceUtils_1.isHostedFeatureServiceMainEntity)(entity);
}
function getDownloadSystemElement(i18nScope) {
    return {
        labelKey: `${i18nScope}.fields.serverExtractCapability.label`,
        scope: "/properties/serverExtractCapability",
        type: "Control",
        options: {
            control: "hub-field-input-tile-select",
            labels: [
                `{{${i18nScope}.fields.serverExtractCapability.exportDataSetting.label:translate}}`,
                `{{${i18nScope}.fields.serverExtractCapability.defaultDownloadsSystem.label:translate}}`,
            ],
            descriptions: [
                `{{${i18nScope}.fields.serverExtractCapability.exportDataSetting.description:translate}}`,
                `{{${i18nScope}.fields.serverExtractCapability.defaultDownloadsSystem.description:translate}}`,
            ],
            layout: "vertical",
            messages: [
                {
                    type: uiSchemaMessageTypes_1.UiSchemaMessageTypes.custom,
                    display: "notice",
                    kind: "warning",
                    icon: "exclamation-mark-triangle",
                    titleKey: `${i18nScope}.fields.serverExtractCapability.noFormatConfigurationNotice.title`,
                    labelKey: `${i18nScope}.fields.serverExtractCapability.noFormatConfigurationNotice.body`,
                    allowShowBeforeInteract: true,
                    conditions: [
                        {
                            scope: "/properties/serverExtractCapability",
                            schema: {
                                const: false,
                            },
                        },
                    ],
                },
            ],
        },
    };
}
/**
 * Returns the ui schema element for the download formats field. Populating this field
 * is a complex process that depends on the entity type, configuration, and product requirements.
 */
function getDownloadFormatsElement(i18nScope, entity) {
    const result = {
        labelKey: `${i18nScope}.fields.downloadFormats.label`,
        scope: "/properties/downloadFormats",
        type: "Control",
        options: {
            control: "hub-field-input-list",
            helperText: {
                labelKey: `${i18nScope}.fields.downloadFormats.helperText`,
            },
            allowReorder: true,
            allowHide: true,
        },
        rules: [],
    };
    // Product has asked that if the extract capability toggle is present, we should disable
    // the download formats control when the toggle is off. We hope this will encourage more
    // users to opt into the hosted downloads experience.
    if (shouldShowDownloadSystemElement(entity)) {
        result.rules.push({
            effect: uiSchemaRuleEffects_1.UiSchemaRuleEffects.DISABLE,
            conditions: [
                {
                    scope: "/properties/serverExtractCapability",
                    schema: {
                        const: false,
                    },
                },
            ],
        });
        // Product had us add this branch in to encourage users to make their content / service downloadable.
        // This branch should run when the entity represents a service that cannot be downloaded and extract
        // cannot be enabled. The control will list all the formats that _could_ be available if the user
        // were to make the necessary changes
    }
    else if (shouldDisableDownloadFormatsControl(entity)) {
        result.rules.push({
            effect: uiSchemaRuleEffects_1.UiSchemaRuleEffects.DISABLE,
            conditions: [true],
        });
        result.options.messages = [
            {
                type: uiSchemaMessageTypes_1.UiSchemaMessageTypes.custom,
                display: "notice",
                kind: "warning",
                icon: "exclamation-mark-triangle",
                titleKey: `${i18nScope}.fields.downloadFormats.downloadsUnavailableNotice.title`,
                labelKey: `${i18nScope}.fields.downloadFormats.downloadsUnavailableNotice.body`,
                allowShowBeforeInteract: true,
                alwaysShow: true,
            },
        ];
    }
    return result;
}
/**
 * Returns true when an entity represents a item/service that
 * cannot be downloaded with its current configuration.
 */
function shouldDisableDownloadFormatsControl(entity) {
    const downloadFlow = (0, getDownloadFlow_1.getDownloadFlow)(entity);
    return !downloadFlow;
}
//# sourceMappingURL=getDownloadsSection.js.map