// really basic version of ./expander.js. focused solely
// on string replacements within objects/arrays/strings.

var kindOf = require('../lang/kindOf')
var get = require('../object/get')

function interpolate (template, replacements, syntax) {
  return resolve(template, replacements || template, syntax)

  function resolve (template, data, opts) {
    switch(kindOf(template)) {
      case 'string': return resolveString(template, data, opts)
      case 'object': return resolveObject(template, data, opts)
      case 'array' : return resolveArray(template, data, opts)
      default      : return template
    }
  }

  function resolveObject (obj, data, opts) {
    Object.keys(obj).forEach(function (key, idx, arr) {
      obj[key] = resolve(obj[key], data, opts)
    })
    return obj
  }

  function resolveArray (arr, data, opts) {
    var len = arr.length
    var i = - 1
    while (++i < len) arr[i] = resolve(arr[i], data, opts)
    return arr
  }

  function resolveString (str, data, opts) {
    var stache = opts || /\{\{([^\}]+)\}\}/g // mustache-like

    function replaceFn (match, prop) {
      var template = String(get(data, prop))

      return stache.test(template)
        ? template.replace(stache, replaceFn)
        : template
    }

    return str.replace(stache, replaceFn)
  }
}

module.exports = interpolate

@@ -151,19 +151,22 @@
 f')%0Avar 
-get
+look  
  = requi
@@ -179,19 +179,20 @@
 /object/
-get
+look
 ')%0A%0Afunc
@@ -359,16 +359,17 @@
   switch
+ 
 (kindOf(
@@ -397,59 +397,16 @@
 se '
-string': return resolveString(template, data, opts)
+array' :
 %0A   
@@ -478,23 +478,23 @@
   case '
-array' 
+string'
 : return
@@ -501,21 +501,22 @@
  resolve
-Array
+String
 (templat
@@ -757,172 +757,8 @@
  %7D%0A%0A
-  function resolveArray (arr, data, opts) %7B%0A    var len = arr.length%0A    var i = - 1%0A    while (++i %3C len) arr%5Bi%5D = resolve(arr%5Bi%5D, data, opts)%0A    return arr%0A  %7D%0A%0A
   fu
@@ -925,18 +925,12 @@
 e = 
-String(get
+look
 (dat
@@ -937,17 +937,16 @@
 a, prop)
-)
 %0A%0A      

refactor string/expand.js