import { isReactive, isRef, onMounted } from 'vue'
import { RouterView, useRoute, useRouter } from 'vue-router'
import { Searchs } from '@/utils/searchs'

function setLoading(loading: any, val: any) {
  if (loading != undefined && isRef(loading)) {
    loading.value = val
  } else if (loading != undefined && isReactive(loading)) {
    loading.loading = val
  }
}

export const useAsync = async (func: Promise<any>, loading: any): Promise<any> => {
  setLoading(loading, true)

  return await func.finally(() => setLoading(loading, false))
}

export const getTabsCache = (callBack: Function) => {
  const route = useRoute()
  const searchs = new Searchs(route.path)

  const active = searchs.get(route.path)
  onMounted(() => {
    callBack(active)
  })
}

export const setTabsCaches = (current: any, key = 'current', routes: any) => {
  const searchs = new Searchs(routes.path)
  searchs.update({ [key]: current }, undefined, 'form')
  const active = searchs.get(routes.path)
  console.log(active, 'setTabsCaches')
}
