import { readJson, writeJson, globalDataDir } from './base-store.js';
import { withFileLock } from './file-mutex.js';
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
const MAX_EVENTS = 10_000;
// ---------------------------------------------------------------------------
// Paths
// ---------------------------------------------------------------------------
function usageFile() {
    return `${globalDataDir()}/usage.json`;
}
function firstRunFile() {
    return `${globalDataDir()}/first-run.json`;
}
// ---------------------------------------------------------------------------
// Usage log (data/global/usage.json)
// ---------------------------------------------------------------------------
export async function getUsageLog() {
    return readJson(usageFile(), { events: [] });
}
// Internal rotate — must be called only from within a withFileLock(usageFile()) context.
async function rotateUsageLogLocked() {
    const isoDate = new Date().toISOString().slice(0, 10);
    const archivePath = `${globalDataDir()}/usage-${isoDate}.json`;
    const currentData = await readJson(usageFile(), { events: [] });
    await writeJson(archivePath, currentData);
    await writeJson(usageFile(), { events: [] });
}
export async function appendUsageEvent(event) {
    await withFileLock(usageFile(), async () => {
        const log = await getUsageLog();
        const events = [...log.events, event];
        if (events.length > MAX_EVENTS) {
            await rotateUsageLogLocked();
            await writeJson(usageFile(), { events: [event] });
            return;
        }
        await writeJson(usageFile(), { events });
    });
}
export async function rotateUsageLog() {
    await withFileLock(usageFile(), rotateUsageLogLocked);
}
// ---------------------------------------------------------------------------
// Trial state (data/global/first-run.json)
// ---------------------------------------------------------------------------
export async function getTrialState() {
    return readJson(firstRunFile(), null);
}
export async function ensureTrialState() {
    const existing = await getTrialState();
    if (existing !== null) {
        return existing;
    }
    const state = { firstRunAt: new Date().toISOString() };
    await writeJson(firstRunFile(), state);
    return state;
}
// ---------------------------------------------------------------------------
// Computed helpers (derived from usage.json — no extra file)
// ---------------------------------------------------------------------------
export async function getDailyCallCount(date) {
    const log = await getUsageLog();
    return log.events.filter((event) => event.timestamp.startsWith(date)).length;
}
export async function getLastCallTimestamp() {
    const log = await getUsageLog();
    if (log.events.length === 0) {
        return null;
    }
    const last = log.events[log.events.length - 1];
    /* v8 ignore start -- always defined when length > 0 */
    return last?.timestamp ?? null;
    /* v8 ignore stop */
}
//# sourceMappingURL=usage-store.js.map