var Model = require('base/model');
var _ = require('lodash');
var debug = require('debug')("craftodex:ui:relationship:model");

var registry = require('registry');

function Relationship (attrs, options) {
  debug("constructor", attrs, options);
  return Model.apply(this, arguments);
}

Model.extend({
  constructor: Relationship,
  modelType: "Relationship",

  props: {
    relationshipType: "link",
    is: "link",
    has: "link",
    symmetric: "link",
  },

  /*
  set: function (key, value, options) {
    debug("set", this, key, value, options);

    // Replicating a bit of the source so we can support all set 
    var attrs;
    options = options || {};

    // Handle both `"key", value` and `{key: value}` -style arguments.
    if (_.isObject(key) || key === null) {
        attrs = key;
        options = value;
    } else {
        attrs = {};
        attrs[key] = value;
    }

    var RelationshipType = require('relationship-type/model');

    relation(this, "relationshipType", RelationshipType)(attrs);

    //
    // polymorphic types for 'is' and 'has' children props
    //
    var Group = require('group/model');
    var Person = require('person/model');

    relation(this, "is", function (isAttrs) {
      switch (isAttrs['@type']) {
        case 'Group': return new Group(isAttrs);
        case 'Person': return new Person(isAttrs);
      }
    })(attrs);

    relation(this, "has", function (hasAttrs) {
      switch (hasAttrs['@type']) {
        case 'Group': return new Group(hasAttrs);
        case 'Person': return new Person(hasAttrs);
      }
    })(attrs);

    relation(this, "symmetric", Relationship)(attrs);

    // defer rest to the original implementation
    return Model.prototype.set.call(this, attrs, options);
  },
*/

});

function relation(self, name, constructor) {
  return function (attrs) {
    debug("relation", attrs);
    if (!self[name] && attrs[name] && !attrs[name].isState) {
      var relAttrs = attrs[name];
      delete attrs[name];
      if (
        typeof relAttrs === 'object' &&
        Object.keys(relAttrs).length === 1 &&
        typeof relAttrs['@id'] === 'string'
      ) {
        debug("registry", relAttrs);
        self[name] = registry.get(relAttrs['@id']);
      } else if (constructor.prototype.isState) {
        debug("model", relAttrs, constructor);
        self[name] = new constructor(relAttrs);
      } else {
        debug("constructor", relAttrs, constructor);
        self[name] = constructor(relAttrs);
      }
    }
  };
}

module.exports = Relationship;

@@ -461,2057 +461,11 @@
  %7D,%0A
-%0A  /*%0A  set: function (key, value, options) %7B%0A    debug(%22set%22, this, key, value, options);%0A%0A    // Replicating a bit of the source so we can support all set %0A    var attrs;%0A    options = options %7C%7C %7B%7D;%0A%0A    // Handle both %60%22key%22, value%60 and %60%7Bkey: value%7D%60 -style arguments.%0A    if (_.isObject(key) %7C%7C key === null) %7B%0A        attrs = key;%0A        options = value;%0A    %7D else %7B%0A        attrs = %7B%7D;%0A        attrs%5Bkey%5D = value;%0A    %7D%0A%0A    var RelationshipType = require('relationship-type/model');%0A%0A    relation(this, %22relationshipType%22, RelationshipType)(attrs);%0A%0A    //%0A    // polymorphic types for 'is' and 'has' children props%0A    //%0A    var Group = require('group/model');%0A    var Person = require('person/model');%0A%0A    relation(this, %22is%22, function (isAttrs) %7B%0A      switch (isAttrs%5B'@type'%5D) %7B%0A        case 'Group': return new Group(isAttrs);%0A        case 'Person': return new Person(isAttrs);%0A      %7D%0A    %7D)(attrs);%0A%0A    relation(this, %22has%22, function (hasAttrs) %7B%0A      switch (hasAttrs%5B'@type'%5D) %7B%0A        case 'Group': return new Group(hasAttrs);%0A        case 'Person': return new Person(hasAttrs);%0A      %7D%0A    %7D)(attrs);%0A%0A    relation(this, %22symmetric%22, Relationship)(attrs);%0A%0A    // defer rest to the original implementation%0A    return Model.prototype.set.call(this, attrs, options);%0A  %7D,%0A*/%0A%0A%7D);%0A%0Afunction relation(self, name, constructor) %7B%0A  return function (attrs) %7B%0A    debug(%22relation%22, attrs);%0A    if (!self%5Bname%5D && attrs%5Bname%5D && !attrs%5Bname%5D.isState) %7B%0A      var relAttrs = attrs%5Bname%5D;%0A      delete attrs%5Bname%5D;%0A      if (%0A        typeof relAttrs === 'object' &&%0A        Object.keys(relAttrs).length === 1 &&%0A        typeof relAttrs%5B'@id'%5D === 'string'%0A      ) %7B%0A        debug(%22registry%22, relAttrs);%0A        self%5Bname%5D = registry.get(relAttrs%5B'@id'%5D);%0A      %7D else if (constructor.prototype.isState) %7B%0A        debug(%22model%22, relAttrs, constructor);%0A        self%5Bname%5D = new constructor(relAttrs);%0A      %7D else %7B%0A        debug(%22constructor%22, relAttrs, constructor);%0A        self%5Bname%5D = constructor(relAttrs);%0A      %7D%0A    %7D%0A  %7D;%0A%7D
+%7D);
 %0A%0Amo

remove old code