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

import type { DeviceInformationSyncError } from '../../error/deviceInformation/sync/DeviceInformationSyncError';
import { DeviceInformationSyncErrorConverter } from '../../error/deviceInformation/sync/DeviceInformationSyncErrorConverter';

/**
 * This is the result of the {@link DeviceInformationSync} operation.
 *
 * @see {@link DeviceInformationSync}
 */
export abstract class DeviceInformationSyncResult {
	/**
	 * Returns the errors that occurred retrieving the configuration mismatches.
	 *
	 * @return the errors that occurred retrieving the configuration mismatches.
	 */
	abstract errors: Array<DeviceInformationSyncError>;

	/**
	 * Alternate constructor that creates a {@link DeviceInformationSyncResult} from a json.
	 *
	 * @returns the created {@link DeviceInformationSyncResult} instance.
	 */
	static fromJson(json: any): DeviceInformationSyncResult {
		return DeviceInformationSyncResultImpl.fromJson(json);
	}
}

export class DeviceInformationSyncResultImpl extends DeviceInformationSyncResult {
	errors: Array<DeviceInformationSyncError>;

	constructor(errors: Array<DeviceInformationSyncError>) {
		super();
		this.errors = errors;
	}

	static fromJson(json: any): DeviceInformationSyncResultImpl {
		const errorsData = json.errors;
		const errors = errorsData?.map((error: any) =>
			new DeviceInformationSyncErrorConverter(error).convert()
		);
		return new DeviceInformationSyncResultImpl(errors || []);
	}
}
