"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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = killChild;
const console_colors_1 = __importDefault(require("./console-colors"));
const kill_port_1 = __importDefault(require("kill-port"));
let childProcess = null;
const pTitle = "nodecid";
process.title = pTitle;
/**
 * ## Kill Child Process Function
 * @param {string | number | (string | number)[]} [port]
 * @returns {Promise<boolean>}
 */
function killChild(port) {
    return __awaiter(this, void 0, void 0, function* () {
        if (!childProcess)
            return false;
        try {
            const childProcessPID = childProcess.pid;
            childProcess.kill();
            if (typeof port == "object" && (port === null || port === void 0 ? void 0 : port[0])) {
                for (let i = 0; i < port.length; i++) {
                    const singlePort = port[i];
                    yield (0, kill_port_1.default)(Number(singlePort));
                }
            }
            else if (port) {
                yield (0, kill_port_1.default)(Number(port));
            }
            return true;
        }
        catch (error) {
            console.log(`${console_colors_1.default.FgRed}Error:${console_colors_1.default.Reset} Child Process couldn't be killed! ${error.message}`);
            return false;
        }
    });
}
