/**
 * Production dependencies factory for DAP Proxy
 */
import { spawn } from 'child_process';
import fs from 'fs-extra';
import path from 'path';
import { MinimalDapClient } from './minimal-dap.js';
import { createLogger } from '../utils/logger.js';
/**
 * Create production dependencies for the DAP Proxy Worker
 */
export function createProductionDependencies() {
    // Logger factory for delayed initialization
    const loggerFactory = async (sessionId, logDir) => {
        const logPath = path.join(logDir, `proxy-${sessionId}.log`);
        return createLogger(`dap-proxy:${sessionId}`, {
            level: 'debug',
            file: logPath
        });
    };
    return {
        loggerFactory,
        fileSystem: {
            ensureDir: (path) => fs.ensureDir(path),
            pathExists: (path) => fs.pathExists(path)
        },
        processSpawner: {
            spawn
        },
        dapClientFactory: {
            create: (host, port) => new MinimalDapClient(host, port) // eslint-disable-line @typescript-eslint/no-explicit-any -- MinimalDapClient implements IDapClient but has type compatibility issues
        },
        messageSender: {
            send: (message) => {
                if (process.send) {
                    process.send(message);
                }
                else {
                    process.stdout.write(JSON.stringify(message) + '\n');
                }
            }
        }
    };
}
/**
 * Create a simple console logger for pre-initialization errors
 */
export function createConsoleLogger() {
    return {
        info: (...args) => console.error('[INFO]', ...args),
        error: (...args) => console.error('[ERROR]', ...args),
        debug: (...args) => console.error('[DEBUG]', ...args),
        warn: (...args) => console.error('[WARN]', ...args)
    };
}
/**
 * Setup global error handlers for the proxy process
 */
export function setupGlobalErrorHandlers(logger, messageSender, shutdownFn, getCurrentSessionId) {
    process.on('uncaughtException', (err, origin) => {
        logger.error(`[Proxy Worker UNCAUGHT_EXCEPTION] Origin: ${origin}`, err);
        messageSender.send({
            type: 'error',
            message: `Proxy worker uncaught exception: ${err.message} (origin: ${origin})`,
            sessionId: getCurrentSessionId() || 'unknown'
        });
        shutdownFn().finally(() => process.exit(1));
    });
    process.on('unhandledRejection', (reason, promise) => {
        logger.error('[Proxy Worker UNHANDLED_REJECTION] Reason:', reason, 'Promise:', promise);
        messageSender.send({
            type: 'error',
            message: `Proxy worker unhandled rejection: ${String(reason)}`,
            sessionId: getCurrentSessionId() || 'unknown'
        });
        shutdownFn().finally(() => process.exit(1));
    });
    process.on('SIGINT', async () => {
        logger.info('[Proxy] SIGINT received, shutting down proxy worker.');
        await shutdownFn();
        process.exit(0);
    });
    process.on('SIGTERM', async () => {
        logger.info('[Proxy] SIGTERM received, shutting down proxy worker.');
        await shutdownFn();
        process.exit(0);
    });
}
//# sourceMappingURL=dap-proxy-dependencies.js.map