"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($bundleValidatorHelper, $errors, $liveSyncService, $logger, $networkConnectivityValidator, $projectData, $options, $previewAppLogProvider, $previewQrCodeService) {
        this.$bundleValidatorHelper = $bundleValidatorHelper;
        this.$errors = $errors;
        this.$liveSyncService = $liveSyncService;
        this.$logger = $logger;
        this.$networkConnectivityValidator = $networkConnectivityValidator;
        this.$projectData = $projectData;
        this.$options = $options;
        this.$previewAppLogProvider = $previewAppLogProvider;
        this.$previewQrCodeService = $previewQrCodeService;
        this.allowedParameters = [];
    }
    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.$liveSyncService.liveSyncToPreviewApp({
                bundle: !!this.$options.bundle,
                useHotModuleReload: this.$options.hmr,
                projectDir: this.$projectData.projectDir,
                env: this.$options.env
            });
            yield this.$previewQrCodeService.printLiveSyncQrCode({ 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(PreviewCommand.MIN_SUPPORTED_WEBPACK_VERSION);
            return true;
        });
    }
}
PreviewCommand.MIN_SUPPORTED_WEBPACK_VERSION = "0.17.0";
exports.PreviewCommand = PreviewCommand;
$injector.registerCommand("preview", PreviewCommand);
