// src/lib/reporting/runWithEnvelope.ts
import { performance as performance2 } from "node:perf_hooks";

// src/lib/reporting/xyReporter.ts
import { performance } from "node:perf_hooks";
var jsonMode = false;
var strictMode = false;
var originalStdoutWrite;
var envelopeEmitted = false;
var currentCommand = "unknown";
var startTime = 0;
function isJsonMode() {
  return jsonMode;
}
function setJsonMode(enabled) {
  if (jsonMode === enabled) return;
  jsonMode = enabled;
  if (enabled && !originalStdoutWrite) {
    originalStdoutWrite = process.stdout.write.bind(process.stdout);
    const stderrWrite = process.stderr.write.bind(process.stderr);
    process.stdout.write = stderrWrite;
    process.on("exit", emitFallbackEnvelope);
  }
}
function isStrictMode() {
  return strictMode || process.env.XY_STRICT === "1";
}
function setStrictMode(enabled) {
  strictMode = enabled;
  if (enabled) {
    process.env.XY_STRICT = "1";
  }
}
function setCurrentCommand(name) {
  currentCommand = name;
  startTime = performance.now();
  envelopeEmitted = false;
}
function emitJson(envelope) {
  if (envelopeEmitted) return;
  envelopeEmitted = true;
  const json = JSON.stringify(envelope) + "\n";
  if (originalStdoutWrite) {
    originalStdoutWrite(json);
  } else {
    process.stdout.write(json);
  }
}
function emitFallbackEnvelope() {
  if (envelopeEmitted) return;
  const raw = process.exitCode ?? 0;
  const code = typeof raw === "number" ? raw : Number(raw) || 0;
  emitJson({
    command: currentCommand,
    durationMs: Math.round(performance.now() - startTime),
    errorCount: code === 0 ? 0 : 1,
    exitCode: code,
    schemaVersion: 1,
    success: code === 0,
    warningCount: 0
  });
}
function xyLog(...args) {
  if (jsonMode) {
    process.stderr.write(args.map((arg) => typeof arg === "string" ? arg : String(arg)).join(" ") + "\n");
  } else {
    console.log(...args);
  }
}
function resetReporterForTesting() {
  if (originalStdoutWrite) {
    process.stdout.write = originalStdoutWrite;
    originalStdoutWrite = void 0;
  }
  jsonMode = false;
  envelopeEmitted = false;
  currentCommand = "unknown";
  startTime = 0;
}

// src/lib/reporting/runWithEnvelope.ts
async function runWithEnvelope(command, action) {
  setCurrentCommand(command);
  const start = performance2.now();
  let exitCode;
  let envelope;
  try {
    const ret = await action();
    if (typeof ret === "number") {
      exitCode = ret;
    } else {
      exitCode = ret.exitCode;
      envelope = ret.envelope;
    }
  } catch (ex) {
    exitCode = 1;
    const message = ex instanceof Error ? ex.message : String(ex);
    envelope = {
      errorCount: 1,
      errors: [{ message }]
    };
    if (!isJsonMode()) {
      console.error(message);
    }
  }
  if (isJsonMode()) {
    emitJson({
      command,
      data: envelope?.data,
      durationMs: Math.round(performance2.now() - start),
      errorCount: envelope?.errorCount ?? (exitCode === 0 ? 0 : 1),
      errors: envelope?.errors,
      exitCode,
      schemaVersion: 1,
      success: exitCode === 0,
      warningCount: envelope?.warningCount ?? 0,
      warnings: envelope?.warnings
    });
  }
  return exitCode;
}
async function withEnvelope(command, action) {
  process.exitCode = await runWithEnvelope(command, action);
}
export {
  emitJson,
  isJsonMode,
  isStrictMode,
  resetReporterForTesting,
  runWithEnvelope,
  setCurrentCommand,
  setJsonMode,
  setStrictMode,
  withEnvelope,
  xyLog
};
//# sourceMappingURL=index.mjs.map
