"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.addAnimations = addAnimations;
const schematics_1 = require("@angular-devkit/schematics");
const schematics_2 = require("@angular/cdk/schematics");
const utility_1 = require("@schematics/angular/utility");
const calls_providers_function_1 = require("../utils/calls-providers-function");
const main_ts_file_path_1 = require("../utils/main-ts-file-path");
const ng_module_utils_1 = require("../utils/ng-module-utils");
/**
 * Adds animations to the project.
 * @param options
 */
function addAnimations(options) {
    return (0, schematics_1.chain)([_addAnimations(options)]);
}
function _addAnimations(options) {
    return (tree, context) => __awaiter(this, void 0, void 0, function* () {
        const mainFilePath = yield (0, main_ts_file_path_1.getMainTsFilePath)(tree, options.project);
        const isStandalone = (0, schematics_2.isStandaloneApp)(tree, mainFilePath);
        return isStandalone
            ? addAnimationsProviders(options, tree, context)
            : addAnimationsModule(options, tree, context);
    });
}
function addAnimationsProviders(options, tree, context) {
    return __awaiter(this, void 0, void 0, function* () {
        const animationsFunction = 'provideAnimations';
        const noopAnimationsFunction = 'provideNoopAnimations';
        if (options.animations) {
            if (yield (0, calls_providers_function_1.callsProvidersFunction)(tree, options.project, animationsFunction)) {
                context.logger.info(`✅️ ${animationsFunction} already present in bootstrap call. Skipping.`);
                return;
            }
            if (yield (0, calls_providers_function_1.callsProvidersFunction)(tree, options.project, noopAnimationsFunction)) {
                context.logger.warn(`⚠️ Could not set up "${animationsFunction} because "${noopAnimationsFunction}" is already imported. Please manually set up browser animations.`);
                return;
            }
            return (0, utility_1.addRootProvider)(options.project, ({ code, external }) => code `${external(animationsFunction, '@angular/platform-browser/animations')}()`);
        }
        return;
    });
}
function addAnimationsModule(options, tree, context) {
    return __awaiter(this, void 0, void 0, function* () {
        const browserAnimationsModuleName = 'BrowserAnimationsModule';
        const noopAnimationsModuleName = 'NoopAnimationsModule';
        const modulePath = yield (0, schematics_2.findModuleFromOptions)(tree, options);
        if (!modulePath) {
            context.logger.warn(`⚠️ Could not set up animations because root module not found. Please manually set up animations.`);
            return;
        }
        if (options.animations) {
            if ((0, ng_module_utils_1.hasModuleImport)(tree, modulePath, noopAnimationsModuleName)) {
                context.logger.warn(`⚠️ Could not set up "${browserAnimationsModuleName} because "${noopAnimationsModuleName}" is already imported. Please manually set up browser animations.`);
                return;
            }
            if ((0, ng_module_utils_1.hasModuleImport)(tree, modulePath, browserAnimationsModuleName)) {
                context.logger.info(`✅️ Import of ${browserAnimationsModuleName} already present in app module. Skipping.`);
                return;
            }
            (0, schematics_2.addModuleImportToModule)(tree, modulePath, browserAnimationsModuleName, '@angular/platform-browser/animations');
            context.logger.info(`✅️ Added ${browserAnimationsModuleName} to app module.`);
            return;
        }
        if (!(0, ng_module_utils_1.hasModuleImport)(tree, modulePath, browserAnimationsModuleName)) {
            (0, schematics_2.addModuleImportToModule)(tree, modulePath, noopAnimationsModuleName, '@angular/platform-browser/animations');
            context.logger.info(`✅️ Added ${noopAnimationsModuleName} to app module.`);
        }
    });
}
//# sourceMappingURL=index.js.map