import { ofType } from 'redux-observable';
import { filter, map } from 'rxjs/operators';

import { startAction } from '@/modules/@common/monitoring/redux/slices';
import { selectedVideoSelector } from '@/modules/editorial/editorialSearchResults/redux/selectors';
import { monitoringJobAction } from '@/modules/editorial/editorialVideoDetails/redux/slices';

export default (action$, state$) =>
  action$.pipe(
    ofType(monitoringJobAction.type),
    filter(() => selectedVideoSelector(state$.value)?.uid),
    map(() => {
      const selectedVideo = selectedVideoSelector(state$.value);

      return startAction(selectedVideo.uid);
    }),
  );
