import { applyData } from './apply-data';

function getStore(App, name) {
  return App.__container__.lookup('store:' + (name || 'main'));
}

function getCreateData() {
  var createData = this._grateConfig.create, // todo - make this less sucky (2 of 2, see crudder)
      data;

  function processCreateOption(opt) {
    if (opt.allow !== false && opt.forbid !== true) {
      data = opt.data;
    }
  }

  if (Array.isArray(createData)) {
    createData.find(function(opt) {
      processCreateOption(opt);
      return !!data;
    });
  } else {
    processCreateOption(createData);
  }

  Ember.assert('Create is not a valid source for these options', data);
  return data;
}

function getList(name) {
  return getStore(this.App).find(name);
}
function getRandomFromList(name) {
  return getList.call(this, name)
    .then(function(list) {
      var length = list.get('length'),
          rand = Math.floor(Math.random() * length),
          item = list.objectAt(rand);

      if (length === 0) {
        throw new Error("'listRandom' source specified, but list was empty!");
      }
      return item.reload();
    });
}
function createItem(name, data) {
  var newObject = getStore(this.App).createRecord(name);

  return applyData.call(this, newObject, data)
    .then(function(object) {
      return object.save();
    });
}
function fetchItem(name, source) {
  var fetchFn;

  if (source === 'listRandom') {
    fetchFn = getRandomFromList.bind(this, name);
  } else if (source === 'listFilter') {
    // TODO - handle listFilter
  } else if (source === 'create') {
    fetchFn = createItem.bind(this, name, getCreateData.call(this));
  } else if (typeof source === 'number' || typeof source === 'string') {
    var store = getStore(this.App);
    fetchFn = store.find.bind(store, name, source);
  } else if (typeof source === 'function') {
    fetchFn = source.bind(this);
  } else {
    Ember.assert("Improper source specified");
    return;
  }

  return fetchFn(); // TODO - promise wrap
}

function updateItem(item, data) {
  return applyData.call(this, item, data)
    .then(function(object) {
      return object.save();
    });
}

export { getList, createItem, fetchItem, updateItem };

@@ -168,25 +168,21 @@
 %0A  var c
-reateData
+onfig
  = this.
@@ -193,16 +193,53 @@
 teConfig
+,%0A      createData = config && config
 .create,
@@ -354,16 +354,23 @@
  if (opt
+ && opt
 .allow !
@@ -629,75 +629,130 @@
 %0A%0A  
-Ember.assert('Create is not a valid source for these options', data
+if (!data) throw new Error(%22Invalid source: %60%7Bsource: 'create'%7D%60 was specified, but no %60%7Ballow: true%7D%60 create tests exist%22
 );%0A 

Provide better errors on an invalid source:'create'