import type { UseMutationResult } from '@tanstack/react-query'

function build<TData, TVariables>(
  partial: Record<string, unknown>,
): UseMutationResult<TData, Error, TVariables> {
  return {
    failureCount: 0,
    failureReason: null,
    isPaused: false,
    submittedAt: 0,
    context: undefined,
    variables: undefined,
    mutate: () => undefined,
    mutateAsync: () => Promise.resolve(undefined as never),
    reset: () => undefined,
    ...partial,
  } as unknown as UseMutationResult<TData, Error, TVariables>
}

export const stubMutation = {
  idle<TData = unknown, TVariables = unknown>(): UseMutationResult<TData, Error, TVariables> {
    return build<TData, TVariables>({
      status: 'idle',
      isIdle: true,
      isPending: false,
      isError: false,
      isSuccess: false,
      data: undefined,
      error: null,
    })
  },
  pending<TData = unknown, TVariables = unknown>(): UseMutationResult<TData, Error, TVariables> {
    return build<TData, TVariables>({
      status: 'pending',
      isIdle: false,
      isPending: true,
      isError: false,
      isSuccess: false,
      data: undefined,
      error: null,
    })
  },
  error<TData = unknown, TVariables = unknown>(
    error: Error,
  ): UseMutationResult<TData, Error, TVariables> {
    return build<TData, TVariables>({
      status: 'error',
      isIdle: false,
      isPending: false,
      isError: true,
      isSuccess: false,
      data: undefined,
      error,
    })
  },
  success<TData = unknown, TVariables = unknown>(
    data: TData,
  ): UseMutationResult<TData, Error, TVariables> {
    return build<TData, TVariables>({
      status: 'success',
      isIdle: false,
      isPending: false,
      isError: false,
      isSuccess: true,
      data,
      error: null,
    })
  },
}
