"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 readline = require("readline");
const helpers = require("../common/helpers");
class PreviewCommandHelper {
    constructor($playgroundQrCodeGenerator, $processService) {
        this.$playgroundQrCodeGenerator = $playgroundQrCodeGenerator;
        this.$processService = $processService;
        this.$processService.attachToProcessExitSignals(this, () => {
            this.stopWaitingForCommand();
        });
    }
    run() {
        this.startWaitingForCommand();
    }
    startWaitingForCommand() {
        if (helpers.isInteractive()) {
            this.ctrlcReader = readline.createInterface({
                input: process.stdin,
                output: process.stdout
            });
            this.ctrlcReader.on("SIGINT", process.exit);
            process.stdin.setRawMode(true);
            process.stdin.resume();
            process.stdin.setEncoding("utf8");
            process.stdin.on("data", this.handleKeypress.bind(this));
        }
    }
    stopWaitingForCommand() {
        if (helpers.isInteractive()) {
            process.stdin.removeListener("data", this.handleKeypress);
            process.stdin.setRawMode(false);
            process.stdin.resume();
            this.closeCtrlcReader();
        }
    }
    handleKeypress(key) {
        return __awaiter(this, void 0, void 0, function* () {
            switch (key) {
                case "a":
                    yield this.$playgroundQrCodeGenerator.generateQrCodeForAndroid();
                    return;
                case "i":
                    yield this.$playgroundQrCodeGenerator.generateQrCodeForiOS();
                    return;
                case "c":
                    yield this.$playgroundQrCodeGenerator.generateQrCodeForCurrentApp();
                    return;
            }
        });
    }
    closeCtrlcReader() {
        if (this.ctrlcReader) {
            this.ctrlcReader.close();
        }
    }
}
exports.PreviewCommandHelper = PreviewCommandHelper;
$injector.register("previewCommandHelper", PreviewCommandHelper);
