"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    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) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const yok_1 = require("../common/yok");
class StartService {
    constructor($keyCommandHelper, $childProcess, $devicePlatformsConstants, $projectData, $logger, $staticConfig) {
        this.$keyCommandHelper = $keyCommandHelper;
        this.$childProcess = $childProcess;
        this.$devicePlatformsConstants = $devicePlatformsConstants;
        this.$projectData = $projectData;
        this.$logger = $logger;
        this.$staticConfig = $staticConfig;
        this.verbose = false;
    }
    toggleVerbose() {
        this.verbose = true;
        this.$logger.info(this.verbose ? `Verbose logging enabled` : `Verbose logging disabled`);
    }
    format(data, platform) {
        return data;
    }
    runForPlatform(platform) {
        return __awaiter(this, void 0, void 0, function* () {
            const platformLowerCase = platform.toLowerCase();
            this[platformLowerCase] = this.$childProcess.spawn("node", [this.$staticConfig.cliBinPath, "run", platform.toLowerCase()], {
                cwd: this.$projectData.projectDir,
                stdio: ["ipc"],
                env: Object.assign({ FORCE_COLOR: 1, HIDE_HEADER: true, NS_IS_INTERACTIVE: true }, process.env),
            });
            this[platformLowerCase].stdout.on("data", (data) => {
                process.stdout.write(this.format(data, platform));
            });
            this[platformLowerCase].stderr.on("data", (data) => {
                process.stderr.write(this.format(data, platform));
            });
        });
    }
    runIOS() {
        return __awaiter(this, void 0, void 0, function* () {
            this.runForPlatform(this.$devicePlatformsConstants.iOS);
        });
    }
    runVisionOS() {
        return __awaiter(this, void 0, void 0, function* () {
            this.runForPlatform(this.$devicePlatformsConstants.visionOS);
        });
    }
    runAndroid() {
        return __awaiter(this, void 0, void 0, function* () {
            this.runForPlatform(this.$devicePlatformsConstants.Android);
        });
    }
    stopIOS() {
        return __awaiter(this, void 0, void 0, function* () {
            if (this.ios) {
                this.ios.kill("SIGINT");
            }
        });
    }
    stopVisionOS() {
        return __awaiter(this, void 0, void 0, function* () {
            if (this.visionos) {
                this.visionos.kill("SIGINT");
            }
        });
    }
    stopAndroid() {
        return __awaiter(this, void 0, void 0, function* () {
            if (this.android) {
                this.android.kill("SIGINT");
            }
        });
    }
    start() {
        this.addKeyCommandOverrides();
        this.$keyCommandHelper.attachKeyCommands("all", "start");
        this.$keyCommandHelper.printCommands("all");
    }
    addKeyCommandOverrides() {
        const keys = ["w", "r", "R"];
        for (let key of keys) {
            this.$keyCommandHelper.addOverride(key, () => __awaiter(this, void 0, void 0, function* () {
                var _a, _b;
                (_a = this.ios) === null || _a === void 0 ? void 0 : _a.send(key);
                (_b = this.android) === null || _b === void 0 ? void 0 : _b.send(key);
                return false;
            }));
        }
        this.$keyCommandHelper.addOverride("c", () => __awaiter(this, void 0, void 0, function* () {
            yield this.stopIOS();
            yield this.stopAndroid();
            const clean = this.$childProcess.spawn("node", [
                this.$staticConfig.cliBinPath,
                "clean",
            ]);
            clean.stdout.on("data", (data) => {
                process.stdout.write(data);
                if (data.toString().includes("Project successfully cleaned.") ||
                    data.toString().includes("Project unsuccessfully cleaned.")) {
                    clean.kill("SIGINT");
                }
            });
            return false;
        }));
    }
}
exports.default = StartService;
yok_1.injector.register("startService", StartService);
//# sourceMappingURL=start-service.js.map