import { mapOutboxEvent } from "../internal/rowMappers.js";
export class OutboxRepository {
    #db;
    constructor(db) {
        this.#db = db;
    }
    appendOutboxEvent(input) {
        const now = new Date().toISOString();
        const payloadJson = JSON.stringify(input.payload);
        this.#db
            .prepare(`INSERT INTO outbox_events
           (id, type, aggregate_id, payload_json, status, attempts, created_at, updated_at)
         VALUES (?, ?, ?, ?, 'pending', 0, ?, ?)`)
            .run(input.id, input.type, input.aggregateId, payloadJson, now, now);
        return this.getOutboxEvent(input.id);
    }
    listPendingOutboxEvents(limit = 100) {
        return this.#db
            .prepare(`SELECT id, type, aggregate_id, payload_json, status, attempts, created_at, updated_at
         FROM outbox_events
         WHERE status = 'pending'
         ORDER BY created_at ASC
         LIMIT ?`)
            .all(limit)
            .map(mapOutboxEvent);
    }
    getOutboxEvent(id) {
        const event = this.#db
            .prepare(`SELECT id, type, aggregate_id, payload_json, status, attempts, created_at, updated_at
         FROM outbox_events
         WHERE id = ?`)
            .get(id);
        return mapOutboxEvent(event);
    }
}
