"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 DebugPlatformCommand {
    constructor(debugService, $devicesService, $injector, $logger, $devicePlatformsConstants, $config, $usbLiveSyncService, $platformService, $options, $platformsData) {
        this.debugService = debugService;
        this.$devicesService = $devicesService;
        this.$injector = $injector;
        this.$logger = $logger;
        this.$devicePlatformsConstants = $devicePlatformsConstants;
        this.$config = $config;
        this.$usbLiveSyncService = $usbLiveSyncService;
        this.$platformService = $platformService;
        this.$options = $options;
        this.$platformsData = $platformsData;
        this.allowedParameters = [];
    }
    execute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (this.$options.start) {
                return this.debugService.debug();
            }
            yield this.$platformService.deployPlatform(this.$devicesService.platform);
            this.$config.debugLivesync = true;
            let applicationReloadAction = (deviceAppData) => __awaiter(this, void 0, void 0, function* () {
                let projectData = this.$injector.resolve("projectData");
                yield this.debugService.debugStop();
                let applicationId = deviceAppData.appIdentifier;
                if (deviceAppData.device.isEmulator && deviceAppData.platform.toLowerCase() === this.$devicePlatformsConstants.iOS.toLowerCase()) {
                    applicationId = projectData.projectName;
                }
                yield deviceAppData.device.applicationManager.stopApplication(applicationId);
                yield this.debugService.debug();
            });
            return this.$usbLiveSyncService.liveSync(this.$devicesService.platform, applicationReloadAction);
        });
    }
    canExecute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.$devicesService.initialize({ platform: this.debugService.platform, deviceId: this.$options.device });
            if (this.$options.emulator || this.$devicesService.deviceCount === 0) {
                return true;
            }
            if (this.$devicesService.deviceCount > 1) {
                this.$options.emulator = true;
                this.$logger.warn("Multiple devices found! Starting debugger on emulator. If you want to debug on specific device please select device with --device option.".yellow.bold);
            }
            return true;
        });
    }
}
exports.DebugPlatformCommand = DebugPlatformCommand;
class DebugIOSCommand extends DebugPlatformCommand {
    constructor($iOSDebugService, $devicesService, $injector, $logger, $devicePlatformsConstants, $config, $usbLiveSyncService, $platformService, $options, $platformsData) {
        super($iOSDebugService, $devicesService, $injector, $logger, $devicePlatformsConstants, $config, $usbLiveSyncService, $platformService, $options, $platformsData);
    }
    canExecute(args) {
        const _super = name => super[name];
        return __awaiter(this, void 0, void 0, function* () {
            return (yield _super("canExecute").call(this, args)) && (yield this.$platformService.validateOptions(this.$platformsData.availablePlatforms.iOS));
        });
    }
}
exports.DebugIOSCommand = DebugIOSCommand;
$injector.registerCommand("debug|ios", DebugIOSCommand);
class DebugAndroidCommand extends DebugPlatformCommand {
    constructor($androidDebugService, $devicesService, $injector, $logger, $devicePlatformsConstants, $config, $usbLiveSyncService, $platformService, $options, $platformsData) {
        super($androidDebugService, $devicesService, $injector, $logger, $devicePlatformsConstants, $config, $usbLiveSyncService, $platformService, $options, $platformsData);
    }
    canExecute(args) {
        const _super = name => super[name];
        return __awaiter(this, void 0, void 0, function* () {
            return (yield _super("canExecute").call(this, args)) && (yield this.$platformService.validateOptions(this.$platformsData.availablePlatforms.Android));
        });
    }
}
exports.DebugAndroidCommand = DebugAndroidCommand;
$injector.registerCommand("debug|android", DebugAndroidCommand);
