import {useShopActions} from '../../internal/useShopActions'
import {useShopActionsDataFetching} from '../../internal/useShopActionsDataFetching'
import {
  DataHookOptionsBase,
  DataHookReturnsBase,
  UserProfile,
} from '../../types'

export interface UseCurrentUserParams extends DataHookOptionsBase {}

export interface UseCurrentUserReturns extends DataHookReturnsBase {
  /**
   * The current user logged into Shop.
   */
  currentUser: UserProfile | null
}

/**
 * Hook to fetch the current user's profile.
 */
export const useCurrentUser = (
  params?: UseCurrentUserParams
): UseCurrentUserReturns => {
  const {skip, ...shopActionParams} = params || {}
  const {getCurrentUser} = useShopActions()

  const {data, ...rest} = useShopActionsDataFetching(
    getCurrentUser,
    shopActionParams,
    {
      skip,
      hook: 'useCurrentUser',
    }
  )

  return {
    ...rest,
    currentUser: data,
  }
}
