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

//# sourceMappingURL=assetsProvider.js.map