'use strict';

var DS          = require('./delaney');
var fundamental = require('./fundamental');
var derived     = require('./derived');
var generators  = require('../common/generators');
var cosets      = require('../fpgroups/cosets');


var coverForTable = function coverForTable(ds, table, edgeToWord) {
  return derived.cover(ds, table.size, function(k, i, D) {
    return edgeToWord.getIn([D, i])
      .reduce(function(k, g) { return table.getIn([k, g]); }, k);
  });
};


var subgroupCover = function subgroupCover(ds, subgroupGens) {
  var fun = fundamental.fundamentalGroup(ds);
  var table = cosets.cosetTable(fun.nrGenerators, fun.relators, subgroupGens);

  return coverForTable(ds, table, fun.edgeToWord);
};


var finiteUniversalCover = function finiteUniversalCover(ds) {
  return subgroupCover(ds, []);
};


var covers = function covers(ds, maxDegree) {
  var fun = fundamental.fundamentalGroup(ds);
  var tableGenerator = cosets.tables(fun.nrGenerators, fun.relators, maxDegree);

  return generators.results(tableGenerator).map(function(table) {
    return coverForTable(ds, table, fun.edgeToWord);
  });
};


if (require.main == module) {
  console.log(finiteUniversalCover(DS.parse('<1.1:1:1,1,1:4,3>')));
}

@@ -615,20 +615,30 @@
 etTable(
-fun.
+%0A    fun.get('
 nrGenera
@@ -641,30 +641,37 @@
 nerators
+')
 , fun.
+get('
 relators
 , subgro
@@ -662,16 +662,18 @@
 relators
+')
 , subgro
@@ -720,26 +720,32 @@
 le, fun.
+get('
 edge
-ToW
+2w
 ord
+')
 );%0A%7D;%0A%0A%0A
@@ -973,20 +973,30 @@
 .tables(
-fun.
+%0A    fun.get('
 nrGenera
@@ -1003,22 +1003,29 @@
 tors
+')
 , fun.
+get('
 relators
 , ma
@@ -1020,16 +1020,18 @@
 relators
+')
 , maxDeg
@@ -1143,26 +1143,32 @@
 le, fun.
+get('
 edge
-ToW
+2w
 ord
+')
 );%0A  %7D);
@@ -1217,16 +1217,21 @@
 ole.log(
+'' + 
 finiteUn

fix a simple bug in D-symbol cover code