"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.addDependencies = addDependencies;
const schematics_1 = require("@angular-devkit/schematics");
const dependencies_1 = require("@schematics/angular/utility/dependencies");
const compare_versions_1 = require("compare-versions");
const semver_1 = require("semver");
const package_utils_1 = require("../utils/package-utils");
const libraryPackageJson = require('@fundamental-ngx/core/package.json');
const desiredVersions = {
    'fundamental-styles': process.env.FD_ENV_FDSTYLES_VER_PLACEHOLDER || libraryPackageJson.peerDependencies['fundamental-styles'],
    '@sap-theming/theming-base-content': process.env.FD_ENV_THEMING_VER_PLACEHOLDER ||
        libraryPackageJson.peerDependencies['@sap-theming/theming-base-content'],
    '@fundamental-ngx/i18n': process.env.FD_ENV_VERSION_PLACEHOLDER || libraryPackageJson.peerDependencies['@fundamental-ngx/i18n'],
    '@fundamental-ngx/cdk': process.env.FD_ENV_VERSION_PLACEHOLDER || libraryPackageJson.peerDependencies['@fundamental-ngx/cdk']
};
/**
 * Adds dependencies to the project
 * @param options
 */
function addDependencies(options) {
    return addExternalLibraries(options);
}
function addExternalLibraries(options) {
    return (tree, context) => {
        const ngCoreVersionTag = (0, dependencies_1.getPackageJsonDependency)(tree, '@angular/core');
        if (!ngCoreVersionTag) {
            throw new schematics_1.SchematicsException('Could not find @angular/core in package.json');
        }
        const angularVersion = (0, semver_1.valid)((0, semver_1.coerce)(ngCoreVersionTag.version))
            ? `^${(0, semver_1.major)((0, semver_1.coerce)(ngCoreVersionTag.version))}.0.0`
            : null;
        if (!angularVersion) {
            throw new schematics_1.SchematicsException(`Could not determine Angular version, ${ngCoreVersionTag} is not valid semver`);
        }
        const formsDependency = (0, dependencies_1.getPackageJsonDependency)(tree, '@angular/forms');
        if (!formsDependency) {
            (0, package_utils_1.addPackageDependency)(tree, {
                type: dependencies_1.NodeDependencyType.Default,
                version: angularVersion,
                name: '@angular/forms'
            }, context);
        }
        const animationsDependency = (0, dependencies_1.getPackageJsonDependency)(tree, '@angular/animations');
        if (options.animations && !animationsDependency) {
            (0, package_utils_1.addPackageDependency)(tree, {
                type: dependencies_1.NodeDependencyType.Default,
                version: angularVersion,
                name: '@angular/animations'
            }, context);
        }
        const cdKDependency = (0, dependencies_1.getPackageJsonDependency)(tree, '@angular/cdk');
        if (!cdKDependency) {
            (0, package_utils_1.addPackageDependency)(tree, {
                type: dependencies_1.NodeDependencyType.Default,
                version: angularVersion,
                name: '@angular/cdk'
            }, context);
        }
        const fundamentalStylesDependency = (0, dependencies_1.getPackageJsonDependency)(tree, 'fundamental-styles');
        if (!fundamentalStylesDependency ||
            (0, compare_versions_1.compare)(fundamentalStylesDependency.version, desiredVersions['fundamental-styles'], '<')) {
            (0, package_utils_1.addPackageDependency)(tree, {
                type: dependencies_1.NodeDependencyType.Default,
                // Will be replaced with the real version during sync-version script run
                version: desiredVersions['fundamental-styles'],
                name: 'fundamental-styles',
                overwrite: true
            }, context);
        }
        const themingBaseContentDependency = (0, dependencies_1.getPackageJsonDependency)(tree, '@sap-theming/theming-base-content');
        if (!themingBaseContentDependency ||
            (0, compare_versions_1.compare)(themingBaseContentDependency.version, desiredVersions['@sap-theming/theming-base-content'], '<')) {
            (0, package_utils_1.addPackageDependency)(tree, {
                type: dependencies_1.NodeDependencyType.Default,
                // Will be replaced with the real version during sync-version script run
                version: desiredVersions['@sap-theming/theming-base-content'],
                name: '@sap-theming/theming-base-content',
                overwrite: true
            }, context);
        }
        const i18nDependency = (0, dependencies_1.getPackageJsonDependency)(tree, '@fundamental-ngx/i18n');
        if (!i18nDependency || (0, compare_versions_1.compare)(i18nDependency.version, desiredVersions['@fundamental-ngx/i18n'], '<')) {
            (0, package_utils_1.addPackageDependency)(tree, {
                type: dependencies_1.NodeDependencyType.Default,
                // Will be replaced with the real version during sync-version script run
                version: desiredVersions['@fundamental-ngx/i18n'],
                name: '@fundamental-ngx/i18n',
                overwrite: true
            }, context);
        }
        const fdCdkDependency = (0, dependencies_1.getPackageJsonDependency)(tree, '@fundamental-ngx/cdk');
        if (!fdCdkDependency || (0, compare_versions_1.compare)(fdCdkDependency.version, desiredVersions['@fundamental-ngx/cdk'], '<')) {
            (0, package_utils_1.addPackageDependency)(tree, {
                type: dependencies_1.NodeDependencyType.Default,
                // Will be replaced with the real version during sync-version script run
                version: desiredVersions['@fundamental-ngx/cdk'],
                name: '@fundamental-ngx/cdk',
                overwrite: true
            }, context);
        }
        return tree;
    };
}
//# sourceMappingURL=index.js.map