"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MockConfigStoreClient = void 0;
var tslib_1 = require("tslib");
var assert_1 = tslib_1.__importDefault(require("assert"));
var ethers_1 = require("ethers");
var utils_1 = require("../../utils");
var AcrossConfigStoreClient_1 = require("../AcrossConfigStoreClient");
var MockEvents_1 = require("./MockEvents");
var MockConfigStoreClient = (function (_super) {
    tslib_1.__extends(MockConfigStoreClient, _super);
    function MockConfigStoreClient(logger, configStore, eventSearchConfig, configStoreVersion, chainId, mockUpdate, availableChainIdsOverride) {
        if (eventSearchConfig === void 0) { eventSearchConfig = { from: 0, maxLookBack: 0 }; }
        if (chainId === void 0) { chainId = 1; }
        if (mockUpdate === void 0) { mockUpdate = false; }
        var _this = _super.call(this, logger, configStore, eventSearchConfig, configStoreVersion) || this;
        _this.configStoreVersion = AcrossConfigStoreClient_1.DEFAULT_CONFIG_STORE_VERSION;
        _this.eventSignatures = {
            OwnershipTransferred: "address,address",
            UpdatedGlobalConfig: "bytes32,string",
            UpdatedTokenConfig: "address,string",
        };
        _this.chainId = chainId;
        _this.eventManager = mockUpdate ? (0, MockEvents_1.getEventManager)(chainId, _this.eventSignatures) : null;
        if ((0, utils_1.isDefined)(_this.eventManager) && _this.eventManager) {
            _this.updateGlobalConfig(AcrossConfigStoreClient_1.GLOBAL_CONFIG_STORE_KEYS.CHAIN_ID_INDICES, JSON.stringify(availableChainIdsOverride), {
                blockNumber: _this.eventManager.blockNumber,
            });
        }
        return _this;
    }
    MockConfigStoreClient.prototype.setAvailableChains = function (chainIds) {
        this.availableChainIdsOverride = chainIds;
    };
    MockConfigStoreClient.prototype.getChainIdIndicesForBlock = function (block) {
        var _a;
        return (_a = this.availableChainIdsOverride) !== null && _a !== void 0 ? _a : _super.prototype.getChainIdIndicesForBlock.call(this, block);
    };
    MockConfigStoreClient.prototype.getConfigStoreVersionForBlock = function (_blockNumber) {
        return this.configStoreVersion === AcrossConfigStoreClient_1.DEFAULT_CONFIG_STORE_VERSION
            ? _super.prototype.getConfigStoreVersionForBlock.call(this, _blockNumber)
            : this.configStoreVersion;
    };
    MockConfigStoreClient.prototype.setConfigStoreVersion = function (version) {
        this.configStoreVersion = version;
    };
    MockConfigStoreClient.prototype._update = function () {
        if (this.eventManager === null) {
            return _super.prototype._update.call(this);
        }
        var eventNames = ["UpdatedGlobalConfig", "UpdatedTokenConfig"];
        var latestBlockSearched = this.eventManager.blockNumber;
        var globalConfigUpdateTimes = [];
        var _events = eventNames.map(function () { return []; });
        for (var _i = 0, _a = this.eventManager.getEvents().flat(); _i < _a.length; _i++) {
            var event_1 = _a[_i];
            var idx = eventNames.indexOf(event_1.event);
            if (idx !== -1) {
                _events[idx].push(event_1);
            }
            if (event_1.event === "UpdatedGlobalConfig") {
                globalConfigUpdateTimes.push((0, utils_1.getCurrentTime)());
            }
        }
        var events = Object.fromEntries(eventNames.map(function (eventName, idx) { return [eventName, _events[idx]]; }));
        return Promise.resolve({
            success: true,
            chainId: this.chainId,
            searchEndBlock: this.eventSearchConfig.to || latestBlockSearched,
            events: {
                updatedGlobalConfigEvents: events["UpdatedGlobalConfig"].map(utils_1.spreadEventWithBlockNumber),
                globalConfigUpdateTimes: globalConfigUpdateTimes,
                updatedTokenConfigEvents: events["UpdatedTokenConfig"].map(utils_1.spreadEventWithBlockNumber),
            },
        });
    };
    MockConfigStoreClient.prototype.updateGlobalConfig = function (key, value, overrides) {
        if (overrides === void 0) { overrides = {}; }
        return this.generateConfig("UpdatedGlobalConfig", (0, utils_1.utf8ToHex)(key), value, overrides);
    };
    MockConfigStoreClient.prototype.updateTokenConfig = function (key, value, overrides) {
        if (overrides === void 0) { overrides = {}; }
        if (ethers_1.ethers.utils.isAddress(key) === false) {
            throw new Error("Invalid address: ".concat(key));
        }
        return this.generateConfig("UpdatedTokenConfig", key, value, overrides);
    };
    MockConfigStoreClient.prototype.generateConfig = function (event, key, value, overrides) {
        if (overrides === void 0) { overrides = {}; }
        (0, assert_1.default)(this.eventManager !== null);
        var topics = [key, value];
        var args = { key: key, value: value };
        return this.eventManager.generateEvent({
            event: event,
            address: this.configStore.address,
            topics: topics.map(function (topic) { return topic.toString(); }),
            args: args,
            blockNumber: overrides.blockNumber,
        });
    };
    return MockConfigStoreClient;
}(AcrossConfigStoreClient_1.AcrossConfigStoreClient));
exports.MockConfigStoreClient = MockConfigStoreClient;
//# sourceMappingURL=MockConfigStoreClient.js.map