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

import { processedUploadQueueIdSelector } from '../selectors';
import { clearAllAction, clearUploadQueueAction, uploadVideoCancelledAction } from '../slices';

export default (action$, state$) =>
  action$.pipe(
    ofType(clearUploadQueueAction.type),
    switchMap(() => {
      const processedUploadQueueId = processedUploadQueueIdSelector(state$.value);

      const actions = [of(uploadVideoCancelledAction({ processedUploadQueueId })), of(clearAllAction())];

      return concat(...actions);
    }),
  );
