import OurVue from 'vue'
import Vuetify from 'vuetify'
import { install } from './install' // Services
import * as components from './components'
import * as directives from './directives'
import * as filters from './filters'
import * as services from './services'

export default class Shipnity {
  constructor (userPreset = {}) {
    this.framework = {
      isHydrating: false
    }
    this.installed = []
    this.preset = {}
    this.userPreset = {}
    this.userPreset = userPreset
    this.use(services.Presets)
    this.use(services.Application)
    this.use(services.Breakpoint)
    this.use(services.Goto)
    this.use(services.Icons)
    this.use(services.Lang)
    this.use(services.Theme)
    this.use(services.Notification)
  }
  // Called on the new vuetify instance
  // bootstrap in install beforeCreate
  // Exposes ssrContext if available

  init (root, ssrContext) {
    if (!root.$vuetify) {
      OurVue.use(Vuetify)
      OurVue.prototype.$vuetify = new Vuetify({}).framework
    }
    this.installed.forEach((property) => {
      const service = this.framework[property]
      service.framework = this.framework
      service.init(root, ssrContext)
    })
    // rtl is not installed and
    // will never be called by
    // the init process

    this.framework.rtl = Boolean(this.preset.rtl)
  } // Instantiate a VuetifyService

  use (Service) {
    const property = Service.property
    if (this.installed.includes(property)) return // TODO maybe a specific type for arg 2?

    this.framework[property] = new Service(this.preset, this)
    this.installed.push(property)
  }
}
Shipnity.install = (Vue, args) => {
  install.call(Shipnity, Vue, {
    components,
    directives,
    filters,
    ...args
  })
}
Shipnity.installed = false
Shipnity.version = '1.0.0'
Shipnity.config = {
  silent: false
}
