//
// Copyright © 2024 Nevis Security AG. All rights reserved.
//

import NevisMobileAuthentication

class PasswordEnrollerImpl {
	// MARK: Properties

	let operationId: String

	// MARK: Initialization

	init(operationId: String) {
		self.operationId = operationId
	}
}

// MARK: - PasswordEnroller

extension PasswordEnrollerImpl: PasswordEnroller {
	func enrollPassword(context: PasswordEnrollmentContext, handler: PasswordEnrollmentHandler) {
		let operation: UserInteractionOperation = OperationCache.shared.read(by: operationId)
		let authenticator = operation.findAuthenticator(aaid: AuthenticatorAaid.Password)
		let state = PasswordEnrollState(context: context,
		                                handler: handler,
		                                authenticator: authenticator)
		OperationCache.shared.update(by: operationId,
		                             operation: operation.update(state: state))

		let message = PasswordEnrollerMessage(operationId: operationId,
		                                      context: context)
		EventEmitter.shared.dispatch(event: .passwordEnroll, message: message)
	}

	func passwordPolicy() -> PasswordPolicy {
		PasswordPolicyImpl(operationId: operationId)
	}
}
