"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getEventState = exports.eventStateDefaults = exports.connect = exports.Factory = void 0;
var tslib_1 = require("tslib");
var typechain_1 = require("../typechain");
var ethers_1 = require("ethers");
var utils_1 = require("./utils");
exports.Factory = typechain_1.HubPool__factory;
function connect(address, provider) {
    return exports.Factory.connect(address, provider);
}
exports.connect = connect;
function eventStateDefaults() {
    return {};
}
exports.eventStateDefaults = eventStateDefaults;
function tokenEventStateDefaults() {
    return {
        tokenBalances: {},
        lpTokenBalances: {},
    };
}
function reduceEvents(state, event) {
    var _a, _b;
    var _c;
    var hubPoolInterface = new ethers_1.utils.Interface(exports.Factory.abi);
    var eventName = (_c = event.event) !== null && _c !== void 0 ? _c : "unknown";
    var args = hubPoolInterface.parseLog(event).args;
    var l1Token = args.l1Token;
    var tokenEventState = state[l1Token] || tokenEventStateDefaults();
    var tokens = (0, utils_1.Balances)(tokenEventState.tokenBalances);
    var lpTokens = (0, utils_1.Balances)(tokenEventState.lpTokenBalances);
    switch (eventName) {
        case "LiquidityAdded":
        case "LiquidityRemoved": {
            var amount = args.amount, liquidityProvider = args.liquidityProvider;
            if (event.event === "LiquidityAdded") {
                lpTokens.add(liquidityProvider, args.lpTokensMinted.toString());
                tokens.add(liquidityProvider, amount.toString());
            }
            else {
                lpTokens.sub(liquidityProvider, args.lpTokensBurnt.toString());
                tokens.sub(liquidityProvider, amount.toString());
            }
            return tslib_1.__assign(tslib_1.__assign({}, state), (_a = {}, _a[l1Token] = tslib_1.__assign(tslib_1.__assign({}, tokenEventState), { l1Token: l1Token, tokenBalances: tslib_1.__assign({}, tokens.balances), lpTokenBalances: tslib_1.__assign({}, lpTokens.balances) }), _a));
        }
        case "L1TokenEnabledForLiquidityProvision":
        case "L2TokenDisabledForLiquidityProvision": {
            var lpToken = args.lpToken;
            var enabled = event.event === "L1TokenEnabledForLiquidityProvision";
            return tslib_1.__assign(tslib_1.__assign({}, state), (_b = {}, _b[l1Token] = tslib_1.__assign(tslib_1.__assign({}, tokenEventState), { lpToken: lpToken, l1Token: l1Token, enabled: enabled }), _b));
        }
    }
    return state;
}
function getEventState(events, eventState) {
    if (eventState === void 0) { eventState = eventStateDefaults(); }
    return events.reduce(reduceEvents, eventState);
}
exports.getEventState = getEventState;
//# sourceMappingURL=hubPool.js.map