"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const schematics_1 = require("@angular-devkit/schematics");
const tasks_1 = require("@angular-devkit/schematics/tasks");
const ast_1 = require("../utils/ast");
const config_1 = require("../utils/devkit-utils/config");
const lib_versions_1 = require("../utils/lib-versions");
const package_1 = require("../utils/package");
const project_targets_1 = require("../utils/project-targets");
const ADD_CONFIG = {
    COMPILED_THEME_PATH: 'node_modules/at-ng/assets/index.css',
    BOOT_PAGE_PATH: 'src/app/app.component.html',
    BOOT_PAGE_HTML: `<!-- At Ui For Angular -->
<a  target="_blank" style="display: flex;align-items: center;justify-content: center;height: 100%;width: 100%;">
    Happy Coding
</a>`
};
function default_1(options) {
    return schematics_1.chain([
        options && options.skipPackageJson ? schematics_1.noop() : addAtToPackageJson(),
        setBootstrapPage(),
        addThemeToAppStyles(options),
        addModulesToAppModule(options),
        installNodeDeps()
    ]);
}
exports.default = default_1;
/** 添加 at-ng 到 package.json 的 dependencies */
function addAtToPackageJson() {
    return (host) => {
        package_1.addPackageToPackageJson(host, 'dependencies', 'at-ng', lib_versions_1.atVersion);
        return host;
    };
}
/** 添加 BrowserAnimationsModule FormsModule HttpClientModule AtModule 到 app.module */
function addModulesToAppModule(options) {
    return (host) => {
        const workspace = config_1.getWorkspace(host);
        const project = config_1.getProjectFromWorkspace(workspace, options.project);
        ast_1.addModuleImportToRootModule(host, 'BrowserAnimationsModule', '@angular/platform-browser/animations', project);
        ast_1.addModuleImportToRootModule(host, 'FormsModule', '@angular/forms', project);
        ast_1.addModuleImportToRootModule(host, 'HttpClientModule', '@angular/common/http', project);
        ast_1.addModuleImportToRootModule(host, 'AtModule', 'at-ng', project);
        return host;
    };
}
/** 添加样式配置 */
function addThemeToAppStyles(options) {
    return function (host) {
        const workspace = config_1.getWorkspace(host);
        const project = config_1.getProjectFromWorkspace(workspace, options.project);
        insertCompiledTheme(project, host, workspace);
        return host;
    };
}
exports.addThemeToAppStyles = addThemeToAppStyles;
/** 设置引导页面到 app.component.ts */
function setBootstrapPage() {
    return (host) => {
        host.overwrite(ADD_CONFIG.BOOT_PAGE_PATH, ADD_CONFIG.BOOT_PAGE_HTML);
        return host;
    };
}
/** 安装依赖 */
function installNodeDeps() {
    return (host, context) => {
        context.addTask(new tasks_1.NodePackageInstallTask());
    };
}
/** 将编译的 css 添加到 angular.json */
function insertCompiledTheme(project, host, workspace) {
    const themePath = ADD_CONFIG.COMPILED_THEME_PATH;
    // tslint:disable-next-line:no-any
    if (project.targets || project.architect) {
        addStyleToTarget('build', host, workspace, project, themePath);
        addStyleToTarget('test', host, workspace, project, themePath);
    }
    else {
        throw new schematics_1.SchematicsException(`${project.name} does not have an architect or targets configuration`);
    }
}
/** Adds a style entry to the given target. */
function addStyleToTarget(targetName, host, workspace, project, asset, exclude = '') {
    const styleEntry = asset;
    const targetOptions = project_targets_1.getProjectTargetOptions(project, targetName);
    // We can't assume that any of these properties are defined, so safely add them as we go
    // if necessary.
    if (!targetOptions.styles) {
        targetOptions.styles = [styleEntry];
    }
    else {
        const existingStyles = targetOptions.styles.map(s => typeof s === 'string' ? s : s.input);
        const hasGivenTheme = existingStyles.find(s => s.includes(asset));
        if (exclude) {
            const removeIndex = targetOptions.styles.indexOf(exclude);
            if (removeIndex >= 0) {
                targetOptions.styles.splice(removeIndex, 1);
            }
        }
        if (!hasGivenTheme) {
            targetOptions.styles.splice(0, 0, styleEntry);
        }
    }
    host.overwrite('angular.json', JSON.stringify(workspace, null, 2));
}
//# sourceMappingURL=index.js.map