"use strict";
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());
    });
};
class BuildCommandBase {
    constructor($options, $projectData, $platformsData, $platformService) {
        this.$options = $options;
        this.$projectData = $projectData;
        this.$platformsData = $platformsData;
        this.$platformService = $platformService;
        this.$projectData.initializeProjectData();
    }
    executeCore(args) {
        return __awaiter(this, void 0, void 0, function* () {
            let platform = args[0].toLowerCase();
            const appFilesUpdaterOptions = { bundle: this.$options.bundle, release: this.$options.release };
            yield this.$platformService.preparePlatform(platform, appFilesUpdaterOptions, this.$options.platformTemplate, this.$projectData, { provision: this.$options.provision, sdk: this.$options.sdk });
            this.$options.clean = true;
            const buildConfig = {
                buildForDevice: this.$options.forDevice,
                projectDir: this.$options.path,
                clean: this.$options.clean,
                teamId: this.$options.teamId,
                device: this.$options.device,
                provision: this.$options.provision,
                release: this.$options.release,
                keyStoreAlias: this.$options.keyStoreAlias,
                keyStorePath: this.$options.keyStorePath,
                keyStoreAliasPassword: this.$options.keyStoreAliasPassword,
                keyStorePassword: this.$options.keyStorePassword
            };
            yield this.$platformService.buildPlatform(platform, buildConfig, this.$projectData);
            if (this.$options.copyTo) {
                this.$platformService.copyLastOutput(platform, this.$options.copyTo, { isForDevice: this.$options.forDevice }, this.$projectData);
            }
        });
    }
}
exports.BuildCommandBase = BuildCommandBase;
class BuildIosCommand extends BuildCommandBase {
    constructor($options, $projectData, $platformsData, $platformService) {
        super($options, $projectData, $platformsData, $platformService);
        this.$options = $options;
        this.allowedParameters = [];
    }
    execute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.executeCore([this.$platformsData.availablePlatforms.iOS]);
        });
    }
    canExecute(args) {
        return args.length === 0 && this.$platformService.validateOptions(this.$options.provision, this.$projectData, this.$platformsData.availablePlatforms.iOS);
    }
}
exports.BuildIosCommand = BuildIosCommand;
$injector.registerCommand("build|ios", BuildIosCommand);
class BuildAndroidCommand extends BuildCommandBase {
    constructor($options, $projectData, $platformsData, $errors, $platformService) {
        super($options, $projectData, $platformsData, $platformService);
        this.$options = $options;
        this.$errors = $errors;
        this.allowedParameters = [];
    }
    execute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.executeCore([this.$platformsData.availablePlatforms.Android]);
        });
    }
    canExecute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (this.$options.release && (!this.$options.keyStorePath || !this.$options.keyStorePassword || !this.$options.keyStoreAlias || !this.$options.keyStoreAliasPassword)) {
                this.$errors.fail("When producing a release build, you need to specify all --key-store-* options.");
            }
            return args.length === 0 && (yield this.$platformService.validateOptions(this.$options.provision, this.$projectData, this.$platformsData.availablePlatforms.Android));
        });
    }
}
exports.BuildAndroidCommand = BuildAndroidCommand;
$injector.registerCommand("build|android", BuildAndroidCommand);
