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

import NevisMobileAuthentication

struct LocalDeleteAuthenticatorMethodHandler: MethodHandler {
	func execute(using client: MobileAuthenticationClient, method: ReactMethod, with message: ChannelInMessage) {
		let message: LocalDeleteAuthenticatorMessage = validate(message: message)

		Task {
			do {
				// This method must not be called on DispatchQueue.main thread.
				try client.localData.deleteAuthenticator(username: message.username,
				                                         aaid: message.aaid)
				MethodChannelHandler.shared.resolve(method: method)
			}
			catch {
				let message = ErrorMessage(operationId: message.operationId, error: error)
				MethodChannelHandler.shared.reject(method: method, message: message)
			}
		}
	}
}
