import { KeyPair } from '..'
import { Jwt } from '../auth/types'
import { AuthService } from '../auth'
import { ICredential, CredentialType } from './credential'

export class WalletCredentials implements ICredential {
	readonly type: CredentialType.Wallet
	private jwt?: Jwt
	private organizer?: string

	constructor(private keyPair: KeyPair) {}

	getToken(): Jwt {
		return this.jwt
	}

	async authorize(authService: AuthService, organizer?: string): Promise<Jwt> {
		if (organizer) {
			this.organizer = organizer
		}
		this.jwt = await authService.loginWithCertificate(
			this.keyPair,
			this.organizer
		)
		return this.jwt
	}

	refreshToken(authService: AuthService, organizer?: string): Promise<Jwt> {
		return this.authorize(authService, organizer)
	}
}
