import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import BigNumber from "bignumber.js";
import React from "react";
import { of } from "rxjs";
import { map, mergeMap } from "rxjs/operators";
import { useObsState } from "../../hooks/rxjs";
import { NetworkProvider, useNetwork } from "../polkadot/network";
import { useSession, SessionProvider } from "../session";
import { BaseProvider, useBase } from "./base";
import { ApiProvider, useApi } from "./api";
import { ConnectProvider, useConnect } from "./connect";
import { useStillNotifications, StillNotificationsProvider, } from "../still-notifications/context";
const networks = [
    {
        type: "Gens",
        name: "DevNet",
        node: "wss://testnet.genshiro.io",
        ss58: 67,
    },
];
const DECIMALS_MULTIPLIER = new BigNumber(10).pow(-9);
const SOFT_CAP = new BigNumber(1e4).times(1e9);
const HARD_CAP = new BigNumber(2e6).times(1e9);
const Inner = () => {
    const { accounts, account, connect, select, signer } = useConnect();
    const api = useApi();
    const { api: eqApi } = useBase();
    const tokenFromAsset = React.useMemo(() => eqApi?.tokenFromAsset, [eqApi]);
    React.useEffect(() => {
        if (api && signer) {
            api._api.setSigner(signer);
        }
    }, [api, signer]);
    // TODO create feature context
    const assetsObs = React.useMemo(() => api
        ? api.query
            .assetInfo()
            .pipe(map((option) => option.unwrapOr(undefined)))
        : of(undefined), [api]);
    // TODO create balance context
    const balancesObs = React.useMemo(() => assetsObs
        .pipe(map((assets) => account && assets
        ? assets.map((asset) => [
            account.address,
            // @ts-ignore
            asset.id,
        ])
        : undefined))
        .pipe(mergeMap((query) => query
        ? api._api.query.eqBalances.account
            .multi(query)
            .pipe(map((balances) => balances.reduce((prev, balance, i) => ({
            ...prev,
            [tokenFromAsset({
                // TODO refactor tokenFromAsset function
                // @ts-ignore
                0: query[i][1][0].toNumber(),
            })]: DECIMALS_MULTIPLIER.times(balance.isPositive
                ? balance.asPositive.toString(10)
                : "-" + balance.asNegative.toString(10)),
        }), {})))
        : of(undefined))), [api, account, assetsObs, tokenFromAsset]);
    const allocObs = React.useMemo(() => api && account
        ? // @ts-ignore
            api._api.query.gensCrowdloan.allocations(account.address).pipe(
            // @ts-ignore
            map((balance) => DECIMALS_MULTIPLIER.times(balance.toString(10))))
        : of(undefined), [api, account]);
    const alloc = useObsState(allocObs);
    const balances = useObsState(balancesObs);
    return (_jsxs("div", { children: [_jsxs("div", { children: ["Current:", " ", account ? (account.address) : (_jsx("span", { onClick: () => connect(), children: "Connect" }))] }), accounts?.map(({ address }) => (_jsx("p", { onClick: () => select(address), children: address }, address))), alloc && _jsxs("p", { children: ["Your allocation: ", alloc.toFixed(4), " GENS"] }), balances && _jsxs("p", { children: ["Your balances: ", JSON.stringify(balances, null, 2)] })] }));
};
export const NewApi = () => {
    return (_jsx(BaseProvider, { children: _jsx(SessionProvider, { children: _jsx(NetworkProvider, { networks: networks, lsKey: "TEST_NETWORK", children: _jsx(ApiProvider, { useNet: useNetwork, useBase: useBase, children: _jsx(StillNotificationsProvider, { children: _jsx(ConnectProvider, { name: "test-new-api", useNet: useNetwork, useBase: useBase, useSession: useSession, useApi: useApi, useStillNotifications: useStillNotifications, children: _jsx(Inner, {}) }) }) }) }) }) }));
};
const meta = {
    title: "Composed/Playground",
};
export default meta;
//# sourceMappingURL=index.stories.js.map