"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.filterChecks = filterChecks;
exports.filterChecksByText = filterChecksByText;
exports.hasStringMatch = hasStringMatch;
exports.isHealthy = isHealthy;
exports.labelIndex = labelIndex;
exports.matchesLabel = matchesLabel;

var _lodash = require("lodash");

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }

function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function matchesLabel(check, labels) {
  if (labels.length === 0) {
    return true;
  }

  var _iterator = _createForOfIteratorHelper(labels),
      _step;

  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var label = _step.value;

      if (label.type === "canary") {
        if (check.labels == null) {
          return true;
        }

        if (check.labels[label.key] === label.value) {
          return true;
        }
      } else {
        return true;
      }
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }

  return false;
}

function isHealthy(check) {
  if (check.checkStatuses == null) {
    return false;
  }

  var passed = true;
  (0, _lodash.forEach)(check.checkStatuses, function (s) {
    passed = passed && s.status;
  });
  return passed;
}

function filterChecks(checks, hidePassing, labels) {
  var filtered = [];

  if (checks == null) {
    return filtered;
  }

  checks = (0, _lodash.orderBy)(checks, function (a) {
    return a.description;
  });

  var _iterator2 = _createForOfIteratorHelper(checks),
      _step2;

  try {
    for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
      var check = _step2.value;

      if (hidePassing && isHealthy(check)) {
        // eslint-disable-next-line no-continue
        continue;
      }

      if (!matchesLabel(check, labels)) {
        // eslint-disable-next-line no-continue
        continue;
      }

      filtered.push(check);
    }
  } catch (err) {
    _iterator2.e(err);
  } finally {
    _iterator2.f();
  }

  return filtered;
}

function labelIndex(selectedLabels, label) {
  return (0, _lodash.findIndex)(selectedLabels, function (l) {
    return l.id === label.id;
  });
}

function hasStringMatch(pattern, text) {
  if (text == null) {
    return false;
  }

  return text.indexOf(pattern) >= 0;
}

function filterChecksByText(checks, textQuery) {
  if (checks == null) {
    return [];
  }

  if ((0, _lodash.isEmpty)(textQuery)) {
    return checks;
  }

  var text = textQuery.toLowerCase();

  var filtered = _toConsumableArray(checks).filter(function (check) {
    var _check$description, _check$canaryName, _check$endpoint, _check$name;

    var match = hasStringMatch(text, (_check$description = check.description) === null || _check$description === void 0 ? void 0 : _check$description.toLowerCase()) || hasStringMatch(text, (_check$canaryName = check.canaryName) === null || _check$canaryName === void 0 ? void 0 : _check$canaryName.toLowerCase()) || hasStringMatch(text, (_check$endpoint = check.endpoint) === null || _check$endpoint === void 0 ? void 0 : _check$endpoint.toLowerCase()) || hasStringMatch(text, (_check$name = check.name) === null || _check$name === void 0 ? void 0 : _check$name.toLowerCase());
    return match;
  });

  return filtered;
}