import { CUSTOM_TABBAR_NO_CACHE } from '@/layouts/fg-tabbar/tabbarList'
import { stringifyQuery } from './queryString'

//  uniapp 返回 入参为堆栈
export function goBack(delta = 1) {
  console.log(delta)

  uni.navigateBack({ delta })
}

// uniapp 跳转页面 可携带参数
export function toPage({ url, params, isRedirect = false, isReLaunch = false }: any) {
  let targetUrl = url + (isReLaunch ? '?isReLaunch=1' : '')
  const tabBarPages = ['/pages/index/index', '/pages/income/income', '/pages/mine/mine']

  if (params && Object.keys(params).length > 0) {
    const strParams = stringifyQuery(params)
    targetUrl = `${url}?${strParams + (isReLaunch ? '&isReLaunch=1' : '')}`
  }
  console.log(targetUrl)
  if (tabBarPages.includes(url)) {
    if (CUSTOM_TABBAR_NO_CACHE) {
      uni.navigateTo({ url: targetUrl })
    }
    else {
      uni.switchTab({
        url: targetUrl,
      })
    }
  }
  else {
    if (isRedirect) {
      uni.redirectTo({
        url: targetUrl,
      })
    }
    else if (isReLaunch) {
      uni.reLaunch({
        url: targetUrl,
      })
    }
    else {
      uni.navigateTo({
        url: targetUrl,
      })
    }
  }
}
