import type { PageMetaDatum, SubPackages } from '@uni-helper/vite-plugin-uni-pages'
import { isMpWeixin } from '@uni-helper/uni-env'
import { IMAGE_BASE_CONFIG } from '@/configs/index'
import { pages, subPackages } from '@/pages.json'

const { VITE_APP_IMAGE_URL } = import.meta.env

export type PageInstance = Page.PageInstance<AnyObject, object> & { $page: Page.PageInstance<AnyObject, object> & { fullPath: string } }

export function getLastPage() {
  // getCurrentPages() 至少有1个元素，所以不再额外判断
  // const lastPage = getCurrentPages().at(-1)
  // 上面那个在低版本安卓中打包会报错，所以改用下面这个【虽然我加了 src/interceptions/prototype.ts，但依然报错】
  const pages = getCurrentPages()
  return pages[pages.length - 1] as PageInstance
}

/**
 * 获取当前页面路由的 path 路径和 redirectPath 路径
 * path 如 '/pages/login/login'
 * redirectPath 如 '/pages/demo/base/route-interceptor'
 */
export function currRoute() {
  const lastPage = getLastPage() as PageInstance
  if (!lastPage) {
    return {
      path: '',
      query: {},
    }
  }
  const currRoute = lastPage.$page
  // console.log('lastPage.$page:', currRoute)
  // console.log('lastPage.$page.fullpath:', currRoute.fullPath)
  // console.log('lastPage.$page.options:', currRoute.options)
  // console.log('lastPage.options:', (lastPage as any).options)
  // 经过多端测试，只有 fullPath 靠谱，其他都不靠谱
  const { fullPath } = currRoute
  // console.log(fullPath)
  // eg: /pages/login/login?redirect=%2Fpages%2Fdemo%2Fbase%2Froute-interceptor (小程序)
  // eg: /pages/login/login?redirect=%2Fpages%2Froute-interceptor%2Findex%3Fname%3Dfeige%26age%3D30(h5)
  return parseUrlToObj(fullPath)
}

export function ensureDecodeURIComponent(url: string) {
  if (url.startsWith('%')) {
    return ensureDecodeURIComponent(decodeURIComponent(url))
  }
  return url
}
/**
 * 解析 url 得到 path 和 query
 * 比如输入url: /pages/login/login?redirect=%2Fpages%2Fdemo%2Fbase%2Froute-interceptor
 * 输出: {path: /pages/login/login, query: {redirect: /pages/demo/base/route-interceptor}}
 */
export function parseUrlToObj(url: string) {
  const [path, queryStr] = url.split('?')
  // console.log(path, queryStr)

  if (!queryStr) {
    return {
      path,
      query: {},
    }
  }
  const query: Record<string, string> = {}
  queryStr.split('&').forEach((item) => {
    const [key, value] = item.split('=')
    // console.log(key, value)
    query[key] = ensureDecodeURIComponent(value) // 这里需要统一 decodeURIComponent 一下，可以兼容h5和微信y
  })
  return { path, query }
}
/**
 * 得到所有的需要登录的 pages，包括主包和分包的
 * 这里设计得通用一点，可以传递 key 作为判断依据，默认是 excludeLoginPath, 与 route-block 配对使用
 * 如果没有传 key，则表示所有的 pages，如果传递了 key, 则表示通过 key 过滤
 */
export function getAllPages(key?: string) {
  // 这里处理主包
  const mainPages = (pages as PageMetaDatum[])
    .filter(page => !key || page[key])
    .map(page => ({
      ...page,
      path: `/${page.path}`,
    }))

  // 这里处理分包
  const subPages: PageMetaDatum[] = []
  ;(subPackages as SubPackages).forEach((subPageObj) => {
    // console.log(subPageObj)
    const { root } = subPageObj

    subPageObj.pages
      .filter(page => !key || page[key])
      .forEach((page) => {
        subPages.push({
          ...page,
          path: `/${root}/${page.path}`,
        })
      })
  })
  const result = [...mainPages, ...subPages]
  // console.log(`getAllPages by ${key} result: `, result)
  return result
}

export function getCurrentPageI18nKey() {
  const routeObj = currRoute()
  const currPage = (pages as PageMetaDatum[]).find(page => `/${page.path}` === routeObj.path)
  if (!currPage) {
    console.warn('路由不正确')
    return ''
  }
  console.log(currPage)
  console.log(currPage.style.navigationBarTitleText)
  return currPage.style?.navigationBarTitleText || ''
}

