"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    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) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeployCommandHelper = void 0;
const yok_1 = require("../common/yok");
class DeployCommandHelper {
    constructor($androidBundleValidatorHelper, $buildDataService, $buildController, $devicesService, $deployController, $options, $projectData) {
        this.$androidBundleValidatorHelper = $androidBundleValidatorHelper;
        this.$buildDataService = $buildDataService;
        this.$buildController = $buildController;
        this.$devicesService = $devicesService;
        this.$deployController = $deployController;
        this.$options = $options;
        this.$projectData = $projectData;
    }
    deploy(platform, additionalOptions) {
        return __awaiter(this, void 0, void 0, function* () {
            const emulator = this.$options.emulator;
            yield this.$devicesService.initialize({
                deviceId: this.$options.device,
                platform,
                emulator,
                skipInferPlatform: !platform,
                sdk: this.$options.sdk,
            });
            const devices = this.$devicesService
                .getDeviceInstances()
                .filter((d) => !platform ||
                d.deviceInfo.platform.toLowerCase() === platform.toLowerCase());
            const deviceDescriptors = devices.map((d) => {
                const outputPath = additionalOptions &&
                    additionalOptions.getOutputDirectory &&
                    additionalOptions.getOutputDirectory({
                        platform: d.deviceInfo.platform,
                        emulator: d.isEmulator,
                        projectDir: this.$projectData.projectDir,
                    });
                const buildData = this.$buildDataService.getBuildData(this.$projectData.projectDir, d.deviceInfo.platform, Object.assign(Object.assign({}, this.$options.argv), { outputPath, buildForDevice: !d.isEmulator, skipWatcher: !this.$options.watch, nativePrepare: {
                        skipNativePrepare: additionalOptions && additionalOptions.skipNativePrepare,
                    } }));
                this.$androidBundleValidatorHelper.validateDeviceApiLevel(d, buildData);
                const buildAction = additionalOptions && additionalOptions.buildPlatform
                    ? additionalOptions.buildPlatform.bind(additionalOptions.buildPlatform, d.deviceInfo.platform, buildData, this.$projectData)
                    : this.$buildController.build.bind(this.$buildController, buildData);
                const info = {
                    identifier: d.deviceInfo.identifier,
                    buildAction,
                    debuggingEnabled: additionalOptions &&
                        additionalOptions.deviceDebugMap &&
                        additionalOptions.deviceDebugMap[d.deviceInfo.identifier],
                    debugOptions: this.$options,
                    skipNativePrepare: additionalOptions && additionalOptions.skipNativePrepare,
                    buildData,
                };
                return info;
            });
            yield this.$deployController.deploy({ deviceDescriptors });
        });
    }
}
exports.DeployCommandHelper = DeployCommandHelper;
yok_1.injector.register("deployCommandHelper", DeployCommandHelper);
//# sourceMappingURL=deploy-command-helper.js.map