"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.KaminoBorrowOrder = void 0;
const bn_js_1 = __importDefault(require("bn.js"));
const types_1 = require("../@codegen/klend/types");
/**
 * Wrapper class for working with borrow orders on obligations.
 * Borrow orders allow users to request fixed-term loans with specific parameters.
 */
class KaminoBorrowOrder {
    debtLiquidityMint;
    remainingDebtAmount;
    filledDebtDestination;
    minDebtTermSeconds;
    fillableUntilTimestamp;
    maxBorrowRateBps;
    enableAutoRolloverOnFilledBorrows;
    placedAtTimestamp;
    lastUpdatedAtTimestamp;
    requestedDebtAmount;
    constructor(debtLiquidityMint, remainingDebtAmount, filledDebtDestination, minDebtTermSeconds, fillableUntilTimestamp, maxBorrowRateBps, enableAutoRolloverOnFilledBorrows, placedAtTimestamp, lastUpdatedAtTimestamp, requestedDebtAmount) {
        this.debtLiquidityMint = debtLiquidityMint;
        this.remainingDebtAmount = remainingDebtAmount;
        this.filledDebtDestination = filledDebtDestination;
        this.minDebtTermSeconds = minDebtTermSeconds;
        this.fillableUntilTimestamp = fillableUntilTimestamp;
        this.maxBorrowRateBps = maxBorrowRateBps;
        this.enableAutoRolloverOnFilledBorrows = enableAutoRolloverOnFilledBorrows;
        this.placedAtTimestamp = placedAtTimestamp;
        this.lastUpdatedAtTimestamp = lastUpdatedAtTimestamp;
        this.requestedDebtAmount = requestedDebtAmount;
    }
    /**
     * Creates a borrow order config args for setting on an obligation
     */
    toConfigArgs() {
        return new types_1.BorrowOrderConfigArgs({
            remainingDebtAmount: this.remainingDebtAmount,
            maxBorrowRateBps: this.maxBorrowRateBps,
            minDebtTermSeconds: this.minDebtTermSeconds,
            fillableUntilTimestamp: this.fillableUntilTimestamp,
            enableAutoRolloverOnFilledBorrows: this.enableAutoRolloverOnFilledBorrows ?? false,
        });
    }
    /**
     * Creates a KaminoBorrowOrder from existing borrow order state
     */
    static fromExistingBorrowOrderState(borrowOrder) {
        return new KaminoBorrowOrder(borrowOrder.debtLiquidityMint, borrowOrder.remainingDebtAmount, borrowOrder.filledDebtDestination, borrowOrder.minDebtTermSeconds, borrowOrder.fillableUntilTimestamp, borrowOrder.maxBorrowRateBps, borrowOrder.enableAutoRolloverOnFilledBorrows !== 0, borrowOrder.placedAtTimestamp, borrowOrder.lastUpdatedAtTimestamp, borrowOrder.requestedDebtAmount);
    }
    /**
     * Creates a fixed-term borrow order with common parameters
     */
    static createFixedTermBorrowOrder(params) {
        const now = Math.floor(Date.now() / 1000);
        const expiryTimestamp = new bn_js_1.default(now).add(params.expirySeconds);
        return new KaminoBorrowOrder(params.debtMint, params.amount, params.destination, params.termSeconds, expiryTimestamp, params.maxRateBps, params.enableAutoRolloverOnFilledBorrows);
    }
    /**
     * Creates an open-term borrow order
     */
    static createOpenTermBorrowOrder(params) {
        const now = Math.floor(Date.now() / 1000);
        const expiryTimestamp = new bn_js_1.default(now).add(params.expirySeconds);
        return new KaminoBorrowOrder(params.debtMint, params.amount, params.destination, new bn_js_1.default(0), expiryTimestamp, 0xffffffff, false);
    }
    /**
     * Creates a cancel order from an existing borrow order.
     * This removes the existing order
     */
    static createCancelBorrowOrderFromExistingState(existing) {
        return new KaminoBorrowOrder(existing.debtLiquidityMint, new bn_js_1.default(0), existing.filledDebtDestination, new bn_js_1.default(0), new bn_js_1.default(0), 0);
    }
    /**
     * Check if the borrow order is active (has remaining debt to be filled)
     */
    isActive() {
        return this.remainingDebtAmount.gtn(0);
    }
    /**
     * Check if the borrow order has expired
     */
    isExpired(currentTimestamp) {
        return currentTimestamp.gt(this.fillableUntilTimestamp);
    }
}
exports.KaminoBorrowOrder = KaminoBorrowOrder;
//# sourceMappingURL=borrowOrder.js.map