"use strict";
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var DebugPlatformCommand = (function () {
    function DebugPlatformCommand(debugService, $devicesService, $injector, $logger, $childProcess, $devicePlatformsConstants, $config, $usbLiveSyncService, $options) {
        this.debugService = debugService;
        this.$devicesService = $devicesService;
        this.$injector = $injector;
        this.$logger = $logger;
        this.$childProcess = $childProcess;
        this.$devicePlatformsConstants = $devicePlatformsConstants;
        this.$config = $config;
        this.$usbLiveSyncService = $usbLiveSyncService;
        this.$options = $options;
        this.allowedParameters = [];
    }
    DebugPlatformCommand.prototype.execute = function (args) {
        var _this = this;
        if (this.$options.watch) {
            this.$options.rebuild = false;
        }
        if (!this.$options.rebuild && !this.$options.start) {
            this.$config.debugLivesync = true;
            var applicationReloadAction = function (deviceAppData, localToDevicePaths) {
                return (function () {
                    var projectData = _this.$injector.resolve("projectData");
                    _this.$logger.info("Successfully synced application " + deviceAppData.appIdentifier + " on device " + deviceAppData.device.deviceInfo.identifier + ".");
                    _this.debugService.debugStop().wait();
                    var applicationId = deviceAppData.appIdentifier;
                    if (deviceAppData.device.isEmulator && deviceAppData.platform.toLowerCase() === _this.$devicePlatformsConstants.iOS.toLowerCase()) {
                        applicationId = projectData.projectName;
                    }
                    deviceAppData.device.applicationManager.stopApplication(applicationId).wait();
                    _this.debugService.debug().wait();
                }).future()();
            };
            return this.$usbLiveSyncService.liveSync(this.$devicesService.platform, applicationReloadAction);
        }
        return this.debugService.debug();
    };
    DebugPlatformCommand.prototype.canExecute = function (args) {
        var _this = this;
        return (function () {
            _this.$devicesService.initialize({ platform: _this.debugService.platform, deviceId: _this.$options.device }).wait();
            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;
        }).future()();
    };
    return DebugPlatformCommand;
}());
exports.DebugPlatformCommand = DebugPlatformCommand;
var DebugIOSCommand = (function (_super) {
    __extends(DebugIOSCommand, _super);
    function DebugIOSCommand($iOSDebugService, $devicesService, $injector, $logger, $childProcess, $devicePlatformsConstants, $config, $usbLiveSyncService, $options) {
        _super.call(this, $iOSDebugService, $devicesService, $injector, $logger, $childProcess, $devicePlatformsConstants, $config, $usbLiveSyncService, $options);
    }
    return DebugIOSCommand;
}(DebugPlatformCommand));
exports.DebugIOSCommand = DebugIOSCommand;
$injector.registerCommand("debug|ios", DebugIOSCommand);
var DebugAndroidCommand = (function (_super) {
    __extends(DebugAndroidCommand, _super);
    function DebugAndroidCommand($androidDebugService, $devicesService, $injector, $logger, $childProcess, $devicePlatformsConstants, $config, $usbLiveSyncService, $options) {
        _super.call(this, $androidDebugService, $devicesService, $injector, $logger, $childProcess, $devicePlatformsConstants, $config, $usbLiveSyncService, $options);
    }
    return DebugAndroidCommand;
}(DebugPlatformCommand));
exports.DebugAndroidCommand = DebugAndroidCommand;
$injector.registerCommand("debug|android", DebugAndroidCommand);
