import { map } from "rxjs";
import { useEffect, useMemo } from "react";
import { useObsState } from "../../hooks/rxjs";
import { createSimpleContext } from "../simple";
const Context = createSimpleContext("eq-rxjs-api", undefined, ({ useNet, useBase }) => {
    const { api: eqApi } = useBase();
    const { network } = useNet();
    const _api = useMemo(() => {
        if (eqApi && network?.type) {
            // TODO do not use api creator; refactor to ApiRx.create
            const api$ = eqApi.getApiCreatorRx(network.type)(network.node);
            const apiRx$ = api$.pipe(map((api) => api._api));
            return { api$, apiRx$ };
        }
    }, [eqApi, network]);
    const api = useObsState(_api?.api$);
    useEffect(() => () => {
        api?._api.disconnect();
    }, [api]);
    return useMemo(() => api && _api?.api$
        ? Object.assign(api, { api$: _api?.api$, apiRx$: _api.apiRx$ })
        : undefined, [api, _api]);
});
export const ApiProvider = Context.Provider;
// @ts-expect-error
export const useApi = Context.useContext;
//# sourceMappingURL=api.js.map