import { buildCommandOutputSpilloverContent, COMMAND_OUTPUT_SPILLOVER_LINE_LIMIT, countOutputLines, formatCommandOutputSpilloverStdout, toDisplayPath, truncateOutput, writeCommandArtifactFile, } from "./commandArtifacts.js";
async function writeCommandOutputSpilloverFile(workspaceRoot, content) {
    return writeCommandArtifactFile(workspaceRoot, "cmd", "out", content);
}
function buildShellCommandResult(input) {
    return {
        exitCode: input.exitCode,
        signal: input.signal,
        stdout: input.stdout,
        stderr: input.stderr,
        durationMs: input.endedAt.getTime() - input.startedAt.getTime(),
        timedOut: input.timedOut,
        inactivityTimedOut: input.inactivityTimedOut,
        outputSpilled: input.outputSpilled,
        outputLineCount: input.outputLineCount,
        ...(input.outputFilePath ? { outputFilePath: input.outputFilePath } : {}),
        ...(input.commandFilePath
            ? { commandFilePath: input.commandFilePath }
            : {}),
        commandLineCount: input.commandLineCount,
    };
}
export async function resolveClosedShellCommand(input) {
    const endedAt = new Date();
    const outputLineCount = countOutputLines(`${input.stdout}${input.stderr}`);
    if (outputLineCount > COMMAND_OUTPUT_SPILLOVER_LINE_LIMIT) {
        const outputFilePath = await writeCommandOutputSpilloverFile(input.workspaceRoot, buildCommandOutputSpilloverContent({
            commandText: input.prepared.originalCommandText,
            commandFilePath: input.prepared.commandFilePath,
            commandLineCount: input.prepared.commandLineCount,
            cwd: input.cwd,
            startedAt: input.startedAt,
            endedAt,
            exitCode: input.exitCode,
            signal: input.signal,
            timedOut: input.timedOut,
            inactivityTimedOut: input.inactivityTimedOut,
            stdout: input.stdout,
            stderr: input.stderr,
        }));
        return buildShellCommandResult({
            exitCode: input.exitCode,
            signal: input.signal,
            stdout: formatCommandOutputSpilloverStdout(outputFilePath, outputLineCount),
            stderr: "",
            startedAt: input.startedAt,
            endedAt,
            timedOut: input.timedOut,
            inactivityTimedOut: input.inactivityTimedOut,
            outputSpilled: true,
            outputFilePath: toDisplayPath(outputFilePath),
            outputLineCount,
            commandFilePath: input.prepared.commandFilePath,
            commandLineCount: input.prepared.commandLineCount,
        });
    }
    return buildShellCommandResult({
        exitCode: input.exitCode,
        signal: input.signal,
        stdout: truncateOutput(input.stdout).text,
        stderr: truncateOutput(input.stderr).text,
        startedAt: input.startedAt,
        endedAt,
        timedOut: input.timedOut,
        inactivityTimedOut: input.inactivityTimedOut,
        outputSpilled: false,
        outputLineCount,
        commandFilePath: input.prepared.commandFilePath,
        commandLineCount: input.prepared.commandLineCount,
    });
}
