"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "ChainIdSwtich", {
    enumerable: true,
    get: function() {
        return ChainIdSwtich;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _react = require("react");
const _chainPicker = require("../../../block/pickers/chainPicker");
const _dialog = require("../../../dialog");
const _hooks = require("@orderly.network/hooks");
const _toast = require("../../../toast");
const _i18n = require("../../../i18n");
const _provider = require("../../../provider");
const _button = /*#__PURE__*/ _interop_require_default(require("../../../button"));
const _utils = require("@orderly.network/utils");
const _utils1 = require("../../../utils");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
const ChainIdSwtich = (props)=>{
    const [open, setOpen] = (0, _react.useState)(false);
    const { onChainChanged } = (0, _react.useContext)(_provider.OrderlyAppContext);
    const [testChains] = (0, _hooks.useChains)("testnet", {
        pick: "network_infos",
        filter: (item)=>(0, _utils.isTestnet)(item.network_infos?.chain_id)
    });
    const [mainChains] = (0, _hooks.useChains)("mainnet", {
        pick: "network_infos",
        filter: (chain)=>chain.network_infos?.bridge_enable || chain.network_infos?.bridgeless
    });
    const t = (0, _i18n.useTranslation)();
    const onChainChange = (0, _react.useCallback)(({ id, name })=>{
        props.onSetChain(id).then((isSuccess)=>{
            if (isSuccess) {
                _toast.toast.success(t("toast.networkSwitched"));
                if (onChainChanged) {
                    onChainChanged(id, (0, _utils.isTestnet)(id));
                }
            } else {
                _toast.toast.error(t("common.cancel"));
            }
        }, (error)=>{
            _toast.toast.error(error.message);
        }).finally(()=>setOpen(false));
    }, []);
    return /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
        id: "orderly-chain-id-switch",
        className: (0, _utils1.cn)("orderly-bg-warning-darken orderly-fixed orderly-left-0 orderly-right-0 orderly-bottom-[64px] orderly-text-warning orderly-z-10 orderly-text-3xs orderly-font-semibold orderly-leading-[18px] orderly-p-[10px]", "desktop:orderly-h-[40px] desktop:orderly-text-sm desktop:orderly-flex desktop:orderly-items-center desktop:orderly-justify-center desktop:orderly-static"),
        children: [
            /*#__PURE__*/ (0, _jsxruntime.jsx)("span", {
                children: "Please connect to a supported network."
            }),
            /*#__PURE__*/ (0, _jsxruntime.jsxs)(_dialog.Dialog, {
                open: open,
                onOpenChange: setOpen,
                children: [
                    /*#__PURE__*/ (0, _jsxruntime.jsxs)(_dialog.DialogTrigger, {
                        children: [
                            /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                                className: "desktop:orderly-hidden orderly-text-link-light orderly-ml-[2px]",
                                children: "Switch network"
                            }),
                            /*#__PURE__*/ (0, _jsxruntime.jsx)(_button.default, {
                                variant: "outlined",
                                size: "small",
                                className: "orderly-hidden desktop:orderly-block orderly-text-warning orderly-border-warning hover:orderly-text-warning orderly-px-[8px] orderly-ml-[10px] orderly-text-3xs desktop:orderly-text-3xs",
                                children: "Switch network"
                            })
                        ]
                    }),
                    /*#__PURE__*/ (0, _jsxruntime.jsxs)(_dialog.DialogContent, {
                        closable: true,
                        children: [
                            /*#__PURE__*/ (0, _jsxruntime.jsx)(_dialog.DialogHeader, {
                                className: "orderly-switch-network-dialog-title orderly-text-xs",
                                children: "Switch network"
                            }),
                            /*#__PURE__*/ (0, _jsxruntime.jsx)(_dialog.DialogBody, {
                                className: "orderly-max-h-[327.5px] orderly-overflow-y-auto orderly-text-3xs",
                                children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_chainPicker.ChainListView, {
                                    // @ts-ignore
                                    mainChains: mainChains,
                                    // @ts-ignore
                                    testChains: testChains,
                                    // @ts-ignore
                                    onItemClick: onChainChange
                                })
                            })
                        ]
                    })
                ]
            })
        ]
    });
};

//# sourceMappingURL=chainIdSwitch.js.map