// @flow
import debug from 'debug'
import kebabCase from 'lodash/kebabCase'
import {sprintf} from 'sprintf-js'

const dbg = debug('woonerf:message')

let messages = {}
if (process.env.MESSAGES) {
  setMessages(JSON.parse(process.env.MESSAGES))
}

export default function getMessage (defaultMessage: string, slug: void | string, ...args): string {
  const key = slug || kebabCase(defaultMessage)
  const msg = messages[key] || defaultMessage
  const result = args ? sprintf(msg, ...args) : msg
  dbg(key, result)
  return result
}

export function setMessages (newMessages) {
  messages = newMessages
}

@@ -28,16 +28,45 @@
 'debug'%0A
+import get from 'lodash/get'%0A
 import k
@@ -431,24 +431,28 @@
 t msg = 
+get(
 messages
 %5Bkey%5D %7C%7C
@@ -447,16 +447,14 @@
 ages
-%5Bkey%5D %7C%7C
+, key,
  def
@@ -464,16 +464,17 @@
 tMessage
+)
 %0A  const

Use lodash/get for keyed lookups