text: import { Grouping } from './groupingTryOut.js';
  
  export class IndividualsGrouper {
    
    constructor(interactiveCanvas, individuals, groupingLayouter){
      this.interactiveCanvas = interactiveCanvas;
      
      // Set up groupingLayouter
      this.groupingLayouter = groupingLayouter;
      this.groupingLayouter.setParentIdKey("parentId");
      this.groupingLayouter.setIdKey("id");
      
      this.rawIndividuals = individuals;
      
      this.groupingKeys = "root";
      
      this.headGrouping = new Grouping(
        this,
        this.groupingKey, 
        [], 
        [],
        [],
        [],
        this.rawIndividuals.slice(), 
        this.groupingLayouter);
      this.headGrouping.initializeRootGrouping();
    }
    
    getGroupingStructure(){
      return this.headGrouping.getGroupingStructure();
    } 
    
    addGrouping(groupingKey){
      this.headGrouping.addGrouping(groupingKey);
      this.interactiveCanvas.updateNodes(this.getGroupingStructure(), groupingKey)
      this.interactiveCanvas.animateNodes();
    }
    
    removeGrouping(groupingKey){
      this.headGrouping.removeGrouping(groupingKey);
      this.interactiveCanvas.updateNodes(this.getGroupingStructure())
      this.interactiveCanvas.animateNodes();
    }
    
    getUniqueValuesForKey(key){
      return this.getUniqueValues(this.rawIndividuals, key);
    }
    
    getUniqueValues(individuals, key){
      if(Array.isArray(individuals[0][key])){
        return this.getUniqueValuesForArrayGrouping(individuals, key)
      } else {
        return [...new Set(this.rawIndividuals.map((individual) => individual[key]))]
      }
      
    }
     
    getUniqueValuesForArrayGrouping(individuals, key){
      let values = new Set();
      individuals.forEach((individual) => {
        let individualValues = this.getSortedValue(individual[key]);
        
        if(individualValues !== "missing"){
          individualValues.forEach((value) => {
            values.add(value);
          })
        }
      });
      
      let permutations = this.getPermutationsForValues(Array.from(values));
      permutations.push("missing");
      return permutations;
          
    }
    
    getSortedValue(value){
      if(Array.isArray(value)) return value.sort();
      return value;
    }
    
    getPermutationsForValues(array){
      let permutations = [];
      let intermediate = [];
      array.forEach((entry) => {
        intermediate.push(entry);
        if(intermediate.length > 1) {
          permutations.push([entry])
        }
        permutations.push([...intermediate]);
      })
      
      return permutations.map(permutation => permutation.toString());
  
    }
  
  }