"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Factory = void 0;
exports.connect = connect;
exports.eventStateDefaults = eventStateDefaults;
exports.getEventState = getEventState;
const typechain_1 = require("../typechain");
const ethers_1 = require("ethers");
const utils_1 = require("./utils");
exports.Factory = typechain_1.HubPool__factory;
function connect(address, provider) {
    return exports.Factory.connect(address, provider);
}
function eventStateDefaults() {
    return {};
}
function tokenEventStateDefaults() {
    return {
        tokenBalances: {},
        lpTokenBalances: {},
    };
}
function reduceEvents(state, event) {
    const hubPoolInterface = new ethers_1.utils.Interface(exports.Factory.abi);
    const eventName = event.event ?? "unknown";
    const { args } = hubPoolInterface.parseLog(event);
    const { l1Token } = args;
    const tokenEventState = state[l1Token] || tokenEventStateDefaults();
    const tokens = (0, utils_1.Balances)(tokenEventState.tokenBalances);
    const lpTokens = (0, utils_1.Balances)(tokenEventState.lpTokenBalances);
    switch (eventName) {
        case "LiquidityAdded":
        case "LiquidityRemoved": {
            const { amount, liquidityProvider } = args;
            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 {
                ...state,
                [l1Token]: {
                    ...tokenEventState,
                    l1Token,
                    tokenBalances: {
                        ...tokens.balances,
                    },
                    lpTokenBalances: {
                        ...lpTokens.balances,
                    },
                },
            };
        }
        case "L1TokenEnabledForLiquidityProvision":
        case "L2TokenDisabledForLiquidityProvision": {
            const { lpToken } = args;
            const enabled = event.event === "L1TokenEnabledForLiquidityProvision";
            return {
                ...state,
                [l1Token]: {
                    ...tokenEventState,
                    lpToken,
                    l1Token,
                    enabled,
                },
            };
        }
    }
    return state;
}
function getEventState(events, eventState = eventStateDefaults()) {
    return events.reduce(reduceEvents, eventState);
}
//# sourceMappingURL=hubPool.js.map