"use strict";
/**
 * Sanitized bug report for the error-outro recovery launchpad.
 *
 * Writes a short, shareable support report to `<tmpdir>/amplitude-bug-report.txt`.
 * Passes every string through the observability redactor so API keys,
 * tokens, and user-typed secrets are stripped before the user pastes the
 * report into a support channel.
 *
 * Intentionally small — the diagnostic dump (`crash-dump.ts`) carries the
 * full state. This is the quick-copy companion users actually paste.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.bugReportPath = bugReportPath;
exports.buildBugReportBody = buildBugReportBody;
exports.writeBugReport = writeBugReport;
const node_fs_1 = require("node:fs");
const node_os_1 = require("node:os");
const node_path_1 = require("node:path");
const index_js_1 = require("./observability/index.js");
const redact_js_1 = require("./observability/redact.js");
const constants_js_1 = require("./constants.js");
const debug_js_1 = require("../utils/debug.js");
/** Path the bug report is written to on success. */
function bugReportPath() {
    return (0, node_path_1.join)((0, node_os_1.tmpdir)(), 'amplitude-bug-report.txt');
}
/** Build the sanitized report body. Pure — no I/O. Exported for tests. */
function buildBugReportBody(inputs = {}) {
    const lines = [
        '# Amplitude Wizard — Support Report',
        '',
        `Captured: ${new Date().toISOString()}`,
        `Wizard version: ${constants_js_1.WIZARD_VERSION}`,
        `Node version: ${process.version}`,
        `Platform: ${process.platform} / ${process.arch}`,
        `Run ID: ${(0, index_js_1.getRunId)()}`,
        `Session ID: ${(0, index_js_1.getSessionId)()}`,
        `Integration: ${inputs.integration ?? 'unknown'}`,
        `Log file: ${(0, index_js_1.getLogFilePath)()}`,
        '',
        '## Error',
        '',
        inputs.errorMessage
            ? (0, redact_js_1.redactString)(inputs.errorMessage)
            : '(no error message provided)',
        '',
        '## What I was doing',
        '',
        '(please describe the steps you took before this error)',
        '',
        '## Additional context',
        '',
        '(attach the log file above if support asks for it)',
        '',
    ];
    return (0, redact_js_1.redactString)(lines.join('\n'));
}
/**
 * Write the sanitized bug report to a temp file.
 * Non-throwing — returns null on failure so the caller can surface a
 * friendly message instead of crashing the outro.
 */
function writeBugReport(inputs = {}) {
    const path = bugReportPath();
    try {
        (0, node_fs_1.writeFileSync)(path, buildBugReportBody(inputs), { mode: 0o600 });
        (0, debug_js_1.logToFile)(`bug-report: wrote ${path}`);
        return path;
    }
    catch (err) {
        (0, debug_js_1.logToFile)(`bug-report: write failed: ${err instanceof Error ? err.message : String(err)}`);
        return null;
    }
}
