import { autoSubcrebe } from './utils'

const tabRoutes = {
  // home: "/pages/index/index",
  // my: "/pages/my/my",
  // chatList: "/pages/chat-list/chat-list",
}

const routes = {
  select: "/pages/select/index",
  example: "/pages/example/index",
  camera: "/pages/camera/index",
  index: "/pages/index/index",
  start: "/pages/start/index",
  work: "/pages/work/index"
}

function sortQuery (query) {
  return Object.keys(query).map(key => `${key}=${query[key]}`).join('&')
}
function toUrl (url, query) {
  if (query) {
    query = sortQuery(query)
  }
  if (tabRoutes[url]) {
    wx.switchTab({
      url: `${tabRoutes[url]}?${query}`,
    })
  } else {
    wx.navigateTo({
      url: `${routes[url]}?${query}`,
    })
  }
}
export default {
  tabRoutes,
  push (options) {
    // autoSubcrebe()
    if (typeof options === 'string') {
      return toUrl(options)
    }
    const { url, query } = options
    return toUrl(url, query)
  },
  redirectTo(options){
    const { url, query } = options
    let q = query
    if (q) {
      q = sortQuery(q)
    }
    return (()=>{
      wx.redirectTo({
        url: `${routes[url]}?${q}`,
      })
    })()
  },
  back () {
    wx.navigateBack()
  }
}