"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventManager = void 0;
const tslib_1 = require("tslib");
const assert_1 = tslib_1.__importDefault(require("assert"));
const ethers_1 = require("ethers");
const lodash_1 = require("lodash");
const utils_1 = require("../../utils");
const { id, keccak256, toUtf8Bytes } = ethers_1.utils;
class EventManager {
    blockNumber;
    logIndexes = {};
    events = [];
    minBlockRange = 10;
    eventSignatures = {};
    constructor(blockNumber = 0) {
        this.blockNumber = blockNumber;
    }
    addEventSignatures(eventSignatures) {
        Object.entries(eventSignatures).forEach(([event, signature]) => {
            if ((0, utils_1.isDefined)(this.eventSignatures[event])) {
                (0, assert_1.default)(signature === this.eventSignatures[event], `Event ${event} conflict detected.`);
            }
            this.eventSignatures[event] = signature;
        });
    }
    addEvent(event) {
        this.events.push(event);
    }
    getEvents() {
        const events = this.events;
        this.events = [];
        return events;
    }
    reset(blockNumber = 0) {
        this.blockNumber = blockNumber;
        this.logIndexes = {};
        this.events = [];
    }
    generateEvent(inputs) {
        const { address, event, topics, data, args } = inputs;
        let { blockNumber, transactionIndex } = inputs;
        const eventSignature = `${event}(${this.eventSignatures[event]})`;
        blockNumber ??= (0, lodash_1.random)(this.blockNumber + 1, this.blockNumber + this.minBlockRange, false);
        (0, assert_1.default)(blockNumber >= this.blockNumber, `${blockNumber} < ${this.blockNumber}`);
        this.blockNumber = blockNumber;
        transactionIndex ??= (0, lodash_1.random)(1, 32, false);
        const _logIndex = `${blockNumber}-${transactionIndex}`;
        this.logIndexes[_logIndex] ??= 0;
        const generatedEvent = {
            event,
            blockNumber,
            transactionIndex,
            logIndex: this.logIndexes[_logIndex]++,
            transactionHash: id(`Across-${event}-${blockNumber}-${transactionIndex}-${(0, lodash_1.random)(1, 100_000)}`),
            removed: false,
            address,
            data: data ?? id(`Across-random-txndata-${(0, lodash_1.random)(1, 100_000)}`),
            topics: [keccak256(toUtf8Bytes(eventSignature)), ...topics],
            args,
            blockHash: id(`Across-blockHash-${(0, lodash_1.random)(1, 100_000)}`),
        };
        this.addEvent(generatedEvent);
        return generatedEvent;
    }
}
exports.EventManager = EventManager;
//# sourceMappingURL=MockEvents.js.map