import { __awaiter, __generator } from "tslib";
import { utils as ethersUtils } from "ethers";
import { mapAsync } from "../utils";
import { INVALID_ADDRESS } from "./types";
import * as adapters from "./adapters";
import * as adapters_1 from "./adapters";
export { adapters_1 as adapters };
var AddressAggregator = /** @class */ (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 __awaiter(this, void 0, void 0, function () {
            var allAddresses, addresses;
            var _this = this;
            return __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 /*yield*/, mapAsync(this.adapters, function (adapter) { return __awaiter(_this, void 0, void 0, function () {
                                var invalidAddresses, addresses;
                                var _a, _b;
                                return __generator(this, function (_c) {
                                    switch (_c.label) {
                                        case 0:
                                            invalidAddresses = [];
                                            return [4 /*yield*/, adapter.update()];
                                        case 1:
                                            addresses = (_c.sent())
                                                .map(function (address) {
                                                try {
                                                    return ethersUtils.getAddress(address.toLowerCase());
                                                }
                                                catch (_a) {
                                                    invalidAddresses.push(address);
                                                    return INVALID_ADDRESS;
                                                }
                                            })
                                                .filter(function (address) { return address !== 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 /*return*/, 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 /*return*/, addresses];
                }
            });
        });
    };
    return AddressAggregator;
}());
export { AddressAggregator };
function run() {
    return __awaiter(this, void 0, void 0, function () {
        var addressList, addresses;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    addressList = new AddressAggregator([new adapters.risklabs.AddressList({ throwOnError: true })]);
                    return [4 /*yield*/, addressList.update()];
                case 1:
                    addresses = _a.sent();
                    console.log("Retrieved ".concat(addresses.size, " addresses: ").concat(JSON.stringify(Array.from(addresses), null, 2)));
                    return [2 /*return*/, 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