/**
 * Telegram bot offset persistence.
 * Tracks the last processed update_id to prevent stale command replay on restart.
 */
import { readFileSync, existsSync } from "fs";
import { join } from "path";
import { KASTELL_DIR } from "../../utils/paths.js";
import { secureWriteFileSync, secureMkdirSync } from "../../utils/secureWrite.js";
const OFFSET_FILE = join(KASTELL_DIR, "telegram-bot-offset.json");
const STALE_THRESHOLD_MS = 24 * 60 * 60 * 1000;
/** Ensure KASTELL_DIR exists. Call once at startup, not per-save. */
export function ensureOffsetDir() {
    if (!existsSync(KASTELL_DIR))
        secureMkdirSync(KASTELL_DIR);
}
/** Load the last saved offset. Returns null if file is missing or corrupt. */
export function loadOffset() {
    try {
        return JSON.parse(readFileSync(OFFSET_FILE, "utf-8"));
    }
    catch {
        return null;
    }
}
/** Persist the last processed update_id with current timestamp. */
export function saveOffset(updateId) {
    const data = {
        lastUpdateId: updateId,
        savedAt: new Date().toISOString(),
    };
    secureWriteFileSync(OFFSET_FILE, JSON.stringify(data, null, 2));
}
/** Check if the given savedAt timestamp is older than 24 hours. */
export function isStale(savedAt) {
    return Date.now() - new Date(savedAt).getTime() > STALE_THRESHOLD_MS;
}
//# sourceMappingURL=offset.js.map