"use strict";
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;
    }
    async runForPlatform(platform) {
        const platformLowerCase = platform.toLowerCase();
        this[platformLowerCase] = this.$childProcess.spawn("node", [this.$staticConfig.cliBinPath, "run", platform.toLowerCase()], {
            cwd: this.$projectData.projectDir,
            stdio: ["ipc"],
            env: {
                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));
        });
    }
    async runIOS() {
        this.runForPlatform(this.$devicePlatformsConstants.iOS);
    }
    async runVisionOS() {
        this.runForPlatform(this.$devicePlatformsConstants.visionOS);
    }
    async runAndroid() {
        this.runForPlatform(this.$devicePlatformsConstants.Android);
    }
    async stopIOS() {
        if (this.ios) {
            this.ios.kill("SIGINT");
        }
    }
    async stopVisionOS() {
        if (this.visionos) {
            this.visionos.kill("SIGINT");
        }
    }
    async stopAndroid() {
        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, async () => {
                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", async () => {
            await this.stopIOS();
            await 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);
