'use strict';

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

const useSolanaWallet = () => {
  const {
    provider,
    web3Auth
  } = useWeb3Auth.useWeb3Auth();
  const accounts = vue.ref([]);
  const solanaWallet = vue.shallowRef(null);
  const connection = vue.shallowRef(null);
  const setupWallet = async () => {
    var _web3Auth$value, _web3Auth$value2;
    if (!((_web3Auth$value = web3Auth.value) !== null && _web3Auth$value !== void 0 && (_web3Auth$value = _web3Auth$value.currentChain) !== null && _web3Auth$value !== void 0 && _web3Auth$value.chainNamespace) || web3Auth.value.currentChain.chainNamespace !== noModal.CHAIN_NAMESPACES.SOLANA) {
      return;
    }
    solanaWallet.value = new noModal.SolanaWallet(provider.value);
    const result = await solanaWallet.value.requestAccounts();
    if ((result === null || result === void 0 ? void 0 : result.length) > 0) {
      accounts.value = result;
    }
    connection.value = new web3_js.Connection((_web3Auth$value2 = web3Auth.value) === null || _web3Auth$value2 === void 0 || (_web3Auth$value2 = _web3Auth$value2.currentChain) === null || _web3Auth$value2 === void 0 ? void 0 : _web3Auth$value2.rpcTarget);
  };
  if (provider.value && !solanaWallet.value) {
    setupWallet();
  }
  vue.watch(provider, async newVal => {
    if (!newVal && solanaWallet.value) {
      solanaWallet.value = null;
      accounts.value = null;
      connection.value = null;
      return;
    }
    if (newVal && !solanaWallet.value) {
      setupWallet();
    }
  }, {
    immediate: true
  });
  return {
    solanaWallet,
    accounts,
    connection
  };
};

exports.useSolanaWallet = useSolanaWallet;
