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

import NevisMobileAuthentication

class PinChangerImpl {
	// MARK: Properties

	let operationId: String
	let policy: PinPolicy?

	// MARK: Initialization

	init(operationId: String, pinPolicy: PinPolicy?) {
		self.operationId = operationId
		self.policy = pinPolicy
	}
}

// MARK: - PinChanger

extension PinChangerImpl: PinChanger {
	func changePin(context: PinChangeContext, handler: PinChangeHandler) {
		let operation: PinChangeOperation = OperationCache.shared.read(by: operationId)
		let state = PinChangeState(context: context, handler: handler)
		OperationCache.shared.update(by: operationId,
		                             operation: operation.update(state: state))

		let message = PinChangerMessage(operationId: operationId,
		                                context: context)
		EventEmitter.shared.dispatch(event: .pinChange, message: message)
	}

	func pinPolicy() -> PinPolicy {
		policy ?? PinPolicyImpl(operationId: operationId)
	}
}
