"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const decorators_1 = require("../common/decorators");
const command_base_1 = require("./command-base");
const livesync_command_helper_1 = require("../helpers/livesync-command-helper");
class DebugPlatformCommand extends command_base_1.ValidatePlatformCommandBase {
    constructor(platform, $bundleValidatorHelper, $devicesService, $platformValidationService, $projectData, $options, $platformsDataService, $logger, $errors, $debugDataService, $debugController, $liveSyncCommandHelper, $androidBundleValidatorHelper) {
        super($options, $platformsDataService, $platformValidationService, $projectData);
        this.platform = platform;
        this.$bundleValidatorHelper = $bundleValidatorHelper;
        this.$devicesService = $devicesService;
        this.$logger = $logger;
        this.$errors = $errors;
        this.$debugDataService = $debugDataService;
        this.$debugController = $debugController;
        this.$liveSyncCommandHelper = $liveSyncCommandHelper;
        this.$androidBundleValidatorHelper = $androidBundleValidatorHelper;
        this.allowedParameters = [];
    }
    execute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.$devicesService.initialize({
                platform: this.platform,
                deviceId: this.$options.device,
                emulator: this.$options.emulator,
                skipDeviceDetectionInterval: true
            });
            const selectedDeviceForDebug = yield this.$devicesService.pickSingleDevice({
                onlyEmulators: this.$options.emulator,
                onlyDevices: this.$options.forDevice,
                deviceId: this.$options.device
            });
            if (this.$options.start) {
                const debugOptions = _.cloneDeep(this.$options.argv);
                const debugData = this.$debugDataService.getDebugData(selectedDeviceForDebug.deviceInfo.identifier, this.$projectData, debugOptions);
                yield this.$debugController.printDebugInformation(yield this.$debugController.startDebug(debugData));
                return;
            }
            yield this.$liveSyncCommandHelper.executeLiveSyncOperation([selectedDeviceForDebug], this.platform, {
                deviceDebugMap: {
                    [selectedDeviceForDebug.deviceInfo.identifier]: true
                },
                buildPlatform: undefined,
                skipNativePrepare: false
            });
        });
    }
    canExecute(args) {
        const _super = Object.create(null, {
            canExecuteCommandBase: { get: () => super.canExecuteCommandBase }
        });
        return __awaiter(this, void 0, void 0, function* () {
            this.$androidBundleValidatorHelper.validateNoAab();
            if (!this.$platformValidationService.isPlatformSupportedForOS(this.platform, this.$projectData)) {
                this.$errors.fail(`Applications for platform ${this.platform} can not be built on this OS`);
            }
            if (this.$options.release) {
                this.$errors.fail("--release flag is not applicable to this command");
            }
            const minSupportedWebpackVersion = this.$options.hmr ? livesync_command_helper_1.LiveSyncCommandHelper.MIN_SUPPORTED_WEBPACK_VERSION_WITH_HMR : null;
            this.$bundleValidatorHelper.validate(this.$projectData, minSupportedWebpackVersion);
            const result = yield _super.canExecuteCommandBase.call(this, this.platform, { validateOptions: true, notConfiguredEnvOptions: { hideCloudBuildOption: true, hideSyncToPreviewAppOption: true } });
            return result;
        });
    }
}
exports.DebugPlatformCommand = DebugPlatformCommand;
class DebugIOSCommand {
    constructor($errors, $devicePlatformsConstants, $platformValidationService, $options, $injector, $sysInfo, $projectData, $iosDeviceOperations, $iOSSimulatorLogProvider, $cleanupService) {
        this.$errors = $errors;
        this.$devicePlatformsConstants = $devicePlatformsConstants;
        this.$platformValidationService = $platformValidationService;
        this.$options = $options;
        this.$injector = $injector;
        this.$sysInfo = $sysInfo;
        this.$projectData = $projectData;
        this.allowedParameters = [];
        this.platform = this.$devicePlatformsConstants.iOS;
        this.$projectData.initializeProjectData();
        $iosDeviceOperations.setShouldDispose(false);
        $iOSSimulatorLogProvider.setShouldDispose(false);
        $cleanupService.setShouldDispose(false);
    }
    get debugPlatformCommand() {
        return this.$injector.resolve(DebugPlatformCommand, { platform: this.platform });
    }
    execute(args) {
        return this.debugPlatformCommand.execute(args);
    }
    canExecute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!this.$platformValidationService.isPlatformSupportedForOS(this.$devicePlatformsConstants.iOS, this.$projectData)) {
                this.$errors.fail(`Applications for platform ${this.$devicePlatformsConstants.iOS} can not be built on this OS`);
            }
            const isValidTimeoutOption = this.isValidTimeoutOption();
            if (!isValidTimeoutOption) {
                this.$errors.fail(`Timeout option specifies the seconds NativeScript CLI will wait to find the inspector socket port from device's logs. Must be a number.`);
            }
            if (this.$options.inspector) {
                const macOSWarning = yield this.$sysInfo.getMacOSWarningMessage();
                if (macOSWarning && macOSWarning.severity === "high") {
                    this.$errors.fail(`You cannot use NativeScript Inspector on this OS. To use it, please update your OS.`);
                }
            }
            const result = yield this.debugPlatformCommand.canExecute(args);
            return result;
        });
    }
    isValidTimeoutOption() {
        if (!this.$options.timeout) {
            return true;
        }
        const timeout = parseInt(this.$options.timeout, 10);
        if (timeout === 0) {
            return true;
        }
        if (!timeout) {
            return false;
        }
        return true;
    }
}
__decorate([
    decorators_1.cache()
], DebugIOSCommand.prototype, "debugPlatformCommand", null);
exports.DebugIOSCommand = DebugIOSCommand;
$injector.registerCommand("debug|ios", DebugIOSCommand);
class DebugAndroidCommand {
    constructor($errors, $devicePlatformsConstants, $injector, $projectData) {
        this.$errors = $errors;
        this.$devicePlatformsConstants = $devicePlatformsConstants;
        this.$injector = $injector;
        this.$projectData = $projectData;
        this.allowedParameters = [];
        this.platform = this.$devicePlatformsConstants.Android;
        this.$projectData.initializeProjectData();
    }
    get debugPlatformCommand() {
        return this.$injector.resolve(DebugPlatformCommand, { platform: this.platform });
    }
    execute(args) {
        return this.debugPlatformCommand.execute(args);
    }
    canExecute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            const result = yield this.debugPlatformCommand.canExecute(args);
            return result;
        });
    }
}
__decorate([
    decorators_1.cache()
], DebugAndroidCommand.prototype, "debugPlatformCommand", null);
exports.DebugAndroidCommand = DebugAndroidCommand;
$injector.registerCommand("debug|android", DebugAndroidCommand);
