"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.execService = void 0;
const spawn_1 = require("../utils/spawn");
exports.execService = {
    exec(containerName, command, options) {
        const args = ['exec'];
        if (options?.tty) {
            args.push('-t');
        }
        if (options?.user) {
            args.push('-u', options.user);
        }
        if (options?.workingDir) {
            args.push('-w', options.workingDir);
        }
        if (options?.env) {
            for (const [key, value] of Object.entries(options.env)) {
                args.push('-e', `${key}=${value}`);
            }
        }
        args.push(containerName, ...command);
        const result = (0, spawn_1.spawnCommand)('docker', args, {
            encoding: 'utf8',
            timeout: 120000
        });
        if (result.error) {
            throw result.error;
        }
        if (result.status === null) {
            throw new Error('docker command not found. Is Docker installed?');
        }
        if (result.status !== 0) {
            throw new Error(`docker exec failed for container '${containerName}': ${result.stderr?.trim() || 'unknown error'}`);
        }
        return result.stdout ?? '';
    }
};
//# sourceMappingURL=docker-exec.js.map