"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AddressAggregator = exports.adapters = void 0;
var tslib_1 = require("tslib");
var ethers_1 = require("ethers");
var utils_1 = require("../utils");
var types_1 = require("./types");
var adapters = tslib_1.__importStar(require("./adapters"));
exports.adapters = tslib_1.__importStar(require("./adapters"));
var AddressAggregator = (function () {
    function AddressAggregator(adapters, logger) {
        this.adapters = adapters;
        this.logger = logger;
    }
    AddressAggregator.sources = function () {
        return Object.keys(adapters);
    };
    AddressAggregator.prototype.update = function () {
        var _a, _b;
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var allAddresses, addresses;
            var _this = this;
            return tslib_1.__generator(this, function (_c) {
                switch (_c.label) {
                    case 0:
                        (_a = this.logger) === null || _a === void 0 ? void 0 : _a.debug({
                            at: "AddressAggregator::update",
                            message: "Updating addresses.",
                            sources: this.adapters.map(function (adapter) { return adapter.name; }),
                            supportedSources: AddressAggregator.sources(),
                        });
                        return [4, (0, utils_1.mapAsync)(this.adapters, function (adapter) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
                                var invalidAddresses, addresses;
                                var _a, _b;
                                return tslib_1.__generator(this, function (_c) {
                                    switch (_c.label) {
                                        case 0:
                                            invalidAddresses = [];
                                            return [4, adapter.update()];
                                        case 1:
                                            addresses = (_c.sent())
                                                .map(function (address) {
                                                try {
                                                    return ethers_1.utils.getAddress(address.toLowerCase());
                                                }
                                                catch (_a) {
                                                    invalidAddresses.push(address);
                                                    return types_1.INVALID_ADDRESS;
                                                }
                                            })
                                                .filter(function (address) { return address !== types_1.INVALID_ADDRESS; });
                                            if (invalidAddresses.length > 0) {
                                                (_a = this.logger) === null || _a === void 0 ? void 0 : _a.warn({
                                                    at: "AddressAggregator::update()",
                                                    message: "Read ".concat(invalidAddresses.length, " malformed addresses on ").concat(adapter.name, "."),
                                                    invalidAddresses: invalidAddresses,
                                                });
                                            }
                                            (_b = this.logger) === null || _b === void 0 ? void 0 : _b.debug({
                                                at: "AddressAggregator::update",
                                                message: "Loaded ".concat(addresses.length, " addresses from ").concat(adapter.name, "."),
                                            });
                                            return [2, addresses];
                                    }
                                });
                            }); })];
                    case 1:
                        allAddresses = _c.sent();
                        addresses = new Set(allAddresses.flat());
                        (_b = this.logger) === null || _b === void 0 ? void 0 : _b.debug({
                            at: "AddressAggregator::update",
                            message: "Loaded ".concat(addresses.size, " addresses."),
                            sources: this.adapters.map(function (adapter) { return adapter.name; }),
                        });
                        return [2, addresses];
                }
            });
        });
    };
    return AddressAggregator;
}());
exports.AddressAggregator = AddressAggregator;
function run() {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var addressList, addresses;
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    addressList = new AddressAggregator([new adapters.risklabs.AddressList({ throwOnError: true })]);
                    return [4, addressList.update()];
                case 1:
                    addresses = _a.sent();
                    console.log("Retrieved ".concat(addresses.size, " addresses: ").concat(JSON.stringify(Array.from(addresses), null, 2)));
                    return [2, 0];
            }
        });
    });
}
if (require.main === module) {
    run()
        .then(function (result) {
        process.exitCode = result;
    })
        .catch(function (error) {
        console.error("Process exited with", error);
        process.exitCode = 127;
    });
}
//# sourceMappingURL=index.js.map