import { BaseAction } from './base-action';
import { Observable, of } from 'rxjs';
import { IActionResult } from '../interfaces/actions/action-result';
import { IYesNoDialogResult } from '@shared/ui-api/interfaces/yes-no-dialog-result';

export class CallbackAction extends BaseAction {
  callback?: (action: CallbackAction, dialogResult?: IYesNoDialogResult) => Observable<IActionResult>;
  needsResult?: boolean;

  constructor(init: Partial<CallbackAction>) {
    super(init);
    if (init.displayMessages === undefined) {
      this.displayMessages = false;
    }
    this.refreshAfterExecute = init ? !!init.refreshAfterExecute : false;
  }

  executeImpl(dialogResult: IYesNoDialogResult): Observable<IActionResult> {
    if (this.callback) {
      return this.callback(this, dialogResult);
    }

    return of(null);
  }
}
