"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, {
    WalletConnectDialog: function() {
        return WalletConnectDialog;
    },
    WalletConnectSheet: function() {
        return WalletConnectSheet;
    },
    showAccountConnectorModal: function() {
        return showAccountConnectorModal;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _modal = require("../../modal");
const _modalHelper = require("../../modal/modalHelper");
const _types = require("@orderly.network/types");
const _sheet = require("../../sheet");
const _toast = require("../../toast");
const _hooks = require("@orderly.network/hooks");
const _react = require("react");
const _walletConnect = require("./walletConnect");
const _logo = require("../../logo");
const _dialog = require("../../dialog");
const useWalletConnect = ()=>{
    const { visible, hide, resolve, reject, onOpenChange } = (0, _modal.useModal)();
    // get account status and handle sign in and enable trading
    const { account, createOrderlyKey, createAccount } = (0, _hooks.useAccount)();
    const onSignIn = (0, _react.useCallback)(()=>{
        return createAccount().catch((err)=>{
            reject();
            _toast.toast.error(err.message);
            hide();
        });
    }, [
        account
    ]);
    const onComplete = (0, _react.useCallback)(()=>{
        _toast.toast.success("Wallet connected");
        resolve();
        hide();
    }, []);
    return {
        visible,
        hide,
        resolve,
        reject,
        onOpenChange,
        onSignIn,
        onComplete,
        createOrderlyKey
    };
};
const WalletConnectSheet = (0, _modalHelper.create)((props)=>{
    // const { visible, hide, resolve, reject, onOpenChange } = useModal();
    // // get account status and handle sign in and enable trading
    // const { account, createOrderlyKey, createAccount } = useAccount();
    // // @ts-ignore
    // const { logoUrl } = useContext(OrderlyContext);
    // const onSignIn = useCallback(() => {
    //   return createAccount().catch((err: Error) => {
    //     reject();
    //     toast.error(err.message);
    //     hide();
    //   });
    // }, [account]);
    // const onComplete = useCallback(() => {
    //   toast.success("Wallet connected");
    //   resolve();
    //   hide();
    // }, []);
    const { visible, hide, resolve, reject, onOpenChange, onSignIn, onComplete, createOrderlyKey } = useWalletConnect();
    return /*#__PURE__*/ (0, _jsxruntime.jsx)(_sheet.Sheet, {
        open: visible,
        onOpenChange: onOpenChange,
        children: /*#__PURE__*/ (0, _jsxruntime.jsxs)(_sheet.SheetContent, {
            onOpenAutoFocus: (e)=>e.preventDefault(),
            children: [
                /*#__PURE__*/ (0, _jsxruntime.jsx)(_sheet.SheetHeader, {
                    leading: /*#__PURE__*/ (0, _jsxruntime.jsx)(_logo.Logo, {}),
                    children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_sheet.SheetTitle, {
                        children: "Connect wallet"
                    })
                }),
                /*#__PURE__*/ (0, _jsxruntime.jsx)(_walletConnect.WalletConnect, {
                    onEnableTrading: createOrderlyKey,
                    onSignIn: onSignIn,
                    onComplete: onComplete,
                    ...props
                })
            ]
        })
    });
});
const WalletConnectDialog = (0, _modalHelper.create)((props)=>{
    const { visible, hide, resolve, reject, onOpenChange, onSignIn, onComplete, createOrderlyKey } = useWalletConnect();
    const _onOpenChange = (0, _react.useCallback)((open)=>{
        // console.log(open);
        if (!open) reject("cancel");
        onOpenChange(open);
    }, []);
    return /*#__PURE__*/ (0, _jsxruntime.jsx)(_dialog.Dialog, {
        open: visible,
        onOpenChange: _onOpenChange,
        children: /*#__PURE__*/ (0, _jsxruntime.jsxs)(_dialog.DialogContent, {
            maxWidth: "sm",
            closable: true,
            onOpenAutoFocus: (e)=>e.preventDefault(),
            className: "orderly-bg-base-800 desktop:orderly-max-w-[400px] orderly-px-[24px] orderly-py-6",
            children: [
                /*#__PURE__*/ (0, _jsxruntime.jsx)(_dialog.DialogHeader, {
                    children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_dialog.DialogTitle, {
                        children: "Connect wallet"
                    })
                }),
                /*#__PURE__*/ (0, _jsxruntime.jsx)(_dialog.DialogBody, {
                    children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_walletConnect.WalletConnect, {
                        onEnableTrading: createOrderlyKey,
                        onSignIn: onSignIn,
                        onComplete: onComplete,
                        ...props
                    })
                })
            ]
        })
    });
});
const showAccountConnectorModal = async (props)=>{
    const matches = window.matchMedia(_types.MEDIA_TABLET).matches;
    if (matches) {
        return await _modal.modal.show(WalletConnectSheet, props);
    } else {
        return await _modal.modal.show(WalletConnectDialog, props);
    }
};

//# sourceMappingURL=walletModal.js.map