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

import NevisMobileAuthentication

private func executeSelectorBehavior(operationId: String, userInteractionState: UserInteractionState, message: ChannelOutMessage, event: Event) async {
	let operation: any UserInteractionOperation = await OperationCache.shared.read(by: operationId)
	await OperationCache.shared.update(by: operationId, operation: operation.update(state: userInteractionState))
	EventEmitter.shared.dispatch(event: event, message: message)
}

extension AccountSelector {
	func selectAccount(operationId: String, message: ChannelOutMessage, userInteractionState: UserInteractionState) async {
		await executeSelectorBehavior(operationId: operationId, userInteractionState: userInteractionState, message: message, event: .selectAccount)
	}
}

extension AuthenticatorSelector {
	func selectAuthenticator(operationId: String, message: ChannelOutMessage, userInteractionState: UserInteractionState) async {
		await executeSelectorBehavior(operationId: operationId, userInteractionState: userInteractionState, message: message, event: .selectAuthenticator)
	}
}
