/**
 * @param {Function} checkSyncFn 检测方法-只能返回 true or false
 * @param {number} heartTime 心跳间隔
 * @param {number} timeout 超时时间, 默认 1000
 */
export function heartCheck(checkSyncFn, heartTime = 500, timeout = 30000) {
  return new Promise((resolve, reject) => {
    if (checkSyncFn()) {
      resolve()
      return
    }
    const startTime = new Date().getTime()
    const intervalId = window.setInterval(() => {
      if (checkSyncFn()) {
        window.clearInterval(intervalId)
        resolve()
      } else if (new Date().getTime() - startTime >= timeout) {
        reject(
          new Error(
            `timeout - ${timeout}: 用时 ${new Date().getTime() - startTime}`,
          ),
        )
      }
    }, heartTime)
  })
}

export function assert(condition, msg) {
  if (!condition) throw new Error(`[bxs-ui-vue] ${msg}`)
}
// 跳转到升级保险师的H5页面
export function updateApp() {
  if (window) {
    window.location.href = 'https://app.winbaoxian.com/view/update-app/'
  }
}
