import { router } from '@/router'
import Message from '@arco-design/web-vue/es/message'
/**
 * * 新开页面
 * @param url
 */
export const openNewWindow = (url: string) => {
  return window.open(url, '_blank')
}

/**
 * * 根据路径跳转路由
 * @param path
 * @param query
 * @param isReplace
 * @param windowOpen
 */
export const routerTurnByPath = (
  path: string,
  query?: Array<string | number>,
  isReplace?: boolean,
  windowOpen?: boolean
) => {
  let fullPath = ''
  if (query?.length) {
    fullPath = `${path}/${query.join('/')}`
  }
  if (windowOpen) {
    return openNewWindow(fullPath)
  }

  if (isReplace) {
    router.replace({
      path: fullPath
    })
    return null
  }
  router.push({
    path: fullPath
  })
  return null
}

/**
 * * 根据名称获取路由信息
 * @param pageName
 * @param pageName
 */
export const fetchPathByName = (pageName: string, p?: string) => {
  try {
    const pathData = router.resolve({
      name: pageName
    })
    return p ? (pathData as any)[p] : pathData
  } catch (error) {
    Message.warning('查询路由信息失败，请联系管理员！')
  }
  return null
}
