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

import { updateChannelAction } from '../slices';
import { gqlRequest } from '@/modules/@common/request';

const queryGQL = `
  mutation UpdateChannel(
    $id: Int!,
    $watchId: Int!,
    $order: Int
  ) {
    updateChannel(
      id: $id,
      watchId: $watchId,
      order: $order
    ) {
      id
    }
  }
`;

export default (action$) =>
  action$.pipe(
    ofType(updateChannelAction.type),
    switchMap(({ payload: { watchId, id, order } }) => {
      const stream$ = gqlRequest({
        query: queryGQL,
        variables: {
          watchId,
          id,
          order,
        },
      }).pipe(switchMap(() => EMPTY));

      return concat(stream$);
    }),
  );
