"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildObligationFilters = buildObligationFilters;
exports.listenToObligationChanges = listenToObligationChanges;
const accounts_1 = require("../@codegen/klend/accounts");
const programId_1 = require("../@codegen/klend/programId");
const anchorConstants_1 = require("./anchorConstants");
const createManagerSubscription_1 = require("./createManagerSubscription");
const obligation_1 = require("../classes/obligation");
const OBLIGATION_ACCOUNT_SIZE = BigInt(accounts_1.Obligation.layout.span + anchorConstants_1.ANCHOR_DISCRIMINATOR_BYTES);
function buildObligationFilters(owner) {
    return [
        { dataSize: OBLIGATION_ACCOUNT_SIZE },
        { memcmp: { offset: 64n, bytes: owner.toString(), encoding: 'base58' } },
    ];
}
function listenToObligationChanges(params) {
    const getMarkets = typeof params.markets === 'function' ? params.markets : () => params.markets;
    return (0, createManagerSubscription_1.createManagerSubscription)({
        manager: params.manager,
        programId: params.programId ?? programId_1.PROGRAM_ID,
        filters: buildObligationFilters(params.owner),
        // Obligations are low-frequency — use 'processed' for instant delivery.
        commitment: params.commitment ?? 'processed',
        throttleMs: params.throttleMs,
        scheduler: params.scheduler,
        decode: (address, buffer, slot) => {
            const obligation = obligation_1.KaminoObligation.fromAccountData(getMarkets(), address, buffer, slot);
            // `undefined` is honored by createManagerSubscription — no event fires, no error.
            if (!obligation)
                return undefined;
            return { obligation, address, slot };
        },
        onChange: params.onChange,
        onError: params.onError,
        errorPrefix: '[listenToObligationChanges]',
    });
}
//# sourceMappingURL=obligationListener.js.map