export class EventStore {

  constructor(params) {
    this.store = {}
    this.params = params
  }

  storeGetData(data, updateFunc) {
    if (data.onlyGet) {
      return this.store[data.key] || null
    }
    return updateFunc({
      newData: data.newData,
      key: data.key,
      data: this.store[data.key] || null
    })
  }

  storeSetData(data) {
    return this.store[data.key] = data.value
  }

  storeRemoveData(data) {
    return this.store[data.key] = null
  }

  storeCreateOrGetData(data, updateFunc) {
    if (this.store[data.key]) {
      return updateFunc({
        newData: data.newData,
        key: data.key,
        data: this.store[data.key]
      })
    }
    if (!this.store[data.key]) {
      this.store[data.key] = data.defaultValue
      return updateFunc({
        newData: data.newData,
        key: data.key,
        data: data.defaultValue
      })
    }
  }

  localStoreGetData(data, updateFunc) {
    if (data.onlyGet) {
      return JSON.parse(localStorage.getItem(data.key)) || null
    }
    return updateFunc({
      newData: data.newData,
      key: data.key,
      data: JSON.parse(localStorage.getItem(data.key)) || null
    })
  }

  localStoreSetData(data) {
    return localStorage.setItem(data.key, JSON.stringify(data.value))
  }

  localStoreRemoveData(data) {
    return localStorage.removeItem(data.key)
  }
}
