import { ofType } from 'redux-observable';
import { concat, of } from 'rxjs';
import { filter, switchMap } from 'rxjs/operators';

import { hasUnfinishedJobs } from '../../helpers/helpers/monitoring';
import { monitoringDataAction, monitoringInProgressAction } from '../slices';
import { reloadSelectedVideoAction } from '@/modules/editorial/editorialVideoDetails/redux/slices';

export default (action$) =>
  action$.pipe(
    ofType(monitoringDataAction.type),
    filter((action) => !hasUnfinishedJobs(action.payload)),
    switchMap(() => concat(of(monitoringInProgressAction(false)), of(reloadSelectedVideoAction()))),
  );
