"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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const constants_1 = require("../common/constants");
class PreviewCommand {
    constructor($analyticsService, $bundleValidatorHelper, $errors, $logger, $previewAppController, $networkConnectivityValidator, $projectData, $options, $previewAppLogProvider, $previewQrCodeService, $cleanupService) {
        this.$analyticsService = $analyticsService;
        this.$bundleValidatorHelper = $bundleValidatorHelper;
        this.$errors = $errors;
        this.$logger = $logger;
        this.$previewAppController = $previewAppController;
        this.$networkConnectivityValidator = $networkConnectivityValidator;
        this.$projectData = $projectData;
        this.$options = $options;
        this.$previewAppLogProvider = $previewAppLogProvider;
        this.$previewQrCodeService = $previewQrCodeService;
        this.allowedParameters = [];
        this.$analyticsService.setShouldDispose(false);
        $cleanupService.setShouldDispose(false);
    }
    execute() {
        return __awaiter(this, void 0, void 0, function* () {
            this.$previewAppLogProvider.on(constants_1.DEVICE_LOG_EVENT_NAME, (deviceId, message) => {
                this.$logger.info(message);
            });
            yield this.$previewAppController.startPreview({
                projectDir: this.$projectData.projectDir,
                useHotModuleReload: this.$options.hmr,
                env: this.$options.env
            });
            yield this.$previewQrCodeService.printLiveSyncQrCode({
                projectDir: this.$projectData.projectDir,
                useHotModuleReload: this.$options.hmr,
                link: this.$options.link
            });
        });
    }
    canExecute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args && args.length) {
                this.$errors.fail(`The arguments '${args.join(" ")}' are not valid for the preview command.`);
            }
            yield this.$networkConnectivityValidator.validate();
            this.$bundleValidatorHelper.validate(this.$projectData, "1.0.0");
            return true;
        });
    }
}
exports.PreviewCommand = PreviewCommand;
$injector.registerCommand("preview", PreviewCommand);
