import { uniqueId } from 'lodash-es'

class Constants {
  constructor(namespace) {
    this.uniqueId = uniqueId()
    this.namespace = namespace
  }
  of(key, value) {
    if (typeof key !== 'string') {
      throw new Error(`Expect key of constants to be string, but accept type ${typeof key} and value 'of'`)
    }
    key = key.toUpperCase()
    if (typeof this[key] !== 'undefined') {
      return this[key]
    }

    this[key] = value !== undefined ? value : `${this.namespace}-${this.uniqueId}-${key}`
    return this[key]
  }
}

export default (function() {
  const store = {}
  return function(namespace) {
    if (typeof namespace !== 'string') {
      throw new Error(`Expect namespace to be string, but accept type ${typeof namespace}`)
    }

    namespace = namespace.toUpperCase()
    if (!store[namespace]) {
      store[namespace] = new Constants(namespace)
    }
    return store[namespace]
  }
})()
