"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchScaledUiAmountMultipliers = fetchScaledUiAmountMultipliers;
const kit_1 = require("@solana/kit");
const token_2022_1 = require("@solana-program/token-2022");
const decimal_js_1 = __importDefault(require("decimal.js"));
/**
 * Extracts the effective scaledUiAmount multiplier from a mint's extensions.
 * Returns 1 if the mint does not have the ScaledUiAmountConfig extension.
 *
 * The multiplier accounts for scheduled updates: if `newMultiplierEffectiveTimestamp`
 * has passed, `newMultiplier` is used instead of `multiplier`.
 */
function getScaledUiAmountMultiplierFromExtensions(extensions) {
    for (const ext of extensions) {
        if (ext.__kind === 'ScaledUiAmountConfig') {
            const now = Math.floor(Date.now() / 1000);
            const effectiveTimestamp = Number(ext.newMultiplierEffectiveTimestamp);
            if (effectiveTimestamp > 0 && now >= effectiveTimestamp) {
                return new decimal_js_1.default(ext.newMultiplier);
            }
            return new decimal_js_1.default(ext.multiplier);
        }
    }
    return new decimal_js_1.default(1);
}
/**
 * Batch-fetches mint accounts for the given addresses and returns a map of
 * mint address → scaledUiAmount multiplier. Only Token-2022 mints with
 * the ScaledUiAmountConfig extension will have multiplier !== 1.
 *
 * Mints that fail to fetch are silently skipped (multiplier defaults to 1).
 */
async function fetchScaledUiAmountMultipliers(rpc, mintAddresses) {
    const result = new Map();
    if (mintAddresses.length === 0) {
        return result;
    }
    const mintAccounts = await (0, token_2022_1.fetchAllMaybeMint)(rpc, mintAddresses);
    for (let i = 0; i < mintAccounts.length; i++) {
        const account = mintAccounts[i];
        if (!account.exists) {
            continue;
        }
        const extensions = account.data.extensions;
        if ((0, kit_1.isSome)(extensions)) {
            const multiplier = getScaledUiAmountMultiplierFromExtensions(extensions.value);
            if (!multiplier.eq(1)) {
                result.set(mintAddresses[i], multiplier);
            }
        }
    }
    return result;
}
//# sourceMappingURL=scaledUiAmount.js.map