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

function build<T>(partial: Record<string, unknown>): UseQueryResult<T> {
  return {
    failureCount: 0,
    failureReason: null,
    errorUpdateCount: 0,
    isFetched: true,
    isFetchedAfterMount: true,
    isFetching: partial.fetchStatus === 'fetching',
    isPaused: false,
    isPlaceholderData: false,
    isRefetching: false,
    isStale: false,
    isInitialLoading: partial.isLoading === true,
    isLoadingError: false,
    isRefetchError: false,
    dataUpdatedAt: 0,
    errorUpdatedAt: 0,
    refetch: () => Promise.resolve(undefined as never),
    promise: Promise.resolve(undefined as never),
    ...partial,
  } as unknown as UseQueryResult<T>
}

export const stubQuery = {
  loading<T>(): UseQueryResult<T> {
    return build<T>({
      status: 'pending',
      fetchStatus: 'fetching',
      isPending: true,
      isLoading: true,
      isError: false,
      isSuccess: false,
      data: undefined,
      error: null,
    })
  },
  error<T>(error: Error): UseQueryResult<T> {
    return build<T>({
      status: 'error',
      fetchStatus: 'idle',
      isPending: false,
      isLoading: false,
      isError: true,
      isSuccess: false,
      data: undefined,
      error,
    })
  },
  success<T>(data: T): UseQueryResult<T> {
    return build<T>({
      status: 'success',
      fetchStatus: 'idle',
      isPending: false,
      isLoading: false,
      isError: false,
      isSuccess: true,
      data,
      error: null,
    })
  },
  empty<T>(): UseQueryResult<T> {
    return stubQuery.success<T>(null as T)
  },
}
