"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@angular-devkit/core");
const schematics_1 = require("@angular-devkit/schematics");
const tasks_1 = require("@angular-devkit/schematics/tasks");
const workspace_1 = require("@schematics/angular/utility/workspace");
const ast_1 = require("./../utils/ast");
const config_1 = require("./../utils/config");
const package_1 = require("./../utils/package");
function addIonicAngularToPackageJson() {
    return (host) => {
        (0, package_1.addPackageToPackageJson)(host, 'dependencies', '@brucestafflink/ionic-angular', 'latest');
        return host;
    };
}
function addIonicAngularToolkitToPackageJson() {
    return (host) => {
        (0, package_1.addPackageToPackageJson)(host, 'devDependencies', '@brucestafflink/ionic-angular-toolkit', 'latest');
        return host;
    };
}
function addIonicAngularModuleToAppModule(projectSourceRoot) {
    return (host) => {
        (0, ast_1.addModuleImportToRootModule)(host, projectSourceRoot, 'IonicModule.forRoot()', '@brucestafflink/ionic-angular');
        return host;
    };
}
function addIonicStyles(projectName, projectSourceRoot) {
    return (host) => {
        const ionicStyles = [
            'node_modules/@brucestafflink/ionic-angular/css/core.css',
            'node_modules/@brucestafflink/ionic-angular/css/normalize.css',
            'node_modules/@brucestafflink/ionic-angular/css/structure.css',
            'node_modules/@brucestafflink/ionic-angular/css/typography.css',
            'node_modules/@brucestafflink/ionic-angular/css/display.css',
            'node_modules/@brucestafflink/ionic-angular/css/padding.css',
            'node_modules/@brucestafflink/ionic-angular/css/float-elements.css',
            'node_modules/@brucestafflink/ionic-angular/css/text-alignment.css',
            'node_modules/@brucestafflink/ionic-angular/css/text-transformation.css',
            'node_modules/@brucestafflink/ionic-angular/css/flex-utils.css',
            `${projectSourceRoot}/theme/variables.css`,
        ];
        ionicStyles.forEach((entry) => {
            (0, config_1.addStyle)(host, projectName, entry);
        });
        return host;
    };
}
function addIonicons(projectName) {
    return (host) => {
        const ioniconsGlob = {
            glob: '**/*.svg',
            input: 'node_modules/ionicons/dist/ionicons/svg',
            output: './svg',
        };
        (0, config_1.addAsset)(host, projectName, 'build', ioniconsGlob);
        (0, config_1.addAsset)(host, projectName, 'test', ioniconsGlob);
        return host;
    };
}
function addIonicBuilder(projectName) {
    return (host) => {
        (0, config_1.addArchitectBuilder)(host, projectName, 'ionic-cordova-serve', {
            builder: '@brucestafflink/ionic-angular-toolkit:cordova-serve',
            options: {
                cordovaBuildTarget: `${projectName}:ionic-cordova-build`,
                devServerTarget: `${projectName}:serve`,
            },
            configurations: {
                production: {
                    cordovaBuildTarget: `${projectName}:ionic-cordova-build:production`,
                    devServerTarget: `${projectName}:serve:production`,
                },
            },
        });
        (0, config_1.addArchitectBuilder)(host, projectName, 'ionic-cordova-build', {
            builder: '@brucestafflink/ionic-angular-toolkit:cordova-build',
            options: {
                browserTarget: `${projectName}:build`,
            },
            configurations: {
                production: {
                    browserTarget: `${projectName}:build:production`,
                },
            },
        });
        return host;
    };
}
function installNodeDeps() {
    return (_host, context) => {
        context.addTask(new tasks_1.NodePackageInstallTask());
    };
}
function ngAdd(options) {
    return async (host) => {
        const workspace = await (0, workspace_1.getWorkspace)(host);
        if (!options.project) {
            options.project = (0, config_1.getDefaultAngularAppName)(workspace);
        }
        const project = workspace.projects.get(options.project);
        if (!project || project.extensions.projectType !== 'application') {
            throw new schematics_1.SchematicsException(`Ionic Add requires a project type of "application".`);
        }
        const sourcePath = (0, core_1.join)(project.sourceRoot);
        const rootTemplateSource = (0, schematics_1.apply)((0, schematics_1.url)('./files/root'), [(0, schematics_1.template)(Object.assign({}, options)), (0, schematics_1.move)(sourcePath)]);
        return (0, schematics_1.chain)([
            // @brucestafflink/ionic-angular
            addIonicAngularToPackageJson(),
            addIonicAngularToolkitToPackageJson(),
            addIonicAngularModuleToAppModule(sourcePath),
            addIonicBuilder(options.project),
            addIonicStyles(options.project, sourcePath),
            addIonicons(options.project),
            (0, schematics_1.mergeWith)(rootTemplateSource),
            // install freshly added dependencies
            installNodeDeps(),
        ]);
    };
}
exports.default = ngAdd;
