'use strict';
import { cliLogSubtitle, cliLogError } from '../../../fd-toolbox/logging/cli-logger.js';
import { spawn } from 'node:child_process';
import { createError } from '../../../fd-toolbox/errors/errors.js';
import { stdioStatuses } from '../../../base-repo/constants/tools-constants.js';
import { streamEventNames } from '../../../base-repo/constants/internal-common-strings.js';
import { childProcessEvents } from '../../../fd-toolbox/constants/child-process-events.js';
import { processSignals } from '../../../base-repo/app-constants/internal-system-constants.js';

var __defProp=Object['defineProperty'],__name=(_0x1c0ca6,_0x515f0f)=>__defProp(_0x1c0ca6,'name',{'value':_0x515f0f,'configurable':!![]});const serverReadyMessage='[Server\x20ready]\x20>\x20Server\x20listening\x20at';async function waitForServerReady(_0x1b1d06,_0x156e2a,_0xe06220){const [_0x3d0376,..._0x5c545b]=_0x156e2a['split']('\x20'),_0x35e037=spawn(_0x3d0376,_0x5c545b,{'cwd':_0x1b1d06,'stdio':[stdioStatuses['ignore'],stdioStatuses['pipe'],stdioStatuses['pipe']],'detached':!![]});return new Promise((_0x5a3181,_0x550201)=>{attachServerListeners(_0x35e037,_0x5a3181,_0x550201,_0xe06220);});}__name(waitForServerReady,'waitForServerReady');function attachServerListeners(_0x453391,_0x4e6ca4,_0x129293,_0x76daa4=serverReadyMessage){let _0x15e8e7=![];_0x453391['stdout']?.['on'](streamEventNames['data'],_0x484b6a=>{const _0x2fcb76=_0x484b6a['toString']();cliLogSubtitle(_0x2fcb76),_0x2fcb76['includes'](_0x76daa4)&&!_0x15e8e7&&(_0x15e8e7=!![],_0x4e6ca4(_0x453391));}),_0x453391['stderr']?.['on'](streamEventNames['data'],_0x4b5874=>{cliLogError(_0x4b5874['toString']());}),_0x453391['on'](childProcessEvents['exit'],_0xff2fad=>{!_0x15e8e7&&(_0x15e8e7=!![],_0x129293(_0xff2fad));}),_0x453391['on'](childProcessEvents['exit'],_0x11fd6a=>{!_0x15e8e7&&(_0x15e8e7=!![],_0x129293(createError('Server\x20process\x20exited\x20with\x20code\x20'+_0x11fd6a+'\x20before\x20reporting\x20readiness')));});}__name(attachServerListeners,'attachServerListeners');async function terminateServerProcess(_0x43abd2){if(_0x43abd2?.['pid']&&!_0x43abd2['killed']){cliLogSubtitle('Terminating\x20server\x20process...');try{process['kill'](-_0x43abd2['pid'],processSignals['gracefulTerminate']),await new Promise(_0x461a6c=>{_0x43abd2['once'](childProcessEvents['exit'],()=>{cliLogSubtitle('Server\x20process\x20terminated\x20successfully'),_0x461a6c();});});}catch(_0x2426bf){cliLogError('Failed\x20to\x20kill\x20process:\x20'+_0x2426bf);}}}__name(terminateServerProcess,'terminateServerProcess');

export { attachServerListeners, serverReadyMessage, terminateServerProcess, waitForServerReady };
