/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Mickael Jeanroy
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/* global DATA_WAFFLE_IDX */
/* exported GridUtil */

var GridUtil = (function() {

  // Create initial empty object.
  var instance = {};

  var findRow = function(rows, index, def, stopOn) {
    var max = rows.length - 1;
    if (max < 0 || index < 0 || index <= def) {
      return def;
    }

    var currentIndex = Math.min(index, max);
    var dataIndex = index;

    while (currentIndex >= 0 && dataIndex >= index) {
      dataIndex = instance.getDataIndex(rows[currentIndex]);
      if (stopOn(dataIndex, index)) {
        return currentIndex;
      }

      currentIndex--;
    }

    return def;
  };

  instance.getDataIndex = function(row) {
    return Number(row.getAttribute(DATA_WAFFLE_IDX));
  };

  instance.getRowIndexForDataIndex = function(rows, index) {
    return findRow(rows, index, -1, function(dataIndex, index) {
      return dataIndex === index;
    });
  };

  instance.getPreviousRowIndexForDataIndex = function(rows, index) {
    return findRow(rows, index - 1, -1, function(dataIndex, index) {
      return dataIndex <= index;
    });
  };

  instance.getCheckbox = function(row) {
    return row.childNodes[0].childNodes[0];
  };

  return instance;

})();

@@ -1197,19 +1197,21 @@
 til */%0A%0A
-var
+const
  GridUti
@@ -1219,18 +1219,13 @@
  = (
-function()
+() =%3E
  %7B%0A%0A
@@ -1260,19 +1260,21 @@
 ject.%0A  
-var
+const
  instanc
@@ -1284,19 +1284,21 @@
  %7B%7D;%0A%0A  
-var
+const
  findRow
@@ -1300,24 +1300,16 @@
 ndRow = 
-function
 (rows, i
@@ -1330,18 +1330,23 @@
 pOn)
+ =%3E
  %7B%0A    
-var
+const
  max
@@ -1442,19 +1442,19 @@
  %7D%0A%0A    
-var
+let
  current
@@ -1491,11 +1491,11 @@
     
-var
+let
  dat
@@ -1783,34 +1783,14 @@
 x = 
-function(row) %7B%0A    return
+row =%3E
  Num
@@ -1829,19 +1829,80 @@
 _IDX));%0A
+%0A
   
-%7D
+instance.getCheckbox = row =%3E row.childNodes%5B0%5D.childNodes%5B0%5D
 ;%0A%0A  ins
@@ -1929,32 +1929,24 @@
 DataIndex = 
-function
 (rows, index
@@ -1938,32 +1938,35 @@
  = (rows, index)
+ =%3E
  %7B%0A    return fi
@@ -1984,32 +1984,24 @@
  index, -1, 
-function
 (dataIndex, 
@@ -2003,38 +2003,26 @@
 dex, index) 
-%7B%0A      return
+=%3E
  dataIndex =
@@ -2025,31 +2025,24 @@
 ex === index
-;%0A    %7D
 );%0A  %7D;%0A%0A  i
@@ -2079,32 +2079,24 @@
 DataIndex = 
-function
 (rows, index
@@ -2092,24 +2092,27 @@
 rows, index)
+ =%3E
  %7B%0A    retur
@@ -2142,24 +2142,16 @@
  1, -1, 
-function
 (dataInd
@@ -2161,30 +2161,18 @@
  index) 
-%7B%0A      return
+=%3E
  dataInd
@@ -2186,107 +2186,9 @@
 ndex
-;%0A    %7D);%0A  %7D;%0A%0A  instance.getCheckbox = function(row) %7B%0A    return row.childNodes%5B0%5D.childNodes%5B0%5D
+)
 ;%0A  

migrate GridUtil to es6