import { WalletServicesPluginError } from '@web3auth/no-modal';
import { ref } from 'vue';
import { useWalletServicesPlugin } from './useWalletServicesPlugin.js';

const useFunding = () => {
  const {
    plugin,
    ready
  } = useWalletServicesPlugin();
  const loading = ref(false);
  const error = ref(null);
  const showFunding = async showFundingParams => {
    loading.value = true;
    error.value = null;
    try {
      if (!plugin) throw WalletServicesPluginError.notInitialized();
      if (!ready) throw WalletServicesPluginError.walletPluginNotConnected();
      await plugin.value.showFunding(showFundingParams);
    } catch (err) {
      error.value = err;
    } finally {
      loading.value = false;
    }
  };
  return {
    loading,
    error,
    showFunding
  };
};

export { useFunding };
