"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const schematics_1 = require("@angular-devkit/schematics");
const xplat_1 = require("@nstudio/xplat");
const xplat_utils_1 = require("@nstudio/xplat-utils");
const utils_1 = require("../../utils");
function default_1(options) {
    if (!options.name) {
        throw new schematics_1.SchematicsException(xplat_1.missingArgument('name', 'Provide a name for your NativeScript app.', 'nx g @nstudio/nativescript:app name'));
    }
    // if (options.setupSandbox) {
    //   // always setup routing with sandbox
    //   options.routing = true;
    // }
    return schematics_1.chain([
        xplat_utils_1.prerun(options),
        // adjust naming convention
        xplat_1.XplatHelpers.applyAppNamingConvention(options, 'nativescript'),
        utils_1.XplatNativeScriptHelpers.addReferences(),
        // create app files
        (tree, context) => addAppFiles(options, options.name),
        // add app resources
        (tree, context) => 
        // inside closure to ensure it uses the modifed options.name from applyAppNamingConvention
        schematics_1.externalSchematic('@nstudio/nativescript', 'app-resources', {
            path: `apps/${options.name}`,
        }, { interactive: false })(tree, context),
        // add root package dependencies
        utils_1.XplatNativeScriptHelpers.updateRootDeps(options),
        utils_1.XplatNativeScriptHelpers.updatePrettierIgnore(),
        xplat_1.XplatHelpers.addPackageInstallTask(options),
        // add start/clean scripts
        (tree) => {
            const scripts = {};
            const platformApp = options.name.replace('-', '.');
            const directory = options.directory ? `${options.directory}/` : '';
            // standard apps don't have hmr on by default since results can vary
            // more reliable to leave off by default for now
            // however, sandbox setup due to its simplicity uses hmr by default
            scripts[`clean`] = `npx rimraf -- hooks node_modules package-lock.json && npm i`;
            return xplat_1.updatePackageScripts(tree, scripts);
        },
        (tree, context) => {
            const platformApp = options.name.replace('-', '.');
            const directory = options.directory ? `${options.directory}/` : '';
            const projects = {};
            projects[`${options.name}`] = {
                root: `apps/${directory}${options.name}/`,
                sourceRoot: `apps/${directory}${options.name}/src`,
                projectType: 'application',
                prefix: xplat_utils_1.getPrefix(),
                architect: {
                    ios: {
                        builder: '@nrwl/workspace:run-commands',
                        options: {
                            commands: [
                                `ns debug ios --no-hmr --env.configuration={args.configuration} --env.projectName=${options.name}`,
                            ],
                            cwd: `apps/${directory}${options.name}`,
                            parallel: false,
                        },
                    },
                    android: {
                        builder: '@nrwl/workspace:run-commands',
                        options: {
                            commands: [
                                `ns debug android --no-hmr --env.configuration={args.configuration} --env.projectName=${options.name}`,
                            ],
                            cwd: `apps/${directory}${options.name}`,
                            parallel: false,
                        },
                    },
                    clean: {
                        builder: '@nrwl/workspace:run-commands',
                        options: {
                            commands: ['ns clean', 'npm i', 'npx rimraf package-lock.json'],
                            cwd: `apps/${directory}${options.name}`,
                            parallel: false,
                        },
                    },
                },
            };
            return xplat_1.updateWorkspace({ projects })(tree, context);
        },
        (tree) => {
            const projects = {};
            projects[`${options.name}`] = {
                tags: [],
            };
            return xplat_1.updateNxProjects(tree, projects);
        },
        (tree) => {
            xplat_1.output.log({
                title: 'You will be able to run your app with:',
                bodyLines: [
                    `nx run ${options.name}:ios`,
                    `   `,
                    `nx run ${options.name}:android`,
                    `   `,
                    `You can also:`,
                    `   `,
                    `nx run ${options.name}:clean`,
                ],
            });
        },
    ]);
}
exports.default = default_1;
function addAppFiles(options, appPath, extra = '') {
    extra = extra ? `${extra}_` : '';
    const appname = xplat_utils_1.getAppName(options, 'nativescript');
    const directory = options.directory ? `${options.directory}/` : '';
    return schematics_1.branchAndMerge(schematics_1.mergeWith(schematics_1.apply(schematics_1.url(`./_${extra}files`), [
        schematics_1.template(Object.assign(Object.assign(Object.assign({}, options), xplat_1.getDefaultTemplateOptions()), { appname, pathOffset: directory ? '../../../' : '../../', xplatFolderName: xplat_1.XplatHelpers.getXplatFoldername('nativescript') })),
        schematics_1.move(`apps/${directory}${appPath}`),
    ])));
}
