"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MetadataFilteringService = void 0;
const path = require("path");
const os = require("os");
const constants_1 = require("../constants");
const yok_1 = require("../common/yok");
class MetadataFilteringService {
    constructor($fs, $pluginsService, $mobileHelper, $platformsDataService, $logger) {
        this.$fs = $fs;
        this.$pluginsService = $pluginsService;
        this.$mobileHelper = $mobileHelper;
        this.$platformsDataService = $platformsDataService;
        this.$logger = $logger;
    }
    generateMetadataFilters(projectData, platform) {
        this.generateWhitelist(projectData, platform);
        this.generateBlacklist(projectData, platform);
    }
    generateWhitelist(projectData, platform) {
        const platformsDirPath = this.getPlatformsDirPath(projectData, platform);
        const pathToWhitelistFile = path.join(platformsDirPath, constants_1.MetadataFilteringConstants.WHITELIST_FILE_NAME);
        this.$fs.deleteFile(pathToWhitelistFile);
        const nativeApiConfiguration = this.getNativeApiConfigurationForPlatform(projectData, platform);
        if (nativeApiConfiguration) {
            const whitelistedItems = [];
            if (nativeApiConfiguration["whitelist-plugins-usages"]) {
                const plugins = this.$pluginsService.getAllProductionPlugins(projectData, platform);
                for (const pluginData of plugins) {
                    const pathToPlatformsDir = pluginData.pluginPlatformsFolderPath(platform);
                    const pathToPluginsMetadataConfig = path.join(pathToPlatformsDir, constants_1.MetadataFilteringConstants.NATIVE_API_USAGE_FILE_NAME);
                    if (this.$fs.exists(pathToPluginsMetadataConfig)) {
                        const pluginConfig = this.$fs.readJson(pathToPluginsMetadataConfig) || {};
                        this.$logger.trace(`Adding content of ${pathToPluginsMetadataConfig} to whitelisted items of metadata filtering: ${JSON.stringify(pluginConfig, null, 2)}`);
                        const itemsToAdd = pluginConfig.uses || [];
                        if (itemsToAdd.length) {
                            whitelistedItems.push(`// Added from: ${pathToPluginsMetadataConfig}`);
                            whitelistedItems.push(...itemsToAdd);
                            whitelistedItems.push(`// Finished part from ${pathToPluginsMetadataConfig}${os.EOL}`);
                        }
                    }
                }
            }
            const applicationWhitelistedItems = nativeApiConfiguration.whitelist || [];
            if (applicationWhitelistedItems.length) {
                this.$logger.trace(`Adding content from application to whitelisted items of metadata filtering: ${JSON.stringify(applicationWhitelistedItems, null, 2)}`);
                whitelistedItems.push(`// Added from application`);
                whitelistedItems.push(...applicationWhitelistedItems);
                whitelistedItems.push(`// Finished part from application${os.EOL}`);
            }
            if (whitelistedItems.length) {
                this.$fs.writeFile(pathToWhitelistFile, whitelistedItems.join(os.EOL));
            }
        }
    }
    generateBlacklist(projectData, platform) {
        const platformsDirPath = this.getPlatformsDirPath(projectData, platform);
        const pathToBlacklistFile = path.join(platformsDirPath, constants_1.MetadataFilteringConstants.BLACKLIST_FILE_NAME);
        this.$fs.deleteFile(pathToBlacklistFile);
        const nativeApiConfiguration = this.getNativeApiConfigurationForPlatform(projectData, platform);
        if (nativeApiConfiguration) {
            const blacklistedItems = nativeApiConfiguration.blacklist || [];
            if (nativeApiConfiguration["whitelist-plugins-usages"]) {
                const plugins = this.$pluginsService.getAllProductionPlugins(projectData, platform);
                for (const pluginData of plugins) {
                    const pathToPlatformsDir = pluginData.pluginPlatformsFolderPath(platform);
                    const pathToPluginsMetadataConfig = path.join(pathToPlatformsDir, constants_1.MetadataFilteringConstants.NATIVE_API_USAGE_FILE_NAME);
                    if (this.$fs.exists(pathToPluginsMetadataConfig)) {
                        const pluginConfig = this.$fs.readJson(pathToPluginsMetadataConfig) || {};
                        this.$logger.trace(`Adding content of ${pathToPluginsMetadataConfig} to whitelisted items of metadata filtering: ${JSON.stringify(pluginConfig, null, 2)}`);
                        const itemsToAdd = pluginConfig.blacklist || [];
                        if (itemsToAdd.length) {
                            blacklistedItems.push(`// Added from: ${pathToPluginsMetadataConfig}`);
                            blacklistedItems.push(...itemsToAdd);
                            blacklistedItems.push(`// Finished part from ${pathToPluginsMetadataConfig}${os.EOL}`);
                        }
                    }
                }
            }
            if (blacklistedItems.length) {
                this.$fs.writeFile(pathToBlacklistFile, blacklistedItems.join(os.EOL));
            }
        }
        else {
            this.$logger.trace(`There's no application configuration for metadata filtering for platform ${platform}. Full metadata will be generated.`);
        }
    }
    getNativeApiConfigurationForPlatform(projectData, platform) {
        let config = null;
        const pathToApplicationConfigurationFile = this.getPathToApplicationConfigurationForPlatform(projectData, platform);
        if (this.$fs.exists(pathToApplicationConfigurationFile)) {
            config = this.$fs.readJson(pathToApplicationConfigurationFile);
        }
        return config;
    }
    getPlatformsDirPath(projectData, platform) {
        const platformData = this.$platformsDataService.getPlatformData(platform, projectData);
        return platformData.projectRoot;
    }
    getPathToApplicationConfigurationForPlatform(projectData, platform) {
        return path.join(projectData.appResourcesDirectoryPath, this.$mobileHelper.normalizePlatformName(platform), constants_1.MetadataFilteringConstants.NATIVE_API_USAGE_FILE_NAME);
    }
}
exports.MetadataFilteringService = MetadataFilteringService;
yok_1.injector.register("metadataFilteringService", MetadataFilteringService);
