function fileNameForRecord(record) {
    return `${record.completedAt.slice(0, 10)}.jsonl`;
}
function fileNameToDate(filename) {
    const datePart = filename.replace(/\.jsonl$/, '');
    const parsed = new Date(`${datePart}T00:00:00.000Z`);
    return Number.isNaN(parsed.getTime()) ? null : parsed;
}
function thresholdFromRetention(retentionDays, now) {
    const ms = retentionDays * 24 * 60 * 60 * 1000;
    return new Date(now.getTime() - ms);
}
export class StubJobHistoryGateway {
    buckets = new Map();
    failOnAppend = false;
    appendCount = 0;
    lastAppended = null;
    async appendRecord(record) {
        this.appendCount += 1;
        if (this.failOnAppend) {
            throw new Error('stub append failure');
        }
        this.lastAppended = record;
        const filename = fileNameForRecord(record);
        const list = this.buckets.get(filename) ?? [];
        list.push(record);
        this.buckets.set(filename, list);
    }
    async loadRecordsWithinWindow(retentionDays, now) {
        const threshold = thresholdFromRetention(retentionDays, now);
        const collected = [];
        for (const [filename, records] of this.buckets.entries()) {
            const fileDate = fileNameToDate(filename);
            if (!fileDate)
                continue;
            if (fileDate < threshold)
                continue;
            collected.push(...records);
        }
        return collected;
    }
    async deleteRecordsOutsideWindow(retentionDays, now) {
        const threshold = thresholdFromRetention(retentionDays, now);
        const deletedFilenames = [];
        for (const filename of Array.from(this.buckets.keys())) {
            const fileDate = fileNameToDate(filename);
            if (!fileDate)
                continue;
            if (fileDate < threshold) {
                deletedFilenames.push(filename);
                this.buckets.delete(filename);
            }
        }
        return { deletedFilenames };
    }
    prepopulate(record) {
        const filename = fileNameForRecord(record);
        const list = this.buckets.get(filename) ?? [];
        list.push(record);
        this.buckets.set(filename, list);
    }
}
//# sourceMappingURL=jobHistory.stub.js.map