declare global {
  interface Window {
    AbsHelper: any;
  }
}

export class ConnectResult {
  public isMainNet: boolean;
  public account: string = "";
  public provider: any = null;

  public walletClient: any = null;
}

export class AbsWrapper {
  result: ConnectResult;
  adapter: any;

  constructor(result: ConnectResult, adapter: any) {
    this.result = result;
    this.adapter = adapter;
  }
}

export class AbsM {
  private static _ins: AbsM;

  public static get ins(): AbsM {
    return AbsM._ins ?? (AbsM._ins = new AbsM());
  }

  public async connectWallet(): Promise<AbsWrapper> {
    //@ts-ignore
    let adapter = await window.AbsHelper.create({
      network: "testnet",
      apiKey: "your-api-key",
    });
    console.log("adapter", adapter);

    const connectResult = await adapter.connect(true);
    console.log("connectHandler", connectResult);

    return new AbsWrapper(connectResult, adapter);
  }

  public async getBalance(w: AbsWrapper): Promise<number> {
    await new Promise((resolve) => setTimeout(resolve, 1000));
    let balance = await window.AbsHelper.getBalance(w.adapter, w.result);
    console.log("balance", balance);
    return balance;
  }

  public disconnectWallet() {
    localStorage.clear();
    window.location.reload();
  }
}
