"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDownloadFormats = getDownloadFormats;
const get_prop_1 = require("../objects/get-prop");
const canUseExportImageFlow_1 = require("./_internal/canUseExportImageFlow");
const getExportImageFormats_1 = require("./_internal/format-fetchers/getExportImageFormats");
const utils_1 = require("./_internal/additional-resources/utils");
const canUseHubDownloadApi_1 = require("./canUseHubDownloadApi");
const getDownloadConfiguration_1 = require("./getDownloadConfiguration");
const getHubDownloadApiFormats_1 = require("./getHubDownloadApiFormats");
/**
 * Gets available download formats / additional resources for the given entity in the order they have been configured.
 * If a format has been configured to be hidden, it will not be included in the results.
 *
 * @param options options to refine / filter the results of the getDownloadFormats operation
 * @returns The available download formats and additional resources
 */
function getDownloadFormats(options) {
    const { entity, context } = options;
    // get the base formats for the item
    let baseFormats = [];
    // TODO: use typescript to enforce a branch for each flow type
    if ((0, canUseHubDownloadApi_1.canUseHubDownloadApi)(entity, context)) {
        baseFormats = (0, getHubDownloadApiFormats_1.getHubDownloadApiFormats)(entity);
    }
    else if ((0, canUseExportImageFlow_1.canUseExportImageFlow)(entity)) {
        baseFormats = (0, getExportImageFormats_1.getExportImageFormats)(entity);
    }
    // add additional resource links as static formats
    const additionalResources = (0, get_prop_1.getProp)(entity, "extendedProps.additionalResources") || [];
    const additionalFormats = additionalResources.map(toStaticFormat);
    // Respect the order and visibility of the formats as configured for the entity
    const downloadConfiguration = (0, getDownloadConfiguration_1.getDownloadConfiguration)(entity);
    return downloadConfiguration.formats.reduce((acc, format) => {
        if (!format.hidden) {
            let includedFormat;
            if ((0, utils_1.isAdditionalResourceConfiguration)(format)) {
                const additionalResourceIndex = (0, utils_1.getAdditionalResourceIndex)(format);
                includedFormat = additionalFormats[additionalResourceIndex];
            }
            else {
                baseFormats.forEach((baseFormat) => {
                    if (baseFormat.format === format.key) {
                        includedFormat = baseFormat;
                    }
                });
            }
            acc.push(includedFormat);
        }
        return acc;
    }, []);
}
function toStaticFormat(resource) {
    return {
        type: "static",
        label: resource.name ||
            (resource.isDataSource && `{{dataSource:translate}}`) || // if the additional resource is the datasource
            `{{noTitle:translate}}`, // if the additional resource has no name
        url: resource.url,
    };
}
//# sourceMappingURL=getDownloadFormats.js.map