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;

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",
  },
});

module.exports = Relationship;

remove old code