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

var Relationships = require('relationship/collection');

module.exports = Model.extend({

  modelType: "Agent",

  initialize: function () {
    debug("initialize", this);
    Model.prototype.initialize.apply(this, arguments);
    this.on("change", debug.bind(null, "change"));
  },

  props: {
    name: "string",
  },

  children: {
    relationships: Relationships,
  },

  derived: {
    relsByType: {
      deps: ['relationships'],
      cache: false,
      fn: function () {
        var rels = {};
        this.relationships.forEach(function (rel) {
          var relType = rel.relationshipType;
          if (!relType) return;
          var relType = relType.getId();
          if (!rels[relType]) {
            rels[relType] = new Relationships();
          }
          rels[relType].add(rel);
        });
        return rels;
      },
    },

    hasRelsByType: {
      deps: ['relsByType'],
      cache: false,
      fn: function () {
        debug("hasRelsByType", this.relsByType)
        return _.mapValues(this.relsByType, function (rels) {
          return rels.filter(function (rel) {
            debug(rel.is && rel.is.getId(), this.getId())
            return rel.is && (rel.is.getId() === this.getId());
          }, this)
        }, this);
      },
    },

  },

});

@@ -442,15 +442,18 @@
 %0A  c
-hildren
+ollections
 : %7B%0A

change agent children to collections