import { EtherspotBundler, ModularSdk, MODULE_TYPE, sleep } from '@etherspot/modular-sdk';
import * as dotenv from 'dotenv';

async function main() {
  const bundlerApiKey = process.env.API_KEY;

  // initializating sdk...
  const modularSdk = new ModularSdk({ privateKey: process.env.WALLET_PRIVATE_KEY }, 
    { chainId: Number(process.env.CHAIN_ID), 
      bundlerProvider: new EtherspotBundler(Number(process.env.CHAIN_ID), bundlerApiKey) })

  console.log('address: ', modularSdk.state.EOAAddress);

  // get address of EtherspotWallet
  const address: string = await modularSdk.getCounterFactualAddress();

  console.log('\x1b[33m%s\x1b[0m', `EtherspotWallet address: ${address}`);

  //this should be previous node address of the module to be uninstalled and the deinit data
  //deinit data is the data that is passed to the module to be uninstalled
  // here we need to call the function which can find out the address of previous node of the module to be uninstalled
  // and the deinit data can be 0x00 as default value
  const deInitDataDefault = '0x00';

  // set the module address
  const moduleAddress = '';

  //generate deinit data...
  const deInitData = await modularSdk.generateModuleDeInitData(MODULE_TYPE.VALIDATOR, moduleAddress, deInitDataDefault);

  console.log(`deinitData: ${deInitData}`);

  const uoHash = await modularSdk.uninstallModule(MODULE_TYPE.VALIDATOR, moduleAddress, deInitData);
  console.log(`UserOpHash: ${uoHash}`);

  // get transaction hash...
  console.log('Waiting for transaction...');
  let userOpsReceipt = null;
  const timeout = Date.now() + 60000; // 1 minute timeout
  while ((userOpsReceipt == null) && (Date.now() < timeout)) {
    await sleep(2);
    userOpsReceipt = await modularSdk.getUserOpReceipt(uoHash);
  }
  console.log('\x1b[33m%s\x1b[0m', `Transaction Receipt: `, userOpsReceipt);
}