"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createFileSink = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
function createFileSink(options) {
    const now = options.now ?? (() => new Date());
    let warned = false;
    const onError = options.onError ??
        ((msg) => {
            if (!warned) {
                warned = true;
                process.stderr.write(`[oauth-trace] file sink disabled: ${msg}\n`);
            }
        });
    let cachedDate = null;
    function ensureDir() {
        fs.mkdirSync(options.dir, { recursive: true, mode: 0o700 });
    }
    function dateStr(d) {
        const yyyy = d.getFullYear().toString().padStart(4, '0');
        const mm = (d.getMonth() + 1).toString().padStart(2, '0');
        const dd = d.getDate().toString().padStart(2, '0');
        return `${yyyy}${mm}${dd}`;
    }
    function pathForToday() {
        const ds = dateStr(now());
        cachedDate = ds;
        return path.join(options.dir, `oauth-${ds}.log`);
    }
    function appendOne(event) {
        const file = pathForToday();
        const line = JSON.stringify(event) + '\n';
        let fd = null;
        try {
            ensureDir();
            fd = fs.openSync(file, 'a', 0o600);
            fs.writeSync(fd, line);
        }
        finally {
            if (fd !== null) {
                try {
                    fs.closeSync(fd);
                }
                catch {
                    // ignore
                }
            }
        }
    }
    return {
        write(event) {
            try {
                appendOne(event);
            }
            catch (err) {
                onError(err.message);
            }
        },
        async flush() {
            // appendSync paths are flushed per-write; no buffer to drain.
            void cachedDate;
        },
    };
}
exports.createFileSink = createFileSink;
//# sourceMappingURL=sink-file.js.map