"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeCoreServerProcess = void 0;
const child_process_1 = require("child_process");
const path_1 = __importDefault(require("path"));
function makeCoreServerProcess(options) {
    return new Promise((resolve, reject) => {
        const server = (0, child_process_1.fork)(path_1.default.resolve(__dirname, '../../dist/cli/cli.js'), ['universal', `--config=${JSON.stringify(options)}`], {
            stdio: [options.shutdownMode === 'stdin' ? 'inherit' : 'ignore', 'ignore', 'ignore', 'ipc'],
            ...options.forkOptions,
        });
        const timeout = setTimeout(() => {
            reject(new Error(`Server didn't respond for 10s after being started`));
            server.kill();
        }, 60000);
        server.on('error', reject);
        server.once('message', ({ name, payload }) => {
            var _a;
            if (name === 'port') {
                resolve({ port: payload.port, close: () => server.kill() });
                clearTimeout(timeout);
                (_a = server.channel) === null || _a === void 0 ? void 0 : _a.unref();
            }
        });
        server.unref();
    });
}
exports.makeCoreServerProcess = makeCoreServerProcess;
