"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getGroupedChecks = getGroupedChecks;

// process table groupings, given a list of checks and a 'groupBy' object
function getGroupedChecks(checks, groupBy) {
  if (groupBy === "name" || groupBy === "description" || groupBy === "canaryName") {
    var _groupedChecks = {};
    var _groupNames = [];
    checks.forEach(function (check) {
      var value = check[groupBy] || "(none)";

      if (_groupNames.indexOf(value) === -1) {
        _groupNames.push(value);

        _groupedChecks[value] = [];
      }

      _groupedChecks[value].push(check);
    });
    return _groupedChecks;
  }

  var groupedChecks = {
    Others: []
  };
  var groupNames = ["Others"];
  checks.forEach(function (check) {
    var hasValidGroup = false;
    var groupName;

    if (check.labels) {
      var labelKeys = Object.keys(check.labels);
      labelKeys.forEach(function (key) {
        // if current item has a matching label with the currently selected groupBy
        if (key === groupBy) {
          hasValidGroup = true;
          groupName = check.labels[key];

          if (groupNames.indexOf(groupName) === -1) {
            groupNames.push(groupName);
            groupedChecks[groupName] = [];
          }
        }
      });
    }

    if (hasValidGroup) {
      groupedChecks[groupName].push(check);
    } else {
      groupedChecks.Others.push(check);
    }
  });
  return groupedChecks;
}