'use strict';

var fs = require('fs');
var os = require('os');
var path = require('path');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var fs__default = /*#__PURE__*/_interopDefault(fs);
var os__default = /*#__PURE__*/_interopDefault(os);
var path__default = /*#__PURE__*/_interopDefault(path);

// src/platform/ai-services/internal/output-capture.ts
function prepareCodexLastMessageCapture(args) {
  const tempDir = fs__default.default.mkdtempSync(path__default.default.join(os__default.default.tmpdir(), "ju-hong-e-codex-"));
  const capturePath = path__default.default.join(tempDir, "last-message.txt");
  return {
    args: [...args, "--output-last-message", capturePath],
    finalizeStdout: (currentStdout) => {
      try {
        if (!fs__default.default.existsSync(capturePath)) {
          return currentStdout;
        }
        return fs__default.default.readFileSync(capturePath, "utf8");
      } catch {
        return currentStdout;
      }
    },
    cleanup: () => {
      try {
        fs__default.default.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: () => {
    }
  };
}

exports.prepareAttemptOutputCapture = prepareAttemptOutputCapture;
