// Installer log-file initialization with diagnostic stderr emission on
// failure. Extracted from index.ts so the failure path can be covered by
// node:test without bringing in the rest of the installer's import-time side
// effects.
//
// (maxy-code) — operator-observed silent exit between
// `mkdirSync(LOG_DIR)` and the first `appendFileSync(LOG_FILE, header)`.
// Both writes are guarded inside the same try/catch; a failure emits a
// literal `[create-maxy] init-logging FAILED reason=<msg> log=<path>
// persist=<path>` line to stderr and exits 1 so the operator always has a
// forensic surface regardless of whether the log file ever opened.
import { mkdirSync as realMkdirSync, appendFileSync as realAppendFileSync, } from "node:fs";
export function runInitLogging(opts) {
    const mkdir = opts.mkdirSyncImpl ?? ((p, o) => { realMkdirSync(p, o); });
    const append = opts.appendFileSyncImpl ?? ((p, d) => { realAppendFileSync(p, d); });
    const stderr = opts.stderrWriteImpl ?? ((c) => { process.stderr.write(c); });
    const exit = opts.exitImpl ?? ((c) => { process.exit(c); });
    try {
        mkdir(opts.logDir, { recursive: true });
        append(opts.logFile, opts.headerLines.join("\n") + "\n");
    }
    catch (err) {
        const msg = err instanceof Error ? err.message : String(err);
        stderr(`[create-maxy] init-logging FAILED reason=${msg} log=${opts.logFile} persist=${opts.persistDir}\n`);
        exit(1);
        return;
    }
}
