/**
 * Copyright © 2023-2024 Nevis Security AG. All rights reserved.
 */

import { PlatformOperation } from './PlatformOperation';
import {
	OutOfBandAuthentication,
	OutOfBandAuthenticationImpl,
} from '../../operations/outOfBand/OutOfBandAuthentication';
import {
	OutOfBandRegistration,
	OutOfBandRegistrationImpl,
} from '../../operations/outOfBand/OutOfBandRegistration';

/**
 * Represents the eligible types of an out-of-band operation.
 */
export enum OutOfBandPlatformOperationType {
	/**
	 * Out-of-band registration.
	 */
	registration = 'REGISTRATION',
	/**
	 * Out-of-band authentication.
	 */
	authentication = 'AUTHENTICATION',
}

/**
 * Helps in following the states of out-of-band operations during method channel calls.
 */
export class OutOfBandPlatformOperation extends PlatformOperation {
	operationId: string;
	subOperationId: string;

	/**
	 * The callback that will be invoked by the SDK with the {@link OutOfBandRegistration} object.
	 */
	onRegistration?: (registration: OutOfBandRegistration) => void;

	/**
	 * The callback that will be invoked by the SDK with the {@link OutOfBandAuthentication} object.
	 */
	onAuthentication?: (authentication: OutOfBandAuthentication) => void;

	constructor(
		operationId: string,
		subOperationId: string,
		onRegistration?: (registration: OutOfBandRegistration) => void,
		onAuthentication?: (authentication: OutOfBandAuthentication) => void
	) {
		super();
		this.operationId = operationId;
		this.subOperationId = subOperationId;
		this.onRegistration = onRegistration;
		this.onAuthentication = onAuthentication;
	}

	/**
	 * Provides a way to continue the out-of-band operation when the type of the operation turned out.
	 *
	 * @param type the operation type.
	 */
	selectOperation(type: OutOfBandPlatformOperationType) {
		switch (type) {
			case OutOfBandPlatformOperationType.registration: {
				const registration = new OutOfBandRegistrationImpl(this.subOperationId);
				this.onRegistration?.(registration);
				break;
			}
			case OutOfBandPlatformOperationType.authentication: {
				const authentication = new OutOfBandAuthenticationImpl(this.subOperationId);
				this.onAuthentication?.(authentication);
				break;
			}
		}
	}
}
