/* global UTILS */
'use strict';

angular.module('alienUiApp').factory('topologyJsonProcessor',
  ['orderedMapEnricher', function(orderedMapEnricher) {
    // This service post-process a topology json in order to add maps field from ordered maps array (array of MapEntry).
    return {
      /**
       * Compute the number of times that a requirement template is used by relationships.
       *
       * @param topology The topology dto object to post-process after json deserialization.
       */
      process: function(topology) {
        orderedMapEnricher.processMap(topology.nodeTypes, 'properties');
        orderedMapEnricher.processMap(topology.relationshipTypes, 'properties');
        orderedMapEnricher.processMap(topology.capabilityTypes, 'properties');

        this.postProcess(topology.topology.nodeTemplates, ['properties', 'attributes', 'requirements', 'relationships', 'capabilities']);
        for (var nodeTemplateName in topology.topology.nodeTemplates) {
          if (topology.topology.nodeTemplates.hasOwnProperty(nodeTemplateName)) {
            if (UTILS.isDefinedAndNotNull(topology.topology.nodeTemplates[nodeTemplateName].relationships)) {
              for (var i = 0; i < topology.topology.nodeTemplates[nodeTemplateName].relationships.length; i++) {
                orderedMapEnricher.process(topology.topology.nodeTemplates[nodeTemplateName].relationships[i].value, 'properties');
              }
            } else if (UTILS.isDefinedAndNotNull(topology.topology.nodeTemplates[nodeTemplateName].capabilities)) {
              for (i = 0; i < topology.topology.nodeTemplates[nodeTemplateName].capabilities.length; i++) {
                orderedMapEnricher.process(topology.topology.nodeTemplates[nodeTemplateName].capabilities[i].value, 'properties');
              }
            }
          }
        }
      },

      postProcess: function(object, properties) {
        for (var i = 0; i < properties.length; i++) {
          orderedMapEnricher.processMap(object, properties[i]);
        }
      }
    };
  } // function
  ]);

@@ -88,18 +88,16 @@
 cessor',
-%0A 
  %5B'order
@@ -1440,13 +1440,20 @@
    %7D
- else
+%0A           
  if 
@@ -1565,24 +1565,28 @@
    for (
-i
+var j
  = 0; 
-i
+j
  %3C topol
@@ -1643,33 +1643,33 @@
 ilities.length; 
-i
+j
 ++) %7B%0A          
@@ -1764,17 +1764,17 @@
 ilities%5B
-i
+j
 %5D.value,
@@ -2060,14 +2060,12 @@
 unction%0A
-  
 %5D);%0A

FIX capabilities properties map not well generated when relationships defined for a node