"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "Chains", {
    enumerable: true,
    get: function() {
        return Chains;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _react = require("react");
const _chainPicker = require("../../../block/pickers/chainPicker");
const _button = /*#__PURE__*/ _interop_require_default(require("../../../button"));
const _dialog = require("../../../dialog");
const _types = require("@orderly.network/types");
const _hooks = require("@orderly.network/hooks");
const _icon = require("../../../icon");
const _provider = require("../../../provider");
const _css = require("../../../utils/css");
const _utils = require("@orderly.network/utils");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
const Chains = (props)=>{
    const { disabled } = props;
    const [open, setOpen] = (0, _react.useState)(false);
    const { networkId } = (0, _react.useContext)(_hooks.OrderlyContext);
    const { onChainChanged } = (0, _react.useContext)(_provider.OrderlyAppContext);
    const [defaultChain, setDefaultChain] = (0, _react.useState)(networkId === "mainnet" ? _types.ARBITRUM_MAINNET_CHAINID_HEX : _types.ARBITRUM_TESTNET_CHAINID_HEX);
    const [testChains] = (0, _hooks.useChains)("testnet", {
        pick: "network_infos",
        filter: (item)=>(0, _utils.isTestnet)(item.network_infos?.chain_id)
    });
    const [mainChains, { findByChainId }] = (0, _hooks.useChains)("mainnet", {
        pick: "network_infos",
        filter: (chain)=>chain.network_infos?.bridge_enable || chain.network_infos?.bridgeless
    });
    const { connectedChain, setChain, settingChain } = (0, _hooks.useWalletConnector)();
    const resetDefaultChain = (0, _react.useCallback)(()=>{
        if (networkId === "mainnet" && defaultChain !== _types.ARBITRUM_MAINNET_CHAINID_HEX) {
            setDefaultChain(_types.ARBITRUM_MAINNET_CHAINID_HEX);
        } else if (networkId === "testnet" && defaultChain !== _types.ARBITRUM_TESTNET_CHAINID_HEX) {
            setDefaultChain(_types.ARBITRUM_TESTNET_CHAINID_HEX);
        }
    }, [
        defaultChain
    ]);
    const chainName = (0, _react.useMemo)(()=>{
        const chain = findByChainId(// @ts-ignore
        parseInt(connectedChain?.id || defaultChain), "network_infos");
        if (!chain) return /*#__PURE__*/ (0, _jsxruntime.jsx)("span", {
            children: "Unknown"
        });
        // @ts-ignore
        if ((0, _utils.isTestnet)(chain.chain_id)) {
            return /*#__PURE__*/ (0, _jsxruntime.jsx)("span", {
                children: "Testnet"
            });
        }
        // @ts-ignore
        return /*#__PURE__*/ (0, _jsxruntime.jsx)(_icon.NetworkImage, {
            id: chain.chain_id,
            type: "chain",
            size: 16
        });
    }, [
        connectedChain,
        findByChainId,
        defaultChain
    ]);
    const switchDomain = (chainId)=>{
        // const domain = configStore.get("domain");
        // const url = isTestnet(chainId) ? domain?.testnet : domain?.mainnet;
        // window.location.href = url;
        // window.open(url); // test in storybook
        // console.log("onChainChanged", chainId, isTestnet(chainId), onChainChanged);
        if (onChainChanged) {
            onChainChanged(chainId, (0, _utils.isTestnet)(chainId));
        }
    };
    return /*#__PURE__*/ (0, _jsxruntime.jsxs)(_dialog.Dialog, {
        open: open,
        onOpenChange: setOpen,
        children: [
            /*#__PURE__*/ (0, _jsxruntime.jsx)(_dialog.DialogTrigger, {
                children: /*#__PURE__*/ (0, _jsxruntime.jsxs)(_button.default, {
                    id: "orderly-botom-bar-switch-chain-button",
                    variant: "outlined",
                    size: "small",
                    color: "buy",
                    loading: settingChain,
                    disabled: disabled,
                    className: (0, _css.cn)("orderly-border-primary orderly-gap-1 orderly-text-base-contrast orderly-h-[30px] hover:orderly-text-primary-light hover:orderly-bg-transparent active:orderly-bg-transparent", props.className),
                    children: [
                        chainName,
                        /*#__PURE__*/ (0, _jsxruntime.jsx)(_icon.ArrowIcon, {
                            size: 8,
                            className: "orderly-text-base-contrast-54"
                        })
                    ]
                })
            }),
            /*#__PURE__*/ (0, _jsxruntime.jsxs)(_dialog.DialogContent, {
                onOpenAutoFocus: (event)=>event.preventDefault(),
                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",
                        children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_chainPicker.ChainListView, {
                            // @ts-ignore
                            mainChains: mainChains,
                            // @ts-ignore
                            testChains: testChains,
                            // @ts-ignore
                            onItemClick: (item)=>{
                                setOpen(false);
                                if (connectedChain) {
                                    setChain({
                                        chainId: item.id
                                    }).then((success)=>{
                                        // reset default chain when switch to connected chain
                                        resetDefaultChain();
                                        if (success) {
                                            switchDomain(item.id);
                                        }
                                    });
                                } else {
                                    setDefaultChain(item.id);
                                    switchDomain(item.id);
                                }
                            },
                            // @ts-ignore
                            currentChainId: parseInt(connectedChain?.id || defaultChain)
                        })
                    })
                ]
            })
        ]
    });
};

//# sourceMappingURL=chains.js.map