import Safe, { Web3Adapter } from "@safe-global/protocol-kit";
import { CirclesSdk } from "@ingocollatz/sdk";
import Web3 from "web3";
import { Web3AbiEncoder } from "./web3abiEncoder";

/**
 * CirclesSdkWeb3Factory provides factory methods to create instances of the CirclesSdk.
 *
 * This class offers utility methods specifically tailored for use with the Web3.js library.
 * It's designed to simplify the creation of CirclesSdk instances, abstracting away some
 * of the setup and initialization complexities with the Web3.js library.
 */
export class CirclesSdkWeb3Factory {
  /**
   * Creates an instance of the CirclesSdk using an existing Safe and Web3.js.
   *
   * This method initializes the Circles SDK using an existing Safe and the Web3.js library
   * for interactions with Ethereum.
   *
   * @param web3 - An instance of the Web3.js library.
   * @param signerAddress - Ethereum address that will be used for signing transactions
   *                        using the Web3.js library.
   * @param safeAddress - Ethereum address of an existing Safe which will be utilized
   *                      by the Circles SDK.
   *
   * @returns Promise<CirclesSdk> - A promise that resolves to an initialized instance of the CirclesSdk.
   */
  static async withExistingSafe(
    web3: Web3,
    signerAddress: string,
    safeAddress: string
  ): Promise<CirclesSdk> {
    const abiEncoder = new Web3AbiEncoder(web3);

    const safe = await Safe.create({
      ethAdapter: new Web3Adapter({
        web3: web3,
        signerAddress: signerAddress,
      }),
      safeAddress: safeAddress,
    });

    return new CirclesSdk(safe, abiEncoder);
  }
}
