import { Injectable } from '@angular/core';
import { Effect, Actions, ofType } from '@ngrx/effects';
import { of } from 'rxjs';
import { catchError, exhaustMap, map, tap } from 'rxjs/operators';

import { MetaNoAuthProbeStatus } from '@overflow/commons-typescript/model/meta';

import { MetaNoAuthProbeStatusService } from '../../../service/meta-noauth-probe-status.service';

import {
  ReadAll,
  ReadAllSuccess,
  ReadAllFailure,
  ActionType,
} from './meta-noauth-probe-status.action';

@Injectable()
export class Effects {

  constructor(
    private actions$: Actions,
    private metaNoAuthProbeStatusService: MetaNoAuthProbeStatusService,
  ) { }

  @Effect()
  readAll$ = this.actions$.pipe(
    ofType(ActionType.ReadAll),
    exhaustMap(() =>
      this.metaNoAuthProbeStatusService
        .readAll()
        .pipe(
          map((result: MetaNoAuthProbeStatus[]) => {
            return new ReadAllSuccess(result);
          }),
          catchError(error => of(new ReadAllFailure(error)))
        )
    )
  );
}