/**
 * 根据微信小程序当前环境，判断应该获取的 baseUrl
 */
export function getEnvBaseUrl() {
  // 请求基准地址
  let baseUrl = import.meta.env.VITE_SERVER_BASEURL

  // # 有些同学可能需要在微信小程序里面根据 develop、trial、release 分别设置上传地址，参考代码如下。
  const VITE_SERVER_BASEURL__WEIXIN_DEVELOP = baseUrl
  const VITE_SERVER_BASEURL__WEIXIN_TRIAL = baseUrl
  const VITE_SERVER_BASEURL__WEIXIN_RELEASE = baseUrl

  // 微信小程序端环境区分
  if (isMpWeixin) {
    const {
      miniProgram: { envVersion },
    } = uni.getAccountInfoSync()

    switch (envVersion) {
      case 'develop':
        baseUrl = VITE_SERVER_BASEURL__WEIXIN_DEVELOP || baseUrl
        break
      case 'trial':
        baseUrl = VITE_SERVER_BASEURL__WEIXIN_TRIAL || baseUrl
        break
      case 'release':
        baseUrl = VITE_SERVER_BASEURL__WEIXIN_RELEASE || baseUrl
        break
    }
  }

  return baseUrl
}

/**
 * 是否是双token模式
 */
export const isDoubleTokenMode = import.meta.env.VITE_AUTH_MODE === 'double'

/**
 * 首页路径，通过 page.json 里面的 type 为 home 的页面获取，如果没有，则默认是第一个页面
 * 通常为 /pages/index/index
 */
// export const HOME_PAGE = `/${(pages as PageMetaDatum[]).find(page => page.type === 'home')?.path || (pages as PageMetaDatum[])[0].path}`
export const HOME_PAGE = `/pages/index/index`
/**
 *  获取 url 的参数
 */
export function getUrlQuery(url: string) {
  const queryStr = url.split('?')[1]
  if (!queryStr) {
    return {}
  }
  const query: Record<string, string> = {}
  queryStr.split('&').forEach((item) => {
    const [key, value] = item.split('=')
    query[key] = value
  })
  return query
}

/**
 * 字典翻译
 * @param {Array} list - 字典列表
 * @param {string} key - 字典键
 * @param {string|number|boolean} value - 字典值
 * @param {string} valueKey - 需要返回的字典值对应的键，默认值为 "dicValueName"
 * @returns {any} 值
 */
export function dictTranslate(list: any[], key: string, value: string | number, valueKey: string = 'dicValueName'): any {
  if (value === undefined || value === null)
    return value

  if (!Array.isArray(list) || list.length === 0)
    return value
  const item = list.find(i => i[key] === value.toString())
  return item ? item[valueKey] : value
}

// 获取图片完整路径
export function getImageFullPath(imageFileName: string) {
  if (!imageFileName) {
    return ''
  }
  return `${VITE_APP_IMAGE_URL}${IMAGE_BASE_CONFIG[imageFileName]}`
}

/**
 * 深拷贝
 * @param { string } data 需要深拷贝的数据
 * @returns 深拷贝的数据
 */
export function deepClone(data: any) {
  const stack = []
  let cloned
  if (Array.isArray(data)) {
    cloned = []
  }
  else if (typeof data === 'object' && data !== null) {
    cloned = {}
  }
  else {
    return data
  }
  stack.push({
    original: data,
    copy: cloned,
  })
  while (stack.length > 0) {
    const current: any = stack.pop()
    const original = current.original
    const copy = current.copy

    for (const key in original) {
      if (Object.prototype.hasOwnProperty.call(original, key)) {
        const value = original[key]

        if (typeof value === 'object' && value !== null) {
          copy[key] = Array.isArray(value) ? [] : {}

          stack.push({
            original: value,
            copy: copy[key],
          })
        }
        else {
          copy[key] = value
        }
      }
    }
  }
  return cloned
}

/**
 * 手机号隐私化处理（显示前3位 + 后4位，中间用*隐藏）
 * @param phone 手机号码（字符串或数字）
 * @returns 隐私化后的字符串，如 '189****0097'
 */
export function maskPhone(phone: string): string {
  const phoneNumber = String(phone).trim()

  // 校验是否为有效手机号（长度为11位的纯数字）
  if (!/^\d{11}$/.test(phoneNumber)) {
    return phone
  }

  // 只保留前3位和后4位，中间用 * 替代
  return `${phoneNumber.slice(0, 3)}****${phoneNumber.slice(-4)}`
}
