import { QueryStatus } from 'react-query';

import useNetworkStatus from 'src/hooks/useNetworkStatus';
import { Pagination } from 'src/types/services';
import { getPaginatedQueryState } from 'src/utils/pagination';

export enum PaginatedQueryStates {
  SUCCESS = 'success',
  ERROR = 'error',
  NETWORK_ERROR = 'networkError',
  EMPTY_SEARCH = 'emptySearch',
  EMPTY = 'empty',
  IDLE = 'idle',
  LOADING = 'loading',
}

export const usePaginatedQueryState = <TData extends any>(
  data: Pagination<TData> | undefined,
  status: QueryStatus,
  isFiltered: boolean = false,
) => {
  const { isOnline } = useNetworkStatus();

  const state = getPaginatedQueryState(data, status, isOnline, isFiltered);

  return {
    state,
    isLoading: state === PaginatedQueryStates.LOADING,
    isSuccess: state === PaginatedQueryStates.SUCCESS,
    isError: state === PaginatedQueryStates.ERROR,
    isEmpty: state === PaginatedQueryStates.EMPTY,
    isEmptySearch: state === PaginatedQueryStates.EMPTY_SEARCH,
    isNetworkError: state === PaginatedQueryStates.NETWORK_ERROR,
  };
};

export default usePaginatedQueryState;
