"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MockConfigStoreClient = void 0;
const tslib_1 = require("tslib");
const assert_1 = tslib_1.__importDefault(require("assert"));
const ethers_1 = require("ethers");
const utils_1 = require("../../utils");
const AcrossConfigStoreClient_1 = require("../AcrossConfigStoreClient");
const constants_1 = require("../../constants");
const MockEvents_1 = require("./MockEvents");
class MockConfigStoreClient extends AcrossConfigStoreClient_1.AcrossConfigStoreClient {
    configStoreVersion = AcrossConfigStoreClient_1.DEFAULT_CONFIG_STORE_VERSION;
    eventManager;
    availableChainIdsOverride;
    eventSignatures = {
        OwnershipTransferred: "address,address",
        UpdatedGlobalConfig: "bytes32,string",
        UpdatedTokenConfig: "address,string",
    };
    constructor(logger, configStore, eventSearchConfig = { from: 0, maxLookBack: 0 }, configStoreVersion, chainId = constants_1.CHAIN_IDs.MAINNET, mockUpdate = false, availableChainIdsOverride, opts = {}) {
        super(logger, configStore, eventSearchConfig, configStoreVersion);
        this.chainId = chainId;
        if (mockUpdate) {
            this.eventManager = opts.eventManager ?? new MockEvents_1.EventManager();
            this.eventManager.addEventSignatures(this.eventSignatures);
            this.updateGlobalConfig(AcrossConfigStoreClient_1.GLOBAL_CONFIG_STORE_KEYS.CHAIN_ID_INDICES, JSON.stringify(availableChainIdsOverride), {
                blockNumber: this.eventManager.blockNumber,
            });
        }
        else {
            this.eventManager = null;
        }
    }
    setAvailableChains(chainIds) {
        this.availableChainIdsOverride = chainIds;
    }
    getChainIdIndicesForBlock(block) {
        return this.availableChainIdsOverride ?? super.getChainIdIndicesForBlock(block);
    }
    getConfigStoreVersionForBlock(_blockNumber) {
        return this.configStoreVersion === AcrossConfigStoreClient_1.DEFAULT_CONFIG_STORE_VERSION
            ? super.getConfigStoreVersionForBlock(_blockNumber)
            : this.configStoreVersion;
    }
    setConfigStoreVersion(version) {
        this.configStoreVersion = version;
    }
    _update() {
        if (this.eventManager === null) {
            return super._update();
        }
        const eventNames = ["UpdatedGlobalConfig", "UpdatedTokenConfig"];
        const latestBlockSearched = this.eventManager.blockNumber;
        const globalConfigUpdateTimes = [];
        const _events = eventNames.map(() => []);
        for (const event of this.eventManager.getEvents().flat()) {
            const idx = eventNames.indexOf(event.event);
            if (idx !== -1) {
                _events[idx].push(event);
            }
            if (event.event === "UpdatedGlobalConfig") {
                globalConfigUpdateTimes.push((0, utils_1.getCurrentTime)());
            }
        }
        const events = Object.fromEntries(eventNames.map((eventName, idx) => [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,
                updatedTokenConfigEvents: events["UpdatedTokenConfig"].map(utils_1.spreadEventWithBlockNumber),
            },
        });
    }
    updateGlobalConfig(key, value, overrides = {}) {
        return this.generateConfig("UpdatedGlobalConfig", (0, utils_1.utf8ToHex)(key), value, overrides);
    }
    updateTokenConfig(key, value, overrides = {}) {
        if (ethers_1.ethers.utils.isAddress(key) === false) {
            throw new Error(`Invalid address: ${key}`);
        }
        return this.generateConfig("UpdatedTokenConfig", key, value, overrides);
    }
    generateConfig(event, key, value, overrides = {}) {
        (0, assert_1.default)(this.eventManager !== null);
        const topics = [key, value];
        const args = { key, value };
        return this.eventManager.generateEvent({
            event,
            address: this.configStore.address,
            topics: topics.map((topic) => topic.toString()),
            args,
            blockNumber: overrides.blockNumber,
        });
    }
}
exports.MockConfigStoreClient = MockConfigStoreClient;
//# sourceMappingURL=MockConfigStoreClient.js.map