import { execAsync } from '../../../utils.js';
import { createModuleLogger } from '../../../logger.js';
const logger = createModuleLogger('SwiftPackageRunner');
/**
 * Handles running Swift package executables (Single Responsibility: Running)
 */
export class SwiftPackageRunner {
    /**
     * Run a Swift package executable
     */
    async run(packagePath, options = {}) {
        const startTime = Date.now();
        const { executable, arguments: args = [], configuration = 'Debug' } = options;
        // Convert to lowercase for swift command
        const configFlag = configuration.toLowerCase();
        let command = `swift run --package-path "${packagePath}" -c ${configFlag}`;
        if (executable) {
            command += ` "${executable}"`;
        }
        if (args.length > 0) {
            command += ` ${args.map(arg => `"${arg}"`).join(' ')}`;
        }
        logger.debug({ command }, 'Run command');
        try {
            const { stdout, stderr } = await execAsync(command, {
                maxBuffer: 10 * 1024 * 1024
            });
            const output = stdout + (stderr ? `\n${stderr}` : '');
            const duration = Date.now() - startTime;
            logger.info({ packagePath, executable, duration }, 'Run succeeded');
            return {
                success: true,
                output,
                exitCode: 0,
                duration
            };
        }
        catch (error) {
            const duration = Date.now() - startTime;
            logger.error({ error: error.message, packagePath }, 'Run failed');
            const output = (error.stdout || '') + (error.stderr ? `\n${error.stderr}` : '');
            return {
                success: false,
                output,
                exitCode: error.code || 1,
                error: error.message,
                duration
            };
        }
    }
}
//# sourceMappingURL=SwiftPackageRunner.js.map