"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = require("fs");
const path_1 = require("path");
const schematics_1 = require("@angular-devkit/schematics");
const config_1 = require("@schematics/angular/utility/config");
const tasks_1 = require("@angular-devkit/schematics/tasks");
const terminal_1 = require("@angular-devkit/core/src/terminal");
const schematics_2 = require("@angular/cdk/schematics");
const package_config_1 = require("./package-config");
// Determine where to load the package.json, if doing local dev or not
let corePackage;
if (fs_1.existsSync(path_1.join(__dirname, '../../package.json'))) {
    corePackage = require('../../package.json');
}
else {
    corePackage = require('../../../../package.json');
}
function default_1(options) {
    return schematics_1.chain([
        addMomentumUIDependencies(options),
        addModuleImport(options),
        addMomentumUIAppStyles(options),
    ]);
}
exports.default = default_1;
function addMomentumUIDependencies(options) {
    return (tree, context) => {
        package_config_1.addPackageToPackageJson(tree, '@momentum-ui/angular', `^${corePackage.version}`);
        // add installation task
        if (!options.skipInstall) {
            context.addTask(new tasks_1.NodePackageInstallTask());
        }
        return tree;
    };
}
function addModuleImport(options) {
    return (tree) => {
        const workspace = config_1.getWorkspace(tree);
        const project = schematics_2.getProjectFromWorkspace(workspace, options.project);
        if (options.module) {
            schematics_2.addModuleImportToModule(tree, options.module, 'BadgeModule', '@momentum-ui/angular');
        }
        else {
            schematics_2.addModuleImportToRootModule(tree, 'BadgeModule', '@momentum-ui/angular', project);
        }
        return tree;
    };
}
function addMomentumUIAppStyles(options) {
    return (host) => {
        const workspace = config_1.getWorkspace(host);
        const project = schematics_2.getProjectFromWorkspace(workspace, options.project);
        const styleFilePath = schematics_2.getProjectStyleFile(project);
        if (!styleFilePath) {
            console.warn(terminal_1.red(`Could not find the default style file for this project.`));
            console.warn(terminal_1.red(`Please consider manually including momentum-ui styles in the project.`));
            return;
        }
        const buffer = host.read(styleFilePath);
        if (!buffer) {
            console.warn(terminal_1.red(`Could not read the default style file within the project ` +
                `(${terminal_1.italic(styleFilePath)})`));
            console.warn(terminal_1.red(`Please consider manually including momentum-ui styles in the project.`));
            return;
        }
        const htmlContent = buffer.toString();
        const insertion = '\n' +
            `$brand-font-folder: '~@momentum-ui/core/fonts';\n` +
            `$icon-font-path: '~@momentum-ui/icons/fonts';\n` +
            `$images-path: '~@momentum-ui/core/images';\n\n` +
            `@import '~@momentum-ui/core/scss/momentum-ui';`;
        if (htmlContent.includes(insertion)) {
            return;
        }
        const recorder = host.beginUpdate(styleFilePath);
        recorder.insertLeft(htmlContent.length, insertion);
        host.commitUpdate(recorder);
    };
}
//# sourceMappingURL=index.js.map