"use strict";

var utils = require('./../../../utils');

// yamb.prototype.related

var suppose = function(value) {
  return (utils.is.str(value) && !utils.whitespace(value)) || utils.is.num(value);
};

var validate = function(value) {
  if (!utils.is.arr(value)) {
    return false;
  }

  for (var i=0, length=value.length; i<length; i++) {
    if (!suppose(value[i])) {
      return false;
    }
  }

  return true;
};

module.exports = function(priv) {
  var facility = function(relate) {
    return utils.is.yamb(relate) && relate[priv.symbl].id;
  };

  var getter = function() {
    return utils.clone(this[priv.symbl].related);
  };

  var setter = function(related) {
    if (!utils.is.arr(related) && !suppose(related) && !facility(related)) {
      return;
    }

    if (utils.is.arr(related)) {
      var data = [];

      for (var i=0, length=related.length; i<length; i++) {
        if (suppose(related[i])) {
          data[data.length] = related[i];
        } else if (facility(related[i])) {
          data[data.length] = '' + related[i].id;
        }
      }

      related = data;
    } else if (suppose(related)) {
      related = [related];
    } else {
      related = facility(related) ? ['' + related.id] : [];
    }

    this[priv.symbl].related = related;
    this[priv.flags].related = !utils.compare(this[priv.shado].related, related);
  };

  return {
    validate: validate,
    get: getter,
    set: setter
  };
};
@@ -764,24 +764,56 @@
 urn;%0A    %7D%0A%0A
+    var data = %5B%5D;%0A    var id;%0A%0A
     if (util
@@ -837,30 +837,8 @@
 ) %7B%0A
-      var data = %5B%5D;%0A%0A
     
@@ -1023,33 +1023,18 @@
         
+i
 d
-ata%5Bdata.length%5D
  = '' + 
@@ -1048,24 +1048,25 @@
 %5Bi%5D.id;%0A
+%0A
         
 %7D%0A      
@@ -1061,16 +1061,82 @@
     
-%7D%0A
+  if (this.id && ('' + this.id) === id) %7B%0A            continue;%0A 
       
+   
 %7D%0A%0A 
@@ -1144,23 +1144,53 @@
     
-related = data;
+    data%5Bdata.length%5D = id;%0A        %7D%0A      %7D
 %0A   
@@ -1227,23 +1227,20 @@
 %7B%0A      
-related
+data
  = %5Brela
@@ -1260,69 +1260,137 @@
 lse 
-%7B%0A      related = facility(related) ? %5B'' + related.id%5D : %5B%5D;
+if (facility(related)) %7B%0A      id = '' + related.id;%0A%0A      if (!this.id %7C%7C ('' + this.id) !== id) %7B%0A        data = %5Bid%5D;%0A      %7D
 %0A   
@@ -1424,23 +1424,20 @@
 lated = 
-related
+data
 ;%0A    th
@@ -1502,23 +1502,20 @@
 elated, 
-related
+data
 );%0A  %7D;%0A

Fix related tests