import { __assign } from "tslib";
import { HubPool__factory } from "../typechain";
import { utils as ethersUtils } from "ethers";
import { Balances } from "./utils";
export var Factory = HubPool__factory;
export function connect(address, provider) {
    return Factory.connect(address, provider);
}
export function eventStateDefaults() {
    return {};
}
function tokenEventStateDefaults() {
    return {
        tokenBalances: {},
        lpTokenBalances: {},
    };
}
function reduceEvents(state, event) {
    var _a, _b;
    var _c;
    var hubPoolInterface = new ethersUtils.Interface(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 = Balances(tokenEventState.tokenBalances);
    var lpTokens = 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 __assign(__assign({}, state), (_a = {}, _a[l1Token] = __assign(__assign({}, tokenEventState), { l1Token: l1Token, tokenBalances: __assign({}, tokens.balances), lpTokenBalances: __assign({}, lpTokens.balances) }), _a));
        }
        case "L1TokenEnabledForLiquidityProvision":
        case "L2TokenDisabledForLiquidityProvision": {
            var lpToken = args.lpToken;
            var enabled = event.event === "L1TokenEnabledForLiquidityProvision";
            return __assign(__assign({}, state), (_b = {}, _b[l1Token] = __assign(__assign({}, tokenEventState), { lpToken: lpToken, l1Token: l1Token, enabled: enabled }), _b));
        }
    }
    return state;
}
export function getEventState(events, eventState) {
    if (eventState === void 0) { eventState = eventStateDefaults(); }
    return events.reduce(reduceEvents, eventState);
}
//# sourceMappingURL=hubPool.js.map