"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebhookStorage = exports.WebhookModel = void 0;
const base_1 = require("./base");
class WebhookModel extends base_1.BaseModel {
    constructor(storage) {
        super('webhook', storage);
        this.allowedPaging = [];
    }
    onConnect() {
        // capped at 100 MiB
        this.db?.createCollection(this.collectionName, { capped: true, size: (2 ** 20) * 100 })
            .catch((err) => { if (err.codeName !== 'NamespaceExists' && err.code !== 48)
            throw err; });
        this.collection.createIndex({ chain: 1, network: 1, source: 1 }, { background: true });
    }
    getTail(params) {
        const { chain, network } = params;
        const MINUTE = 1000 * 60;
        const HOUR = MINUTE * 60;
        const DAY = HOUR * 24;
        return this.collection.find({ chain, network, processed: false })
            .addCursorFlag('tailable', true)
            .addCursorFlag('awaitData', true)
            .maxAwaitTimeMS(DAY * 24 + HOUR * 20 + MINUTE * 31)
            .stream();
    }
    setProcessed(params) {
        const { webhook, webhookId } = params;
        const id = webhook?._id || webhookId;
        if (!id) {
            throw new Error('No webhook id given to clear');
        }
        return this.collection.updateOne({ _id: id }, { $set: { processed: true } });
    }
}
exports.WebhookModel = WebhookModel;
exports.WebhookStorage = new WebhookModel();
//# sourceMappingURL=webhook.js.map