import { HubPool__factory } from "../typechain";
import { utils as ethersUtils } from "ethers";
import { Balances } from "./utils";
export const 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) {
    const hubPoolInterface = new ethersUtils.Interface(Factory.abi);
    const eventName = event.event ?? "unknown";
    const { args } = hubPoolInterface.parseLog(event);
    const { l1Token } = args;
    const tokenEventState = state[l1Token] || tokenEventStateDefaults();
    const tokens = Balances(tokenEventState.tokenBalances);
    const lpTokens = 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;
}
export function getEventState(events, eventState = eventStateDefaults()) {
    return events.reduce(reduceEvents, eventState);
}
//# sourceMappingURL=hubPool.js.map