import { BlockhashWithExpiryBlockHeight, Connection, Keypair, PublicKey, clusterApiUrl } from "@solana/web3.js";
import { Program } from "@project-serum/anchor";
import * as anchor from "@project-serum/anchor";
import bs58 from "bs58";

import { Auction, IDL } from "../types/auction"
import config from "../config";





export default class ProgramLoader {
    public connection: Connection;
    public program: Program<Auction>;
    public wallet: Keypair;
    public applicationState: PublicKey | null;

    public constructor(programId: PublicKey, cluster: string, applicationState?: PublicKey, ownerKeypair?: Keypair) {
        this.connection = new Connection(cluster);
        anchor.setProvider(new anchor.AnchorProvider(this.connection, new anchor.Wallet(ownerKeypair || Keypair.generate()), {}));
        this.program = new Program<Auction>(IDL, programId);
        this.wallet = ownerKeypair
        if (!applicationState) {
            this.applicationState = null;
        } else this.applicationState = applicationState
    }

    public async getRecentBlockHash(): Promise<BlockhashWithExpiryBlockHeight> {
        return await this.connection.getLatestBlockhash();
    }
}

