import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';

// src/platform/ai-services/internal/output-capture.ts
function prepareCodexLastMessageCapture(args) {
  const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "ju-hong-e-codex-"));
  const capturePath = path.join(tempDir, "last-message.txt");
  return {
    args: [...args, "--output-last-message", capturePath],
    finalizeStdout: (currentStdout) => {
      try {
        if (!fs.existsSync(capturePath)) {
          return currentStdout;
        }
        return fs.readFileSync(capturePath, "utf8");
      } catch {
        return currentStdout;
      }
    },
    cleanup: () => {
      try {
        fs.rmSync(tempDir, { force: true, recursive: true });
      } catch {
      }
    }
  };
}
function prepareAttemptOutputCapture(attempt) {
  if (attempt.stdoutCaptureMode === "codex-last-message-file") {
    return prepareCodexLastMessageCapture(attempt.args);
  }
  return {
    args: attempt.args,
    finalizeStdout: (currentStdout) => currentStdout,
    cleanup: () => {
    }
  };
}

export { prepareAttemptOutputCapture };
