"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "WalletConnect", {
    enumerable: true,
    get: function() {
        return WalletConnect;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _layout = require("../../layout");
const _listTile = require("../../listView/listTile");
const _switch = require("../../switch");
const _react = require("react");
const _types = require("@orderly.network/types");
const _step = require("./sections/step");
const _hooks = require("@orderly.network/hooks");
const _button = /*#__PURE__*/ _interop_require_default(require("../../button"));
const _toast = require("../../toast");
const _rememberMe = require("./sections/rememberMe");
const _referralCode = require("./sections/referralCode");
const _provider = require("../../provider");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
const WalletConnect = (props)=>{
    // const { status = AccountStatusEnum.NotConnected } = props;
    const { state: { status, isNew } } = (0, _hooks.useAccount)();
    const [handleStep, setHandleStep] = (0, _react.useState)(0);
    const [remember, setRemember] = (0, _react.useState)(true);
    const localRefCode = localStorage.getItem("referral_code") || undefined;
    const [refCode, setRefCode] = (0, _react.useState)(localRefCode);
    const [bindRefCode, { error: updateOrderError, isMutating: updateMutating }] = (0, _hooks.useMutation)("/v1/referral/bind", "POST");
    const { referral } = (0, _react.useContext)(_provider.OrderlyAppContext);
    const buttonLabel = (0, _react.useMemo)(()=>{
        if (status < _types.AccountStatusEnum.SignedIn) {
            return "Sign in";
        }
        if (status < _types.AccountStatusEnum.EnableTrading) {
            return "Enable Trading";
        }
        return "--";
    }, [
        status
    ]);
    const onClick = (0, _react.useCallback)(()=>{
        if (status < _types.AccountStatusEnum.SignedIn) {
            setHandleStep(1);
            return props.onSignIn?.().finally(()=>{
                setHandleStep(0);
            });
        }
        if (status < _types.AccountStatusEnum.EnableTrading) {
            setHandleStep(2);
            return props.onEnableTrading?.(remember).then(()=>{
                if (refCode && refCode.length > 0) {
                    bindRefCode({
                        referral_code: refCode
                    }).then((res)=>{
                        referral?.onBoundRefCode?.(true, undefined);
                    }).catch((e)=>{
                        referral?.onBoundRefCode?.(false, e);
                    }).finally(()=>{
                        localStorage.removeItem("referral_code");
                    });
                }
                props.onComplete?.();
            }, (e)=>{
                let errorText = `${e}`;
                if ("message" in e) {
                    errorText = e.message;
                }
                if ("referral code not exist" === errorText) {
                    errorText = "This referral code does not exist";
                }
                _toast.toast.error(errorText);
            }).finally(()=>{
                setHandleStep(0);
            });
        }
    }, [
        status,
        remember,
        refCode
    ]);
    const isExpired = status === _types.AccountStatusEnum.DisabledTrading && !isNew;
    return /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
        id: "orderly-wallet-connect-view",
        children: [
            /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                className: "orderly-text-base-contrast-54 orderly-text-2xs orderly-py-4 desktop:orderly-text-base",
                children: isExpired ? "Your previous access has expired, you will receive a signature request to enable trading. Signing is free and will not send a transaction." : "Sign two requests to verify ownership of your wallet and enable trading. Signing is free."
            }),
            /*#__PURE__*/ (0, _jsxruntime.jsxs)(_layout.Paper, {
                className: "orderly-bg-base-500",
                children: [
                    !isExpired && /*#__PURE__*/ (0, _jsxruntime.jsx)(_listTile.ListTile, {
                        className: "orderly-text-xs desktop:orderly-text-base",
                        avatar: /*#__PURE__*/ (0, _jsxruntime.jsx)(_step.StepItem, {
                            active: status <= _types.AccountStatusEnum.NotSignedIn,
                            isLoading: handleStep === 1,
                            isCompleted: status >= _types.AccountStatusEnum.SignedIn,
                            children: "1"
                        }),
                        title: "Sign in",
                        disabled: status < _types.AccountStatusEnum.NotConnected || status >= _types.AccountStatusEnum.SignedIn,
                        subtitle: "Confirm you own this wallet"
                    }),
                    /*#__PURE__*/ (0, _jsxruntime.jsx)(_listTile.ListTile, {
                        className: "orderly-text-xs desktop:orderly-text-base",
                        disabled: status < _types.AccountStatusEnum.SignedIn,
                        avatar: /*#__PURE__*/ (0, _jsxruntime.jsx)(_step.StepItem, {
                            active: status >= _types.AccountStatusEnum.SignedIn,
                            isLoading: handleStep === 2,
                            isCompleted: status >= _types.AccountStatusEnum.EnableTrading,
                            children: isExpired ? 1 : 2
                        }),
                        title: "Enable Trading",
                        subtitle: "Enable secure access to our API for lightning-fast trading"
                    })
                ]
            }),
            /*#__PURE__*/ (0, _jsxruntime.jsx)(_referralCode.ReferralCode, {
                className: "orderly-pt-5",
                refCode: refCode,
                setRefCode: setRefCode
            }),
            /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
                className: "orderly-pt-5 orderly-pb-7 orderly-flex orderly-justify-between orderly-items-center",
                children: [
                    /*#__PURE__*/ (0, _jsxruntime.jsx)(_rememberMe.RememberMe, {}),
                    /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                        children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_switch.Switch, {
                            id: "orderly-remember-me-switch",
                            checked: remember,
                            onCheckedChange: setRemember
                        })
                    })
                ]
            }),
            /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_button.default, {
                    className: "orderly-text-xs orderly-text-base-contrast",
                    fullWidth: true,
                    disabled: handleStep > 0,
                    onClick: onClick,
                    loading: handleStep > 0,
                    children: buttonLabel
                })
            })
        ]
    });
};

//# sourceMappingURL=walletConnect.js.map