'use strict';

var web3_js = require('@solana/web3.js');
var noModal = require('@web3auth/no-modal');
var react = require('react');
var useWeb3Auth = require('../../hooks/useWeb3Auth.js');

const useSolanaWallet = () => {
  const {
    provider,
    web3Auth
  } = useWeb3Auth.useWeb3Auth();
  const [accounts, setAccounts] = react.useState(null);
  const solanaWallet = react.useMemo(() => {
    if (!provider) return null;
    return new noModal.SolanaWallet(provider);
  }, [provider]);
  const connection = react.useMemo(() => {
    if (!web3Auth || !provider) return null;
    return new web3_js.Connection(web3Auth.currentChain.rpcTarget);
  }, [web3Auth, provider]);
  react.useEffect(() => {
    const init = async () => {
      var _web3Auth$currentChai;
      if (!(web3Auth !== null && web3Auth !== void 0 && (_web3Auth$currentChai = web3Auth.currentChain) !== null && _web3Auth$currentChai !== void 0 && _web3Auth$currentChai.chainNamespace) || web3Auth.currentChain.chainNamespace !== noModal.CHAIN_NAMESPACES.SOLANA) {
        return;
      }
      if (!solanaWallet) return;
      const accounts = await solanaWallet.requestAccounts();
      if ((accounts === null || accounts === void 0 ? void 0 : accounts.length) > 0) {
        setAccounts(accounts);
      }
    };
    if (solanaWallet) init();
  }, [solanaWallet, web3Auth]);
  return {
    solanaWallet,
    accounts,
    connection
  };
};

exports.useSolanaWallet = useSolanaWallet;
