import types from '../store/mutation-types'
import store from '../store/index'

const io = require('socket.io-client')

let socket = io(`${window.location.hostname}:${window.location.port}`)

let keys = Object.keys(types)

keys.forEach(key => {
  let value = types[key]

  if (typeof value === 'object') {
    let eventKeys = Object.keys(value.events)

    eventKeys.forEach(eventKey => {
      let event = value.events[eventKey]
      let eventName = `${value.namespace}/${event}`

      socket.on(eventName, (data) => {
        // console.log(`Invoking socket child event: ${type}`);
        // console.log(data);
        store.commit(eventName, data)

      })
    })
  } else {
    socket.on(value, (data) => {
      // console.log(`Invoking socket event: ${type}`);
      // console.log(data);
      store.commit(value, data)
      if(value == "prayer") {
        console.log('prayer event');
      }
    })
  }
})

export default socket
