"use strict";
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;
    }
    async deploy(platform, additionalOptions) {
        const emulator = this.$options.emulator;
        await 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, {
                ...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;
        });
        await this.$deployController.deploy({ deviceDescriptors });
    }
}
exports.DeployCommandHelper = DeployCommandHelper;
yok_1.injector.register("deployCommandHelper", DeployCommandHelper);
