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

import NevisMobileAuthentication

class PasswordChangerImpl {
	// MARK: Properties

	let operationId: String

	// MARK: Initialization

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

// MARK: - PasswordChanger

extension PasswordChangerImpl: PasswordChanger {
	func changePassword(context: PasswordChangeContext, handler: PasswordChangeHandler) {
		let operation: PasswordChangeOperation = OperationCache.shared.read(by: operationId)
		let state = PasswordChangeState(context: context, handler: handler)
		OperationCache.shared.update(by: operationId,
		                             operation: operation.update(state: state))

		let message = PasswordChangerMessage(operationId: operationId,
		                                     context: context)
		EventEmitter.shared.dispatch(event: .passwordChange, message: message)
	}

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