import { usePermissions } from '../../composables/permissions'

export const back = () => {
  uni.navigateBack()
}
export const useRouter = () => {
  const { routes, isLogined, isDesigner } = usePermissions()
  const push = async (url: string, switchTab = false) => {
    const path = url.split('?')[0]
    const route = routes.find((it) => it.path === path)
    if (route && !route.meta.canNotLogin && !isLogined.value) {
      if (route.meta.showToast) {
        uni.showToast({ title: '暂无权限', icon: 'none' })
      }
      if (route.meta.toLogin) {
        push('/pages/login/index')
      }
      return false
    }
    if (route && !route.meta.canNotDesigner && !isDesigner.value) {
      return push('/pages/mine/authentication/index')
    }
    if (switchTab) {
      uni.switchTab({ url })
    } else {
      uni.navigateTo({ url })
    }
  }
  const replace = async (path: string) => {
    uni.redirectTo({ url: path })
  }
  const back = (delta = 1) => {
    uni.navigateBack({ delta }).then()
  }
  return { push, replace, back }
}
