"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TokenToAddress = void 0;
const fs_1 = __importDefault(require("fs"));
const logger_1 = require("../logger/logger");
const staticPath_1 = require("../utils/staticPath");
class TokenToAddress {
    constructor() {
        this.chainData = this.loadChainData();
        this.tokenMapping = this.loadTokenMapping();
    }
    loadChainData() {
        const filePath = (0, staticPath_1.resolveStaticFilePath)('chain_IDs.json');
        return JSON.parse(fs_1.default.readFileSync(filePath, 'utf-8'));
    }
    loadTokenMapping() {
        const filePath = (0, staticPath_1.resolveStaticFilePath)('chain_symbol_to_token_address_mapping.json');
        try {
            return JSON.parse(fs_1.default.readFileSync(filePath, 'utf-8'));
        }
        catch (error) {
            throw new Error('chain_symbol_to_token_address_mapping.json not found.');
        }
    }
    verifyChainId(chainId) {
        if (!Object.values(this.chainData).includes(chainId)) {
            logger_1.logger.error(`Chain ID ${chainId} not found in chain data.`);
            return false;
        }
        return true;
    }
    tokenToAddress(symbol, chainId) {
        const tokens = this.tokenMapping[chainId.toString()] || {};
        if (tokens[symbol]) {
            return tokens[symbol].address;
        }
        const lowerSymbol = symbol.toLowerCase();
        const idx = Object.keys(tokens).find(k => k.toLowerCase() === lowerSymbol);
        if (idx) {
            return tokens[idx].address;
        }
        const wrappedSymbol = `w${lowerSymbol}`;
        const wrappedIdx = Object.keys(tokens).find(k => k.toLowerCase() === wrappedSymbol);
        if (wrappedIdx) {
            return `Symbol ${symbol} not found, but W${symbol} exists for chain ID ${chainId} with address ${tokens[wrappedIdx].address}.`;
        }
        return `Symbol ${symbol} not found for chain ID ${chainId}.`;
    }
    tokenToDecimals(symbol, chainId) {
        const tokens = this.tokenMapping[chainId.toString()] || {};
        if (tokens[symbol]) {
            return tokens[symbol].decimals;
        }
        const lowerSymbol = symbol.toLowerCase();
        const idx = Object.keys(tokens).find(k => k.toLowerCase() === lowerSymbol);
        if (idx) {
            return tokens[idx].decimals;
        }
        return `Symbol ${symbol} not found for chain ID ${chainId}.`;
    }
    addressToToken(address, chainId) {
        const tokens = this.tokenMapping[chainId.toString()] || {};
        for (const [key, value] of Object.entries(tokens)) {
            if (value.address === address) {
                return key;
            }
        }
        return `Address ${address} not found for chain ID ${chainId}.`;
    }
    addressToDecimals(address, chainId) {
        const tokens = this.tokenMapping[chainId.toString()] || {};
        const normalizedAddress = address.toLowerCase();
        for (const value of Object.values(tokens)) {
            if (value.address.toLowerCase() === normalizedAddress) {
                return value.decimals;
            }
        }
        return `Address ${address} not found for chain ID ${chainId}.`;
    }
    tokenSymbolsToTokenAddresses(symbols, chainId) {
        return symbols.split(',').reduce((acc, symbol) => {
            acc[symbol] = this.tokenToAddress(symbol, chainId);
            return acc;
        }, {});
    }
}
exports.TokenToAddress = TokenToAddress;
