import qs from 'query-string'
export type queryResult = {[key: string]: string | object | null | string[]}
// 单元测试 | 自主练习 | 评测 | 课后练习
type IFeature = 'UNIT_TEST' | 'PRACTICE' | 'EVALUATION' | 'LESSON_TRAINING'
const getFeature = (search: any): IFeature => {
  if (search.feature){
    return search.feature
  }
  if (search.lessonTrainingId){
    return 'LESSON_TRAINING'
  }
  return 'PRACTICE'
}
export const useOriginSearch = (): queryResult => {
  const originSearchString = location.search || location.hash.replaceAll('#/', '')
  const parseSearch = qs.parse(originSearchString)
  if (parseSearch.Authorization) {
    sessionStorage.setItem('Authorization', (parseSearch.Authorization as string) || '')
  }
  parseSearch.feature = getFeature(parseSearch)
  return parseSearch
}

export const useReload = () => {
  const search = useOriginSearch()
  const newSearchStr = qs.stringify({
    ...search,
    _t: Date.now(),
  }, {})
  location.search = '?' + newSearchStr
}
