"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getEventManager = exports.EventManager = void 0;
var tslib_1 = require("tslib");
var assert_1 = tslib_1.__importDefault(require("assert"));
var ethers_1 = require("ethers");
var lodash_1 = require("lodash");
var utils_1 = require("../../utils");
var id = ethers_1.utils.id, keccak256 = ethers_1.utils.keccak256, toUtf8Bytes = ethers_1.utils.toUtf8Bytes;
var eventManagers = {};
var EventManager = (function () {
    function EventManager(blockNumber) {
        if (blockNumber === void 0) { blockNumber = 0; }
        this.blockNumber = blockNumber;
        this.logIndexes = {};
        this.events = [];
        this.minBlockRange = 10;
        this.eventSignatures = {};
    }
    EventManager.prototype.addEventSignatures = function (eventSignatures) {
        var _this = this;
        Object.entries(eventSignatures).forEach(function (_a) {
            var event = _a[0], signature = _a[1];
            if ((0, utils_1.isDefined)(_this.eventSignatures[event])) {
                (0, assert_1.default)(signature === _this.eventSignatures[event], "Event ".concat(event, " conflict detected."));
            }
            _this.eventSignatures[event] = signature;
        });
    };
    EventManager.prototype.addEvent = function (event) {
        this.events.push(event);
    };
    EventManager.prototype.getEvents = function () {
        var events = this.events;
        this.events = [];
        return events;
    };
    EventManager.prototype.generateEvent = function (inputs) {
        var _a;
        var _b;
        var address = inputs.address, event = inputs.event, topics = inputs.topics, data = inputs.data, args = inputs.args;
        var blockNumber = inputs.blockNumber, transactionIndex = inputs.transactionIndex;
        var eventSignature = "".concat(event, "(").concat(this.eventSignatures[event], ")");
        blockNumber !== null && blockNumber !== void 0 ? blockNumber : (blockNumber = (0, lodash_1.random)(this.blockNumber + 1, this.blockNumber + this.minBlockRange, false));
        (0, assert_1.default)(blockNumber >= this.blockNumber, "".concat(blockNumber, " < ").concat(this.blockNumber));
        this.blockNumber = blockNumber;
        transactionIndex !== null && transactionIndex !== void 0 ? transactionIndex : (transactionIndex = (0, lodash_1.random)(1, 32, false));
        var _logIndex = "".concat(blockNumber, "-").concat(transactionIndex);
        (_a = (_b = this.logIndexes)[_logIndex]) !== null && _a !== void 0 ? _a : (_b[_logIndex] = 0);
        var generatedEvent = {
            event: event,
            blockNumber: blockNumber,
            transactionIndex: transactionIndex,
            logIndex: this.logIndexes[_logIndex]++,
            transactionHash: id("Across-".concat(event, "-").concat(blockNumber, "-").concat(transactionIndex, "-").concat((0, lodash_1.random)(1, 100000))),
            removed: false,
            address: address,
            data: data !== null && data !== void 0 ? data : id("Across-random-txndata-".concat((0, lodash_1.random)(1, 100000))),
            topics: tslib_1.__spreadArray([keccak256(toUtf8Bytes(eventSignature))], topics, true),
            args: args,
            blockHash: id("Across-blockHash-".concat((0, lodash_1.random)(1, 100000))),
        };
        this.addEvent(generatedEvent);
        return generatedEvent;
    };
    return EventManager;
}());
exports.EventManager = EventManager;
function getEventManager(chainId, eventSignatures, blockNumber) {
    if (!(0, utils_1.isDefined)(eventManagers[chainId])) {
        eventManagers[chainId] = new EventManager(blockNumber);
    }
    var eventManager = eventManagers[chainId];
    if ((0, utils_1.isDefined)(eventSignatures)) {
        eventManager.addEventSignatures(eventSignatures);
    }
    return eventManager;
}
exports.getEventManager = getEventManager;
//# sourceMappingURL=MockEvents.js.map