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

import { dataSelector } from '../selectors';
import { dataAction, endAction } from '../slices';

const monitoringEpic = (action$, state$) =>
  action$.pipe(
    ofType(endAction.type),
    delay(10000),
    mergeMap(({ payload }) => {
      const monitoringData = dataSelector(state$.value);

      const videoDataCollection = { ...monitoringData };

      delete videoDataCollection[payload];

      return concat(of(dataAction(videoDataCollection)));
    }),
  );

export default monitoringEpic;
