import { useMutation, useQueryClient } from 'react-query';

import { updateLibraryNotify } from 'src/services/libraryService';
import {
  KnowledgeLibraryArticle,
  LibraryNotificationType,
} from 'src/types/library';
import { RequestError, RequestSuccess } from 'src/types/services';

import { librariesKeys } from '../queries';

export type SuccessResponse = RequestSuccess<typeof updateLibraryNotify>;
export type ErrorResponse = RequestError;
export type Values = {
  notify: boolean;
  notificationType: LibraryNotificationType;
};

export const useUpdateLibraryNotify = (library: KnowledgeLibraryArticle) => {
  const queryClient = useQueryClient();

  return useMutation<SuccessResponse, ErrorResponse, Values>(
    ({ notify, notificationType }) =>
      updateLibraryNotify(library.id, notify, notificationType),
    {
      onSuccess: () => {
        queryClient.invalidateQueries(librariesKeys.library(library.id));
      },
    },
  );
};

export default useUpdateLibraryNotify;
