"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ngAdd = ngAdd;
const schematics_1 = require("@angular-devkit/schematics");
const tasks_1 = require("@angular-devkit/schematics/tasks");
const dependencies_1 = require("@schematics/angular/utility/dependencies");
const package_utils_1 = require("../utils/package-utils");
/**
 * ng add schematic that
 * - adds platform lib to package.json
 * - adds @fundamental-ngx/core to package.json if not present there already
 * - installs dependent libraries and runs their external schematics
 * - adds lib translations and angular.json locale configurations to host app if no translations available
 * - updates lib translations to host app's translations if available by appending at the end of host app's files
 * - replaces lib source paths with node_modules source paths for lib-related trans units
 * @param options options passed for this schematic
 */
function ngAdd(options) {
    return (tree) => {
        const coreInstalled = (0, package_utils_1.hasPackage)(tree, '@fundamental-ngx/core');
        return (0, schematics_1.chain)([coreInstalled ? (0, schematics_1.noop)() : callCoreSchematic(options), endInstallTask()]);
    };
}
/**
 * installs `@fundamental-ngx/core` lib and makes call to core's schematic
 * @param options options passed for this schematic
 */
function callCoreSchematic(options) {
    return (tree, context) => {
        context.logger.info('✅️ Added Fundamental NGX Core schematic to tasks');
        (0, dependencies_1.addPackageJsonDependency)(tree, {
            type: dependencies_1.NodeDependencyType.Default,
            // Will be replaced with the real version during sync-version script run
            version: `0.58.6`,
            name: '@fundamental-ngx/core'
        });
        const installTaskId = context.addTask(new tasks_1.NodePackageInstallTask({
            packageName: '@fundamental-ngx/core'
        }));
        // Chain won't work here since we need the externals to be actually installed before we call their schemas
        // This ensures the externals are a dependency of the node install, so they exist when their schemas run.
        context.addTask(new tasks_1.RunSchematicTask('@fundamental-ngx/core', 'ng-add', options), [installTaskId]);
        return tree;
    };
}
/**
 *  Run npm install.
 */
function endInstallTask() {
    return (tree, context) => {
        context.addTask(new tasks_1.NodePackageInstallTask());
        return tree;
    };
}
//# sourceMappingURL=index.js.map