"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const schematics_1 = require("@angular-devkit/schematics");
const angular_utility_1 = require("../../lib/angular-utility");
const shell_1 = require("../../lib/shell");
const utility_1 = require("../../lib/utility");
const input_1 = require("../../lib/input");
const firebase_1 = require("../../lib/firebase");
const lib_1 = require("../../lib");
function default_1(_options) {
    return schematics_1.chain([
        angular_utility_1.toPromiseRule(async (tree, _context) => {
            if (!shell_1.shell.hasCommand('firebase')) {
                let confirm = await input_1.input.confirm(`It seems you didn't have firebase CLI tool, install il now ? `);
                if (!confirm) {
                    throw new schematics_1.SchematicsException('Operation cancelled by user');
                }
                shell_1.shell.execSync('npm install --global firebase-tools');
                shell_1.shell.execSync('firebase login');
            }
        }),
        angular_utility_1.toPromiseRule(async (tree, _context) => {
            const project = lib_1.loadProject(tree, _context);
            let projectId = await input_1.input.string(`What is your firebase project identifier ?`, {
                validate: async (value) => {
                    let res = await firebase_1.firebase.projectExists(value);
                    if (!res) {
                        _context.logger.warn(`Invalid project id "${value}"`);
                    }
                    return res;
                }
            });
            utility_1.updatePackageFile(tree, (config) => {
                if (!config.scripts) {
                    config.scripts = {};
                }
                if (!('deploy:firebase' in config.scripts)) {
                    config.scripts['deploy:firebase'] = 'ng build --prod && firebase deploy';
                }
                return config;
            });
            await utility_1.updateJsonFile(tree, '.firebaserc', async (config) => {
                if (!config) {
                    config = {};
                }
                if (!config.projects) {
                    config.projects = {};
                }
                if (!config.projects.default) {
                    config.projects.default = projectId;
                }
                return config;
            });
            project.appConfigBuilder.addFirebaseIntegration({
                type: 'FirebaseIntegration',
                projectId: projectId
            });
            project.save();
        }),
        (tree, _context) => {
            let sourceTemplates = schematics_1.url('files');
            return angular_utility_1.applyWithOverwriteRule(sourceTemplates, [
                schematics_1.template({})
            ], {
                overwrite: _options.overwrite,
                format: true
            });
        }
    ]);
}
exports.default = default_1;
//# sourceMappingURL=index.js.map