"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDownloadConfiguration = getDownloadConfiguration;
const get_prop_1 = require("../objects/get-prop");
const getDownloadFlow_1 = require("./_internal/getDownloadFlow");
const getDownloadFormatsByFlow_1 = require("./_internal/getDownloadFormatsByFlow");
/**
 * Returns the download configuration for an entity at this moment in time.
 *
 * If no configuration exists, a default configuration is returned based on the entity's current download flow.
 * If a configuration exists but is no longer valid, the default configuration will also be returned.
 *
 * @param entity entity to get download configuration for
 * @returns the current download configuration for the entity
 */
function getDownloadConfiguration(entity) {
    // TODO: account for enterprise environments
    const downloadFlow = (0, getDownloadFlow_1.getDownloadFlow)(entity);
    const serverFormats = (0, getDownloadFormatsByFlow_1.getDownloadFormatsByFlow)(downloadFlow, entity);
    const additionalResources = (0, get_prop_1.getProp)(entity, "extendedProps.additionalResources") || [];
    const existingConfiguration = (0, get_prop_1.getProp)(entity, "extendedProps.downloads");
    // Base combined default formats
    const combinedDefaultFormats = serverFormats.map((f) => {
        return {
            key: f.format,
            hidden: false,
        };
    });
    additionalResources.forEach((f, idx) => {
        combinedDefaultFormats.push({
            key: `additionalResource::${idx}`,
            hidden: false,
        });
    });
    const shouldUseExistingConfiguration = existingConfiguration && existingConfiguration.flowType === downloadFlow;
    // Existing configuration matches the current flow
    if (shouldUseExistingConfiguration) {
        const missingDefaultFormats = combinedDefaultFormats.filter((f) => {
            return !existingConfiguration.formats.find((df) => df.key === f.key);
        });
        const validConfiguredFormats = existingConfiguration.formats.filter((f) => {
            return combinedDefaultFormats.find((df) => df.key === f.key);
        });
        return {
            flowType: downloadFlow,
            formats: [...validConfiguredFormats, ...missingDefaultFormats],
        };
    }
    // Existing configuration does not match the current flow
    return {
        flowType: downloadFlow,
        formats: combinedDefaultFormats,
    };
}
//# sourceMappingURL=getDownloadConfiguration.js.map