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

import NevisMobileAuthenticationSdkReact from '../../MobileAuthenticationSdk';
import { OperationIdMessage } from '../../model/messages/out/OperationIdMessage';
import { PasswordsChangeMessage } from '../../model/messages/out/PasswordsChangeMessage';
import { CancellableHandler } from '../CancellableHandler';

/**
 * The object handling the old and new password provided by the end-user.
 *
 * @see {@link PasswordChanger.changePassword}
 */
export abstract class PasswordChangeHandler extends CancellableHandler {
	/**
	 * Specify the old password and the new password.
	 *
	 * To change a password, the SDK requires to provide the old password.
	 * When this method is invoked, the SDK will validate the provided passwords.
	 *
	 * @param oldPassword the old password.
	 * @param newPassword the new password.
	 */
	abstract passwords(oldPassword: string, newPassword: string): Promise<void>;
}

export class PasswordChangeHandlerImpl extends PasswordChangeHandler {
	private readonly _operationId: string;

	constructor(operationId: string) {
		super();
		this._operationId = operationId;
	}

	async passwords(oldPassword: string, newPassword: string): Promise<void> {
		const message = new PasswordsChangeMessage(this._operationId, oldPassword, newPassword);
		return NevisMobileAuthenticationSdkReact.passwordsChange(message);
	}

	cancel(): Promise<void> {
		const message = new OperationIdMessage(this._operationId);
		return NevisMobileAuthenticationSdkReact.cancel(message);
	}
}
