/**
 * @description Helper method
 */
function _f(a, obj1) {
  ele = a.filter(ele => {
    return ele.id === obj1.id;
  });
  return Object.assign({}, ele);
}


 /**
  * @description
  * Filter unused property
  */
function removeProp(obj, propName) {
  for (var p in obj) {
    if (obj.hasOwnProperty(p)) {
      if (p == propName) {
        delete obj[p];
      } else if (typeof obj[p] == "object") {
        removeProp(obj[p], propName);
      }
    }
  }
  return obj;
}


/**
 * @description
 * Validates displayPref JSON
 */

module.exports.val = function val(a, b) {
  b = removeProp(b,'expand');
  var l2, l3;
  if (a.length === b.length) {
    var isSame = true;
    for (ele1 of a) {
      ele2 = _f(b, ele1);

      if (Object.keys(ele1).length === Object.keys(ele2[0]).length) {
        //check for nodes
        if (ele1.nodes && ele2[0].nodes) {
          if (ele1.nodes.length === ele2[0].nodes.length) {
            ele1.nodes.forEach(e => {
              if (e.nodes && e.nodes.length > 0) {
                l2 = e.nodes.length;
              }
            });
            ele2[0].nodes.forEach(e2 => {
              if (e2.nodes && e2.nodes.length > 0) {
                l3 = e2.nodes.length;
              }
            });

            if (l2 !== l3) {
              isSame = false;
            }
          } else {
            isSame = false;
          }
        } else {
          isSame = false;
        }
      } else {
        isSame = false;
      }
    }
  } else {
    isSame = false;
  }
  return isSame;
};
