import {
useQueryClient,
type UseMutationResult,
type MutateOptions,
type UseInfiniteQueryResult,
type InfiniteData,
UseMutationOptions,
} from '@tanstack/react-query';
import { useDataProvider } from './useDataProvider';
import type {
RaRecord,
UpdateParams,
MutationMode,
GetListResult as OriginalGetListResult,
GetInfiniteListResult,
DataProvider,
UpdateResult,
} from '../types';
import { useMutationWithMutationMode } from './useMutationWithMutationMode';
import { useEvent } from '../util';
/**
* Get a callback to call the dataProvider.update() method, the result and the loading state.
*
* @param {string} resource
* @param {Params} params The update parameters { id, data, previousData, meta }
* @param {Object} options Options object to pass to the queryClient.
* May include side effects to be executed upon success or failure, e.g. { onSuccess: () => { refresh(); } }
* May include a mutation mode (optimistic/pessimistic/undoable), e.g. { mutationMode: 'undoable' }
*
* @typedef Params
* @prop params.id The resource identifier, e.g. 123
* @prop params.data The updates to merge into the record, e.g. { views: 10 }
* @prop params.previousData The record before the update is applied
* @prop params.meta Optional meta data
*
* @returns The current mutation state. Destructure as [update, { data, error, isPending }].
*
* The return value updates according to the request state:
*
* - initial: [update, { isPending: false, isIdle: true }]
* - start: [update, { isPending: true }]
* - success: [update, { data: [data from response], isPending: false, isSuccess: true }]
* - error: [update, { error: [error from response], isPending: false, isError: true }]
*
* The update() function must be called with a resource and a parameter object: update(resource, { id, data, previousData }, options)
*
* This hook uses react-query useMutation under the hood.
* This means the state object contains mutate, isIdle, reset and other react-query methods.
*
* @see https://react-query-v3.tanstack.com/reference/useMutation
*
* @example // set params when calling the update callback
*
* import { useUpdate, useRecordContext } from 'react-admin';
*
* const IncreaseLikeButton = () => {
* const record = useRecordContext();
* const diff = { likes: record.likes + 1 };
* const [update, { isPending, error }] = useUpdate();
* const handleClick = () => {
* update('likes', { id: record.id, data: diff, previousData: record })
* }
* if (error) { return