import App from './App'

// #ifndef VUE3
import Vue from 'vue'
import './uni.promisify.adaptor'
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
  ...App
})
app.$mount()
// #endif

// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp() {
  const app = createSSRApp(App)
  return {
    app
  }
}
// #endif

// 规范化 WebSocket 关闭码（仅微信小程序）
// #ifdef MP-WEIXIN
if (typeof uni !== 'undefined' && typeof uni.connectSocket === 'function') {
  const _connectSocket = uni.connectSocket
  uni.connectSocket = function(options) {
    const task = _connectSocket.call(this, options)
    if (task && typeof task.close === 'function') {
      const _close = task.close
      task.close = function(params = {}) {
        if (params && typeof params === 'object') {
          const codeNum = Number(params.code)
          const isValid = codeNum === 1000 || (codeNum >= 3000 && codeNum <= 4999)
          if (!isValid) {
            params.code = 1000
            if (!params.reason) params.reason = 'normalized from invalid close code'
          }
        }
        return _close.call(this, params)
      }
    }
    return task
  }
}
// #endif