// import {useStatusStore, useWalletStore} from "../store";
//@ts-ignore
import MetaMaskSDK from ".metamask-sdk.js";
// @ts-ignore
// import whitelist from "../whitelist/whitelest.js";
// import Web3 from "web3";

const MMSDK = new MetaMaskSDK()
const MM = MMSDK.getProvider()
//@ts-ignore
// const web3 = new Web3(window.web3.currentProvider)
// let status = useStatusStore()
// let wallet = useWalletStore()
let chainId = '0x324'
// const mintCosts = 0.08//铸币费用(一个)

//获取钱包地址
const getWalletAddress = async (pop?: boolean) => {
    let accounts: string[]
    if (pop) {
        accounts = await MM.value.request({method: 'eth_requestAccounts'});
    } else {
        accounts = await MM.value.request({method: 'eth_accounts'});
    }
    if (accounts.length >= 1) {
        // wallet.address = accounts[0]
        // status.isConnected = true
    } else {
        // wallet.address = ''
        // status.isConnected = false
    }
}
//获取链id
const getChainId = async () => {
    // wallet.chainId = await MM.value.request({method: 'eth_chainId'});
}
//获取eth余额
const getEthBalance = () => {
    // web3.eth.getBalance(wallet.address).then(res => {
    //     wallet.ethAmount = parseFloat(res) / (10 ** 18)
    // })
}
//检查是否在白名单内
const checkWhitelist = () => {
    // whitelist.forEach((item: string) => {
    //     if (item.toLowerCase() === wallet.address) {
    //         status.isInWhitelist = true
    //     }
    // })
}
//检查是否余额不足以支付
const checkEthBalance = (inputNumber: number) => {
    // status.insufficientBalance = mintCosts * inputNumber > wallet.ethAmount
}
//监听链的变化
const listeningChainChanged = () => {
    MM.value.on('chainChanged', (metaMaskChainId: string) => {
        // status.isCorrectChain = metaMaskChainId === chainId;
    })
}
//切换链
const switchEthereumChain = () => {
    MM.value.request({
        method: 'wallet_switchEthereumChain',
        params: [{chainId: chainId}],
    }).then(() => {

    }).catch(() => {
        // status.isCorrectChain = false
    });
}
//监听连接断开(通过监听账号的变化判断是否断开连接)
const listeningDisconnect = () => {
    MM.value.on('accountsChanged', (accounts: string[]) => {
        if (accounts.length >= 1) {
            // wallet.address = accounts[0]
            // status.isConnected = true
        } else {
            // wallet.address = ''
            // status.isConnected = false
        }
    })
}
