import { jsx as _jsx } from "react/jsx-runtime";
import { modal } from "../modal";
import { createContext, useCallback } from "react";
import { useAccountInstance, useLocalStorage, useWalletSubscription, useSettleSubscription, useEventEmitter, useMediaQuery } from "@orderly.network/hooks";
import { toast } from "../toast";
import { DepositAndWithdrawWithDialog, DepositAndWithdrawWithSheet } from "../block/depositAndwithdraw/depositAndwithdraw";
import { capitalizeString } from "@orderly.network/utils";
import { MEDIA_TABLET } from "@orderly.network/types";
export const AssetsContext = /*#__PURE__*/ createContext({});
export const AssetsProvider = (props)=>{
    const account = useAccountInstance();
    const matches = useMediaQuery(MEDIA_TABLET);
    const openDepositAndWithdraw = useCallback(async (viewName)=>{
        let result;
        if (matches) {
            result = await modal.show(DepositAndWithdrawWithSheet, {
                activeTab: viewName
            });
        } else {
            result = await modal.show(DepositAndWithdrawWithDialog, {
                activeTab: viewName
            });
        }
        return result;
    }, [
        matches
    ]);
    const onDeposit = useCallback(async ()=>{
        return openDepositAndWithdraw("deposit");
    }, [
        matches
    ]);
    const ee = useEventEmitter();
    const onWithdraw = useCallback(async ()=>{
        return openDepositAndWithdraw("withdraw");
    }, [
        matches
    ]);
    const onSettle = useCallback(async ()=>{
        return account.settle().catch((e)=>{
            if (e.code == -1104) {
                toast.error("Settlement is only allowed once every 10 minutes. Please try again later.");
                return Promise.reject(e);
            }
        }).then((res)=>{
            toast.success("Settlement requested");
            return Promise.resolve(res);
        });
    }, []);
    const [visible, setVisible] = useLocalStorage("orderly_assets_visible", true);
    const toggleVisible = useCallback(()=>{
        // @ts-ignore
        setVisible((visible)=>{
            return !visible;
        });
    }, [
        visible
    ]);
    useWalletSubscription({
        onMessage: (data)=>{
            //
            const { side, transStatus } = data;
            if (transStatus === "COMPLETED") {
                let msg = `${capitalizeString(side)} completed`;
                toast.success(msg);
            } else if (transStatus === "FAILED") {
                let msg = `${capitalizeString(side)} failed`;
                toast.error(msg);
            }
            ee.emit("wallet:changed", data);
        }
    });
    useSettleSubscription({
        onMessage: (data)=>{
            const { status } = data;
            // console.log("settle ws: ", data);
            switch(status){
                case "COMPLETED":
                    toast.success("Settlement completed");
                    break;
                case "FAILED":
                    toast.error("Settlement failed");
                    break;
                default:
                    break;
            }
        }
    });
    return /*#__PURE__*/ _jsx(AssetsContext.Provider, {
        value: {
            onDeposit,
            onWithdraw,
            onSettle,
            visible,
            toggleVisible
        },
        children: props.children
    });
};

//# sourceMappingURL=assetsProvider.js.map