"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = startProcess;
const fs_1 = __importDefault(require("fs"));
const console_colors_1 = __importDefault(require("./console-colors"));
const preflight_1 = __importDefault(require("./preflight"));
const run_1 = __importDefault(require("./run"));
const kill_child_1 = __importDefault(require("./kill-child"));
let redeployments = 0;
let childProcess = null;
const pTitle = "nodecid";
process.title = pTitle;
/**
 * # Start the process
 * @param {object} param0
 * @param {string} param0.command
 * @param {string[] | string} param0.preflight
 * @param {string[] | string} [param0.postflight]
 * @param {string} param0.redeploy_file
 * @param {string | number | (string | number)[]} [param0.port] - The port to kill on rebuild
 * @param {boolean} [param0.first_run] - Whether to run the preflight on first run. Default `false`
 */
function startProcess({ command, preflight, postflight, redeploy_file, port, first_run, }) {
    try {
        if (first_run) {
            console.log("First Run ...");
            const runPreflight = (0, preflight_1.default)(preflight);
        }
        if (!preflight) {
            console.log(`${console_colors_1.default.FgRed}Error:${console_colors_1.default.Reset} No preflight included in config file. If you don't want to run any preflight command simply add an empty array.`);
            process.exit();
        }
        childProcess = (0, run_1.default)(command);
        if (!childProcess) {
            console.log(`${console_colors_1.default.FgRed}Error:${console_colors_1.default.Reset} Process couldn't start. Exiting...`);
            process.exit();
        }
        console.log("Watching", redeploy_file);
        fs_1.default.watchFile(redeploy_file, { interval: 100 }, (curr, prev) => {
            console.log(`${console_colors_1.default.BgBlue}File Changed${console_colors_1.default.Reset}`);
            if (redeployments == 0)
                return;
            if (childProcess) {
                console.log("******************************");
                console.log(`******** ${console_colors_1.default.FgBlue}Rebuilding ${console_colors_1.default.FgMagenta}${redeployments}${console_colors_1.default.Reset} ********`);
                console.log("******************************");
                try {
                    const runPreflight = (0, preflight_1.default)(preflight);
                    if (!runPreflight) {
                        // TODO: Action to take if preflight fails
                        console.log(`${console_colors_1.default.FgRed}Error:${console_colors_1.default.Reset} Preflight Failed.`);
                    }
                    else {
                        (0, kill_child_1.default)(port).then((kill) => {
                            if (kill) {
                                childProcess = (0, run_1.default)(command);
                                if (postflight) {
                                    const runPostflight = (0, preflight_1.default)(postflight, true);
                                    if (!runPostflight) {
                                        // TODO: Action to take if postflight fails
                                        console.log(`${console_colors_1.default.FgRed}Error:${console_colors_1.default.Reset} Postflight Failed.`);
                                    }
                                }
                            }
                            else {
                                process.exit();
                            }
                        });
                    }
                }
                catch (error) {
                    console.log(`${console_colors_1.default.FgRed}Error:${console_colors_1.default.Reset} killing child processes => ${error.message}`);
                    process.exit();
                }
            }
        });
    }
    catch (error) {
        console.log(`${console_colors_1.default.FgRed}Error:${console_colors_1.default.Reset} First run failed! => ${error.message}`);
    }
}
