import lodash from 'lodash-es'
window._ = lodash

import { Datastore, Pointer } from '@djinlist/datastore'
import { send } from './lib/send'
import { authorization } from './lib/authorization'
import { subscribe } from './lib/subscribe'
import { unsubscribe } from './lib/unsubscribe'
import Loadable from 'svelte-loadable'

window.datastore = new Datastore
window.Pointer = Pointer
window.send = send
window.authorization = authorization
window.subscribe = subscribe
window.unsubscribe = unsubscribe

window.Loadable = Loadable

import { ThemeHandler } from './lib/theme'
window.theme = new ThemeHandler()

// config
datastore.set('session/node_id', '952ede89-4c91-4df7-bdab-c6dda4257abb')
datastore.set('session/ping_interval', 10000)

datastore._chain = datastore.chain
datastore.chain = svelte => {
  const chain = datastore._chain()

  if (svelte === void 0) {
    return chain
  }

  svelte.$$.on_destroy.push(() => {
    chain.destroy()
  })

  return chain
}

// Drivers
import { Spotify } from './lib/spotify'
const spotify = new Spotify(datastore)

import { Bootstrap } from './bootstrap.js'
let bootstrap

import * as sapper from '@sapper/app';

const load = () => {
  window.document.body.addEventListener('touchstart touchend', function(e) {
    e.preventDefault();
    e.toggleClass('hover_effect');
  })

  console.log('Loading Djinlist bootstrap')
  if (bootstrap != null) {
    console.warn(`index.js ${VERSION} load()`, 'bootstrap already exists') // eslint-disable-line no-console
    return
  }

  new Notification({
    target: document.querySelector('#notifications')
  })

  bootstrap = new Bootstrap()
}

sapper.start({
  target: document.querySelector('#sapper')
}).then(load)