import computed from 'ember-computed';
import collapseKeys from './collapse-keys';
import flattenKeys from './flatten-keys';

function parseComputedArgs(args) {
  return {
    keys: args.slice(0, -1),
    callback: args[args.length - 1]
  };
}

function mapKeysToValues(keys, getValue, context) {
  return keys.map(key => getValue(context, key));
}

function buildCallback(keys, incomingCallback, getValue) {
  let collapsedKeys = collapseKeys(keys);

  let newCallback;
  if (typeof incomingCallback === 'function') {
    newCallback = function() {
      let values = mapKeysToValues(collapsedKeys, getValue, this);
      return incomingCallback.apply(this, values);
    };
  } else {
    newCallback = {};
    if (incomingCallback.get) {
      newCallback.get = function() {
        let values = mapKeysToValues(collapsedKeys, getValue, this);
        return incomingCallback.get.apply(this, values);
      };
    }
    if (incomingCallback.set) {
      newCallback.set = incomingCallback.set;
    }
  }

  return newCallback;
}

export default function(args, getValue) {
  let { keys, callback: incomingCallback } = parseComputedArgs(args);

  let newCallback = buildCallback(keys, incomingCallback, getValue);

  return computed(...flattenKeys(keys), newCallback);
}

@@ -358,33 +358,42 @@
 n buildCallback(
-k
+collapsedK
 eys, incomingCal
@@ -415,51 +415,8 @@
 ) %7B%0A
-  let collapsedKeys = collapseKeys(keys);%0A%0A
   le
@@ -1104,16 +1104,59 @@
 args);%0A%0A
+  let collapsedKeys = collapseKeys(keys);%0A%0A
   let ne
@@ -1181,17 +1181,26 @@
 allback(
-k
+collapsedK
 eys, inc

move collapseKeys