import { keyViewMap, attach } from '@/constant/view'
import { ref, watch } from 'vue'
import router from '@/router'

const auth = ref({})


const setCurrentAuth = () => {
  let viewName = router.currentRoute.value.name
  
  viewName = attach[viewName] ? attach[viewName] : viewName

  let key = Object.keys(keyViewMap).find(key => keyViewMap[key] === viewName) || viewName
  let authParent = [].find(item => item.resourceKey === key)


  if (authParent) {
    let viewAuths = authParent.children.map(item => item.resourceKey.substr(key.length + 1))
    let newValue = {}
    viewAuths.forEach(key => newValue[key] = true)
    auth.value = newValue
  } else {
    auth.value = {}
  }
}

setTimeout(() => {
  watch(
    router.currentRoute, 
    setCurrentAuth, 
    { immediate: true }
  )
})


export default auth