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

import NevisMobileAuthentication

final class PasswordChangerImpl: Sendable {
	// MARK: Properties

	let operationId: String

	// MARK: Initialization

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

// MARK: - PasswordChanger

extension PasswordChangerImpl: PasswordChanger {
	func changePassword(context: PasswordChangeContext, handler: PasswordChangeHandler) {
		Task {
			let operation: PasswordChangeOperation = await OperationCache.shared.read(by: operationId)
			let state = PasswordChangeState(context: context, handler: handler)
			await 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)
	}
}
