"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getUserWhitelist = getUserWhitelist;
exports.saveTokenToWhitelist = saveTokenToWhitelist;
exports.getUserTokens = getUserTokens;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const os_1 = __importDefault(require("os"));
const WHITELIST_FILE_PATH = path_1.default.join(os_1.default.homedir(), '.nyxora', 'user_whitelist.json');
function getUserWhitelist() {
    try {
        if (!fs_1.default.existsSync(WHITELIST_FILE_PATH)) {
            return {};
        }
        const data = fs_1.default.readFileSync(WHITELIST_FILE_PATH, 'utf-8');
        return JSON.parse(data);
    }
    catch (err) {
        console.error('[Whitelist] Error reading user_whitelist.json', err);
        return {};
    }
}
function saveTokenToWhitelist(walletAddress, chainName, tokenAddress) {
    try {
        const whitelist = getUserWhitelist();
        const addr = walletAddress.toLowerCase();
        const tokenAddr = tokenAddress.toLowerCase();
        if (!whitelist[addr])
            whitelist[addr] = {};
        if (!whitelist[addr][chainName])
            whitelist[addr][chainName] = [];
        // Avoid duplicates
        if (!whitelist[addr][chainName].includes(tokenAddr)) {
            whitelist[addr][chainName].push(tokenAddr);
            fs_1.default.writeFileSync(WHITELIST_FILE_PATH, JSON.stringify(whitelist, null, 2), 'utf-8');
            console.log(`[Whitelist] Added ${tokenAddr} to ${chainName} for ${addr}`);
        }
    }
    catch (err) {
        console.error('[Whitelist] Error saving token to user_whitelist.json', err);
    }
}
function getUserTokens(walletAddress, chainName) {
    const whitelist = getUserWhitelist();
    const addr = walletAddress.toLowerCase();
    return whitelist[addr]?.[chainName] || [];
}
