import { AuthService } from '../auth'
import { ICredential, CredentialType } from './credential'
import { RefreshableJwt, UserPassword } from '..'

export class PasswordCredentials implements ICredential {
	private jwt?: RefreshableJwt
	readonly type: CredentialType.Password

	constructor(private credentials?: UserPassword) {}

	getToken(): RefreshableJwt {
		return this.jwt
	}

	async authorize(
		authService: AuthService,
		organizer?: string
	): Promise<RefreshableJwt> {
		if (organizer) {
			this.credentials.organizerId = organizer
		}
		this.jwt = await authService.login(this.credentials)
		return this.jwt
	}

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