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

import { DeviceInformationCheckClockSkewTooBigError } from './DeviceInformationCheckClockSkewTooBigError';
import { DeviceInformationCheckDeviceProtectionError } from './DeviceInformationCheckDeviceProtectionError';
import { DeviceInformationCheckError } from './DeviceInformationCheckError';
import { DeviceInformationCheckForbidden } from './DeviceInformationCheckForbidden';
import { DeviceInformationCheckNetworkError } from './DeviceInformationCheckNetworkError';
import { DeviceInformationCheckNoDeviceLockError } from './DeviceInformationCheckNoDeviceLockError';
import { DeviceInformationCheckOperationNotSupportedByBackendError } from './DeviceInformationCheckOperationNotSupportedByBackendError';
import { DeviceInformationCheckUnknownError } from './DeviceInformationCheckUnknownError';
import { ErrorConverter } from '../../ErrorConverter';

enum DeviceInformationCheckErrorType {
	ClockSkewTooBig,
	DeviceProtectionError,
	Forbidden,
	NetworkError,
	NoDeviceLockError,
	OperationNotSupportedByBackend,
	Unknown,
}

export class DeviceInformationCheckErrorConverter extends ErrorConverter<DeviceInformationCheckError> {
	convert(): DeviceInformationCheckError {
		const subtype =
			DeviceInformationCheckErrorType[
				this.error.type as keyof typeof DeviceInformationCheckErrorType
			];
		switch (subtype) {
			case DeviceInformationCheckErrorType.ClockSkewTooBig:
				if (this.error.server) {
					return new DeviceInformationCheckClockSkewTooBigError(
						this.error.server,
						this.error.description,
						this.error.cause
					);
				}

				return new DeviceInformationCheckUnknownError(
					this.error.description,
					this.error.cause
				);
			case DeviceInformationCheckErrorType.DeviceProtectionError:
				return new DeviceInformationCheckDeviceProtectionError(
					this.error.description,
					this.error.cause
				);
			case DeviceInformationCheckErrorType.Forbidden:
				if (this.error.server) {
					return new DeviceInformationCheckForbidden(
						this.error.server,
						this.error.description,
						this.error.cause
					);
				}

				return new DeviceInformationCheckUnknownError(
					this.error.description,
					this.error.cause
				);
			case DeviceInformationCheckErrorType.NetworkError:
				if (this.error.server) {
					return new DeviceInformationCheckNetworkError(
						this.error.server,
						this.error.description,
						this.error.cause
					);
				}

				return new DeviceInformationCheckUnknownError(
					this.error.description,
					this.error.cause
				);
			case DeviceInformationCheckErrorType.NoDeviceLockError:
				return new DeviceInformationCheckNoDeviceLockError(
					this.error.description,
					this.error.cause
				);
			case DeviceInformationCheckErrorType.OperationNotSupportedByBackend:
				if (this.error.server) {
					return new DeviceInformationCheckOperationNotSupportedByBackendError(
						this.error.server,
						this.error.description,
						this.error.cause
					);
				}

				return new DeviceInformationCheckUnknownError(
					this.error.description,
					this.error.cause
				);
			case DeviceInformationCheckErrorType.Unknown:
				return new DeviceInformationCheckUnknownError(
					this.error.description,
					this.error.cause,
					this.error.server
				);
		}
	}
}
