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

import { modelsByProviders } from '../../helpers';
import { onUpdateFormAction, saveConfigurationDataOnServerAction } from '../slices';

export default (action$) =>
  action$.pipe(
    ofType(onUpdateFormAction.type),
    switchMap(({ payload: { providerName } }) => {
      const providerModelName = modelsByProviders[providerName];

      return concat(of(saveConfigurationDataOnServerAction([providerModelName])));
    }),
  );
