import { UserOperation } from "@biconomy/core-types"
import { IPaymaster, PaymasterAndDataResponse } from "@biconomy/paymaster"
import { resolveProperties } from "@ethersproject/properties"
import { ChainId } from "../types"
import { getPaymasterAndData } from "./core/paymaster"

export class BiconomyPaymaster implements IPaymaster {
  chainId: ChainId

  constructor(chainId: number) {
    this.chainId = chainId
  }

  getDummyPaymasterAndData(_userOp: Partial<UserOperation>): Promise<string> {
    return Promise.resolve(
      "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
    )
  }

  async getPaymasterAndData(
    userOp: Partial<UserOperation>
  ): Promise<PaymasterAndDataResponse> {
    userOp = await resolveProperties(userOp)

    userOp.nonce = Number(userOp.nonce)
    userOp.callGasLimit = Number(userOp.callGasLimit)
    userOp.verificationGasLimit = Number(userOp.verificationGasLimit)
    userOp.maxFeePerGas = Number(userOp.maxFeePerGas)
    userOp.maxPriorityFeePerGas = Number(userOp.maxPriorityFeePerGas)
    userOp.preVerificationGas = Number(userOp.preVerificationGas)
    userOp.signature = "0x"
    userOp.paymasterAndData = "0x"

    const paymasterResponse = await getPaymasterAndData(
      this.chainId,
      userOp as UserOperation
    )

    return {
      paymasterAndData: paymasterResponse.paymasterAndData,
    }
  }
}
