import type { App } from 'vue'
import { createVuetify } from 'vuetify'
import defaults from './defaults'
import { icons } from './icons'
import { themes } from './theme'

import 'vuetify/styles'

export default function installVuetify(app: App): void {
  const saved = typeof window !== 'undefined' ? localStorage.getItem('ezscale-theme') : null
  let defaultTheme: string

  if (saved === 'light' || saved === 'dark') {
    defaultTheme = saved
  } else {
    const hostname = typeof window !== 'undefined' ? window.location.hostname : ''
    const isMarketing = !hostname.startsWith('account.') && !hostname.startsWith('admin.')
    defaultTheme = isMarketing ? 'light' : 'dark'
  }

  const vuetify = createVuetify({
    defaults,
    icons,
    theme: {
      defaultTheme,
      themes,
    },
  })

  app.use(vuetify)
}
