import { createSSRApp } from 'vue'
import App from './App.vue'
import router from './router'
import 'uno.css'

if (typeof globalThis.TextEncoder === 'undefined') {
  class MiniTextEncoder {
    encode(input = '') {
      const bytes: number[] = []
      for (const char of input) {
        const codePoint = char.codePointAt(0) ?? 0
        if (codePoint <= 0x7F) {
          bytes.push(codePoint)
          continue
        }
        if (codePoint <= 0x7FF) {
          bytes.push(
            0xC0 | (codePoint >> 6),
            0x80 | (codePoint & 0x3F),
          )
          continue
        }
        if (codePoint <= 0xFFFF) {
          bytes.push(
            0xE0 | (codePoint >> 12),
            0x80 | ((codePoint >> 6) & 0x3F),
            0x80 | (codePoint & 0x3F),
          )
          continue
        }
        bytes.push(
          0xF0 | (codePoint >> 18),
          0x80 | ((codePoint >> 12) & 0x3F),
          0x80 | ((codePoint >> 6) & 0x3F),
          0x80 | (codePoint & 0x3F),
        )
      }
      return new Uint8Array(bytes)
    }
  }

  globalThis.TextEncoder = MiniTextEncoder as typeof TextEncoder
}

const pinia = createPinia()
pinia.use(persistPlugin)
export function createApp() {
  const app = createSSRApp(App)
  app.use(router)
  app.use(pinia)
  return {
    app,
  }
}
