// Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license. See LICENSE file in the project root for full license information.

/**
 * This method will be called at the start of exports.transform in ManagedReference.html.primary.js
 */
exports.preTransform = function (model) {
  if (model && model.children) {
    model.children.forEach(function (item) {
      handleItem(item);
    })
  }

  return model;

  function handleItem(item) {
    if (!item) return;
    if (item.syntax && item.syntax.parameters) {
      var parameters = item.syntax.parameters;
      var groupedParameters = [];
      var cachedParameters = [];
      var prefix = '';
      while (parameters.length > 0) {
        var parameter = parameters.pop();
        parameter.properties = null;
        if (parameter.id == prefix) {
          parameter.properties = cachedParameters;
          var cachedParameters = [];
          var prefix = '';
        }
        var index = parameter.id.lastIndexOf('.');
        if (index == -1) {
          groupedParameters.unshift(parameter);
        } else {
          prefix = parameter.id.substring(0, index);
          parameter.id = parameter.id.substring(index + 1);
          cachedParameters.unshift(parameter);
        }
      }
      item.syntax.parameters = groupedParameters;
    }
  }

  function groupParameter(parameter, parameters, groupedParameters) {
  }
}

/**
 * This method will be called at the end of exports.transform in ManagedReference.html.primary.js
 */
exports.postTransform = function (model) {
  return model;
}
@@ -527,28 +527,36 @@
 rs) %7B%0A      
-var 
+item.syntax.
 parameters =
@@ -556,16 +556,32 @@
 eters = 
+groupParameters(
 item.syn
@@ -598,58 +598,74 @@
 ters
+)
 ;%0A    
-  var groupedParameters = %5B%5D;%0A    
+%7D%0A  %7D%0A%7D%0A%0Afunction groupParameters(parameters) %7B%0A
   var 
-cach
+group
 edPa
@@ -685,42 +685,46 @@
 ;%0A  
-    var prefix = ''
+var stack = %5B%5D
 ;%0A  
-    while (
+for (var i = 0; i %3C 
 para
@@ -740,20 +740,17 @@
 ngth
- %3E 0) %7B%0A    
+; i++) %7B%0A
     
@@ -779,20 +779,13 @@
 ters
-.pop();%0A    
+%5Bi%5D;%0A
     
@@ -821,134 +821,90 @@
     
-    if (parameter.id == prefix) %7B%0A          parameter.properties = cachedParameters;%0A          var cachedParameters = %5B%5D;%0A    
+var prefixLength = 0;%0A    while (stack.length %3E 0) %7B%0A      var top = stack.pop();%0A
     
@@ -918,17 +918,27 @@
 refix = 
-'
+top.id + '.
 ';%0A     
@@ -942,32 +942,12 @@
     
-  %7D%0A        var index = 
+if (
 para
@@ -959,115 +959,108 @@
 .id.
-lastI
+i
 ndexOf(
-'.');%0A        if (index == -1) %7B%0A          groupedParameters.unshift(parameter);%0A        %7D else
+prefix) == 0) %7B%0A        prefixLength = prefix.length;%0A        if (!top.parameter.properties)
  %7B%0A 
@@ -1068,25 +1068,20 @@
         
-prefix = 
+top.
 paramete
@@ -1086,42 +1086,43 @@
 ter.
-id.substring(0, index);%0A  
+properties = %5B%5D;%0A        %7D%0A
         
 para
@@ -1113,24 +1113,28 @@
   %7D%0A        
+top.
 parameter.id
@@ -1135,62 +1135,128 @@
 ter.
-id = parameter.id.substring(index + 1);%0A          cach
+properties.push(parameter);%0A        stack.push(top);%0A        break;%0A      %7D%0A      if (stack.length == 0) %7B%0A        group
 edPa
@@ -1268,16 +1268,17 @@
 ers.
-unshift(
+push(top.
 para
@@ -1287,30 +1287,26 @@
 ter);%0A      
-  %7D%0A  
+%7D%0A
     %7D%0A      
@@ -1307,90 +1307,77 @@
     
-  item.syntax.parameters = groupedParameters;%0A    %7D%0A  %7D%0A%0A  function groupP
+stack.push(%7B id: parameter.id, parameter: parameter %7D);%0A    p
 arameter
 (par
@@ -1372,17 +1372,22 @@
 arameter
-(
+.id = 
 paramete
@@ -1391,21 +1391,49 @@
 eter
-, parameters,
+.id.substring(prefixLength);%0A  %7D%0A  return
  gro
@@ -1450,15 +1450,9 @@
 ters
-) %7B%0A  %7D
+;
 %0A%7D%0A%0A

fix algorithm to group parameters