"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments)).next());
    });
};
const chalk = require("chalk");
const cli_utils_1 = require("@ionic/cli-utils");
const cordova_1 = require("../lib/utils/cordova");
const configXmlUtils_1 = require("../lib/utils/configXmlUtils");
const resources_1 = require("../lib/resources");
/**
 * Metadata about the platform command
 */
let PlatformCommand = class PlatformCommand extends cli_utils_1.Command {
    run(inputs, options) {
        return __awaiter(this, void 0, void 0, function* () {
            let action = inputs[0];
            action = (action === 'rm') ? 'remove' : action;
            const platformName = inputs[1];
            var tasks = new cli_utils_1.TaskChain();
            // ensure the content node was set back to its original src
            yield configXmlUtils_1.resetConfigXmlContentSrc(this.env.project.directory);
            const optionList = cordova_1.filterArgumentsForCordova(this.metadata, inputs, options);
            tasks.next(`Executing cordova command: ${chalk.bold('cordova ' + optionList.join(' '))}`);
            try {
                yield new cli_utils_1.Shell().run('cordova', optionList, {
                    showExecution: (this.env.log.level === 'debug')
                });
            }
            catch (e) {
                throw e;
            }
            if (action === 'add' && !(options['noresources']) && ['ios', 'android', 'wp8'].includes(platformName)) {
                tasks.next(`Copying default image resources into ${chalk.bold('/resources/' + platformName)}`);
                try {
                    yield resources_1.addDefaultImagesToProjectResources(this.env.project.directory, platformName);
                }
                catch (e) {
                    throw e;
                }
            }
            tasks.end();
        });
    }
};
PlatformCommand = __decorate([
    cli_utils_1.CommandMetadata({
        name: 'platform',
        description: 'Add or remove a platform target for building an Ionic app',
        exampleCommands: ['add android'],
        inputs: [
            {
                name: 'action',
                description: 'Add or remove the platform',
                validators: [cli_utils_1.validators.required],
                prompt: {
                    type: 'list',
                    choices: ['add', 'remove']
                }
            },
            {
                name: 'platform',
                description: 'the platform that you would like to build (ex: ios, android)',
                validators: [cli_utils_1.validators.required],
                prompt: {
                    message: 'What platform would you like to build? (ex: ios, android)'
                }
            }
        ],
        options: [
            {
                name: 'noresources',
                description: 'Do not add default Ionic icons and splash screen resources',
                type: Boolean,
                default: false,
                aliases: ['r']
            },
            {
                name: 'nosave',
                description: 'Do not save the platform to the package.json file',
                type: Boolean,
                aliases: ['e']
            }
        ]
    }),
    __metadata("design:paramtypes", [])
], PlatformCommand);
exports.PlatformCommand = PlatformCommand;
