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

import * as selectors from '../selectors';
import { downloadCSVAction, getAccountsAction } from '../slices';

export default (action$, state$) =>
  action$.pipe(
    ofType(downloadCSVAction.type),
    filter(() => {
      const state = state$.value;

      const pageConfig = selectors.pageConfigSelector(state);

      return !!pageConfig;
    }),
    switchMap(() => concat(of(getAccountsAction({ allPages: true })))),
  );
