import angular from 'angular';
import {
  ColumnDefaults
}
from '../defaults';
import {
  CamelCase
}
from '../utils/utils';

export let DataTableService = {

  // id: [ column defs ]
  columns: {},
  dTables: {},

  saveColumns(id, columnElms) {
    if (columnElms && columnElms.length) {
      this.dTables[id] = columnElms;
    }
  },

  /**
   * Create columns from elements
   * @param  {array} columnElms
   */
  buildColumns(scope, parse) {
    //FIXME: Too many nested for loops.  O(n3)

    // Iterate through each dTable
    angular.forEach(this.dTables, (columnElms, id) => {
      this.columns[id] = [];

      // Iterate through each column
      angular.forEach(columnElms, (c) => {
        var column = {};

        // Iterate through each attribute
        angular.forEach(c.attributes, (attr) => {
          var attrName = CamelCase(attr.name);

          // cuz putting className vs class on
          // a element feels weird
          switch (attrName) {
            case 'class':
              column.className = attr.value;
              break;
            case 'name':
            case 'prop':
              column[attrName] = attr.value;
              break;
            case 'headerRenderer':
            case 'cellRenderer':
            case 'cellDataGetter':
              column[attrName] = parse(attr.value);
              break;
            default:
              column[attrName] = parse(attr.value)(scope);
              break;
          }
        });

        if (c.innerHTML !== '') {
          column.template = c.innerHTML;
        }

        this.columns[id].push(column);
      });
    });
    this.dTables = {};
  }
};

@@ -279,24 +279,76 @@
 s.length) %7B%0A
+      let columnsArray = %5B%5D.slice.call(columnElms);%0A
       this.d
@@ -366,20 +366,22 @@
 = column
-Elms
+sArray
 ;%0A    %7D%0A

Fix column directives not rendering in IE