'use strict';

var utilsLib = require('../tasks/utils');

function getData(source) {
  var answer = addByGroupNameAndUrl(source);

  answer = getMoreThan(answer, 9);
  answer = removeLocationString(answer);
  answer = utilsLib.sortByAlphabet(answer, 'group');

  return answer;
}

function addByGroupNameAndUrl(source) {
  var groups = [];
  var answer = [];
  var attr = 'group_name';

  source.forEach(function(file) {
    var data = require('.' + file);

    data.events.forEach(function(ev) {
      var attribute = ev[ attr ];

      if (groups.indexOf(attribute) < 0) {
        if (attribute) {
          groups.push(attribute)
          answer.push({
            group: attribute,
            n: 1,
            url: ev.group_url
          })
        }
      } else {
        answer.forEach(function(a) {
          if (a.group === ev[ attr ]) {
            a.n += 1;
          }
        })
      }
    })
  })

  return answer;
}

function getMoreThan(array, number) {
  var answer = [];

  array.forEach(function(eachItem) {
    if (eachItem.n > number) {
      answer.push(eachItem)
    }
  })

  return answer;
}

function removeLocationString(array) {
  array.forEach(function(eachItem) {
    var group = eachItem.group.replace('Singapore ', '');
    eachItem.group = group;

    group = eachItem.group.replace('(Singapore)', '');
    eachItem.group = group;

    group = eachItem.group.replace('(SG)', '');
    eachItem.group = group;

    group = eachItem.group.replace(' SG', '');
    eachItem.group = group;

    // remove "/groups" from facebook group url for Facebook Pages/Groups
    var url = eachItem.url;
    if (url.indexOf('www.facebook.com/groups') > -1) {
      eachItem.url = url.replace('/groups', '')
    }
  })

  return array;
}

exports.getData = getData;

@@ -355,35 +355,8 @@
  %5B%5D;
-%0A  var attr = 'group_name';
 %0A%0A  
@@ -489,20 +489,28 @@
 e = ev%5B 
-attr
+'group_name'
  %5D;%0A%0A   
@@ -525,48 +525,26 @@
 roup
-s.indexOf(attribute) %3C 0) %7B%0A        if (
+NotInList(groups, 
 attr
@@ -549,21 +549,20 @@
 tribute)
+)
  %7B%0A
-  
         
@@ -588,26 +588,24 @@
 te)%0A        
-  
 answer.push(
@@ -606,18 +606,16 @@
 .push(%7B%0A
-  
         
@@ -648,18 +648,14 @@
     
-  
 n: 1,%0A
-  
     
@@ -690,44 +690,277 @@
     
-  %7D)%0A        %7D%0A      %7D else %7B%0A      
+%7D)%0A      %7D else %7B%0A        answer = increaseCountOfGraph(answer, attribute);%0A      %7D%0A    %7D)%0A  %7D)%0A%0A  return answer;%0A%7D%0A%0Afunction groupNotInList(groups, attribute) %7B%0A  return groups.indexOf(attribute) %3C 0 && attribute;%0A%7D%0A%0Afunction increaseCountOfGraph(answer, attribute) %7B%0A
   an
@@ -982,30 +982,24 @@
 nction(a) %7B%0A
-      
     if (a.gr
@@ -1009,35 +1009,28 @@
  ===
- ev%5B
  attr
- %5D) %7B%0A     
+ibute) %7B%0A
       
- 
 a.n 
@@ -1043,41 +1043,9 @@
     
-      %7D%0A        %7D)%0A      %7D%0A    %7D)
+%7D
 %0A  %7D
@@ -1661,114 +1661,126 @@
     
-// remove %22/groups%22 from facebook group url for Facebook Pages/Groups%0A    var url = eachItem.url;%0A    if (
+eachItem.url = correctFacebookUrl(eachItem.url);%0A  %7D)%0A%0A  return array;%0A%7D%0A%0Afunction correctFacebookUrl(url) %7B%0A  return 
 url.
@@ -1822,32 +1822,10 @@
 %3E -1
-) %7B%0A      eachItem.url =
+ ?
  url
@@ -1851,35 +1851,13 @@
  '')
-%0A    %7D%0A  %7D)%0A%0A  return array
+: url
 ;%0A%7D%0A

abstract out functions