"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.inferCurrencyCode = inferCurrencyCode;
exports.resolveOmnlLineM2 = resolveOmnlLineM2;
exports.withM2Capabilities = withM2Capabilities;
exports.mergeExchangeTokensWithM2Registry = mergeExchangeTokensWithM2Registry;
exports.findM2TokenByAddress = findM2TokenByAddress;
exports.findM2TokenBySymbol = findM2TokenBySymbol;
const money_supply_1 = require("./money-supply");
const CURRENCY_FROM_SYMBOL = {
    USD: 'USD',
    EUR: 'EUR',
    GBP: 'GBP',
    AUD: 'AUD',
    JPY: 'JPY',
    CHF: 'CHF',
    CAD: 'CAD',
    XAU: 'XAU',
    BTC: 'BTC',
    ETH: 'ETH',
    BNB: 'BNB',
    POL: 'POL',
    AVAX: 'AVAX',
    CRO: 'CRO',
    XDAI: 'XDAI',
    CELO: 'CELO',
    WEMIX: 'WEMIX',
};
function inferCurrencyCode(symbol) {
    const s = symbol.toUpperCase();
    for (const [key, code] of Object.entries(CURRENCY_FROM_SYMBOL)) {
        if (s.includes(key))
            return code;
    }
    if (s.startsWith('C') && s.length > 1) {
        for (const [key, code] of Object.entries(CURRENCY_FROM_SYMBOL)) {
            if (s.slice(1).includes(key))
                return code;
        }
    }
    return 'USD';
}
function resolveOmnlLineM2(currencyCode, symbol) {
    const ccy = (currencyCode || inferCurrencyCode(symbol ?? 'USD')).toUpperCase();
    return `${ccy}-M2`;
}
function withM2Capabilities(token, overrides = {}) {
    const currencyCode = token.currencyCode ?? inferCurrencyCode(token.symbol);
    return {
        symbol: token.symbol,
        name: token.name,
        address: token.address,
        decimals: token.decimals,
        native: token.native,
        currencyCode,
        moneyLayer: 'M2',
        omnlLine: token.omnlLine ?? resolveOmnlLineM2(currencyCode, token.symbol),
        loadFromGl: money_supply_1.GL_CODES.M2_BROAD,
        swappable: true,
        convertible: true,
        transferableInternal: true,
        transferableExternal: true,
        ...overrides,
    };
}
function mergeExchangeTokensWithM2Registry(baseTokens, registry) {
    const byAddress = new Map();
    for (const t of registry.tokens) {
        byAddress.set(t.address.toLowerCase(), t);
    }
    for (const t of baseTokens) {
        const key = t.address.toLowerCase();
        if (!byAddress.has(key)) {
            byAddress.set(key, withM2Capabilities(t));
        }
    }
    return Array.from(byAddress.values()).sort((a, b) => a.symbol.localeCompare(b.symbol));
}
function findM2TokenByAddress(registry, address) {
    const lower = address.toLowerCase();
    return registry.tokens.find((t) => t.address.toLowerCase() === lower);
}
function findM2TokenBySymbol(registry, symbol) {
    const s = symbol.trim().toLowerCase();
    return registry.tokens.find((t) => t.symbol.toLowerCase() === s);
}
