"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const schematics_1 = require("@angular-devkit/schematics");
const config_dependencies_1 = require("../../config-dependencies");
const lib_1 = require("../../lib");
const angular_utility_1 = require("../../lib/angular-utility");
function getNpmDependencies(protocols) {
    return config_dependencies_1.GeneratorConfig.dependencies.filter(filterProtocolConstraint(protocols));
}
exports.getNpmDependencies = getNpmDependencies;
function getCordovaDependencies(protocols) {
    return config_dependencies_1.GeneratorConfig.cordovaDependencies.filter(filterProtocolConstraint(protocols));
}
exports.getCordovaDependencies = getCordovaDependencies;
function arrayIntersect(array1, array2) {
    return array1.filter(value => array2.includes(value));
}
function filterProtocolConstraint(protocols) {
    return (item, index, array) => {
        if (item.constraint && item.constraint.protocols) {
            return arrayIntersect(protocols, item.constraint.protocols).length > 0;
        }
        return true;
    };
}
exports.filterProtocolConstraint = filterProtocolConstraint;
function default_1(options) {
    return schematics_1.chain([
        angular_utility_1.toPromiseRule(async (tree, context) => {
            const project = lib_1.loadProject(tree, context);
            const npmDependencies = getNpmDependencies(project.communicationProtocols);
            const cordovaDependencies = getCordovaDependencies(project.communicationProtocols);
            project
                .dependencies
                .addNpmDependencies(npmDependencies)
                .addCordovaPlugin(cordovaDependencies);
            // if (project.dependencies.hasNewDependencies()) {
            //   context.logger.info(`${options.dryRun ? '[DRY RUN] ' : ''}New dependencies to install: ${project.dependencies.newDependencies.map(p => `${p.name}@${p.version}`).join(', ')}`);
            //   if (!options.skipInstall && !options.dryRun) {
            //     if (tree.exists('package.json')) {
            //       await packageManager.installFromConfig(JSON.parse(tree.read('package.json')?.toString() || '{}'))
            //         .catch(err => {
            //           context.logger.error(`Cannot install dependencies: ${err.message}`);
            //         });
            //     }
            //     else {
            //       context.logger.warn(`package.json file has not been created yet. Install packages later`);
            //     }
            //   }
            //   else {
            //     context.logger.warn(`Dependency installation is disabled (option --skipInstall). Perform dependency installation manually (eg: npm install)`);
            //   }
            // }
        })
    ]);
}
exports.default = default_1;
//# sourceMappingURL=index.js.map