import { useMemo } from 'react'
import { useLocation } from 'react-router'

import isNumber from 'lodash/isNumber'

export const RESUME_KEY = 'resume'

const readResumeParam = (search: string) => {
  const params = new URLSearchParams(search)
  const rawValue = params.get(RESUME_KEY)
  if (!rawValue) return undefined

  const value = JSON.parse(rawValue)
  return isNumber(value) ? value : 0
}

export const useUrlParam = () => {
  const { search } = useLocation()

  const resume = useMemo(() => readResumeParam(search), [search])

  return resume
}
