"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const schematics_1 = require("@angular-devkit/schematics");
const fs_1 = require("fs");
const path_1 = require("path");
const config_dependencies_1 = require("../../config-dependencies");
const lib_1 = require("../../lib");
const shell_1 = require("../../lib/shell");
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).join(',')}`);
                if (!_options.skipInstall && !_options.dryRun) {
                    const packageJsonFile = path_1.resolve(process.cwd(), 'package.json');
                    if (!fs_1.existsSync(packageJsonFile)) {
                        _context.logger.warn(`package.json file has not been created yet. Install packages later`);
                    }
                    else {
                        // TODO does not work with YARN
                        // Add test package manager to use
                        await shell_1.shell.execSpawn('npm', [
                            'install'
                        ]).catch(err => {
                            _context.logger.error(`Cannot install dependencies: ${err.message}`);
                        });
                    }
                }
                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