"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.defaultTabSelections = exports.defaultPivotSelections = exports.defaultCellTypeSelections = void 0;
exports.getBooleanLabels = getBooleanLabels;
exports.getLabelSelections = getLabelSelections;
exports.getLabelsFromState = getLabelsFromState;
exports.getNonBooleanLabels = getNonBooleanLabels;
exports.getTabSelections = getTabSelections;
exports.getUniqueNonBooleanLabelKeys = getUniqueNonBooleanLabelKeys;

var _bi = require("react-icons/bi");

var _im = require("react-icons/im");

var _ti = require("react-icons/ti");

var _isBooleanValue = require("../../../lib/isBooleanValue");

var _jsxRuntime = require("react/jsx-runtime");

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

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

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 _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

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

function getLabelsFromState(labelState) {
  return Object.keys(labelState).reduce(function (acc, fullLabel) {
    var start = new RegExp("^([^:]*:)");
    var end = new RegExp("(:[^:]*)$");
    var value = new RegExp("([^:]*)$");
    var label = fullLabel.replace(start, "").replace(end, "");

    var _value$exec = value.exec(fullLabel),
        _value$exec2 = _slicedToArray(_value$exec, 1),
        labelValue = _value$exec2[0]; // Need to decide here what kinds of values are representations of bools.
    // The loose comparison may be too permissive.


    if ((0, _isBooleanValue.isBooleanValue)(labelValue) === false) {
      acc.nonBoolean[label] = labelValue !== null && labelValue !== void 0 ? labelValue : null;
    }

    acc.all[label] = labelValue !== null && labelValue !== void 0 ? labelValue : null;
    return acc;
  }, {
    all: {},
    nonBoolean: {}
  });
} // provide a list of group selections that includes (non-boolean) labels, given a list of checks.


function getLabelSelections(checks) {
  var defaultSelections = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  var icon = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : /*#__PURE__*/(0, _jsxRuntime.jsx)(_bi.BiLabel, {});
  var nonBooleanLabels = getNonBooleanLabels(Object.values(checks));
  var newLabelSelections = defaultSelections;

  var uniqueLabels = _toConsumableArray(new Set(Object.values(nonBooleanLabels).map(function (o) {
    return o.key;
  })));

  uniqueLabels.sort().forEach(function (label) {
    var onlyAlphabets = label.replace(/[^a-zA-Z]/g, "");
    newLabelSelections[label] = {
      id: label,
      name: onlyAlphabets,
      icon: icon,
      description: label,
      value: label,
      labelValue: label,
      key: label
    };
  });
  return newLabelSelections;
}

var defaultPivotSelections = {
  none: {
    id: "none",
    name: "none",
    icon: /*#__PURE__*/(0, _jsxRuntime.jsx)(_im.ImUngroup, {}),
    description: "None",
    value: "none",
    labelValue: null,
    key: "none"
  },
  labels: {
    id: "labels",
    name: "labels",
    icon: /*#__PURE__*/(0, _jsxRuntime.jsx)(_bi.BiLabel, {}),
    description: "Labels",
    value: "labels",
    labelValue: null,
    key: "labels"
  },
  namespace: {
    id: "namespace",
    name: "namespace",
    icon: /*#__PURE__*/(0, _jsxRuntime.jsx)(_ti.TiSortAlphabeticallyOutline, {}),
    description: "Namespace",
    value: "namespace",
    labelValue: null,
    key: "namespace"
  }
};
exports.defaultPivotSelections = defaultPivotSelections;
var defaultTabSelections = {
  namespace: {
    id: "namespace",
    name: "namespace",
    icon: /*#__PURE__*/(0, _jsxRuntime.jsx)(_ti.TiSortAlphabeticallyOutline, {}),
    description: "Namespace",
    value: "namespace",
    labelValue: null,
    key: "namespace"
  }
};
exports.defaultTabSelections = defaultTabSelections;
var defaultCellTypeSelections = {
  uptime: {
    id: "uptime",
    name: "uptime",
    icon: /*#__PURE__*/(0, _jsxRuntime.jsx)(_im.ImUngroup, {}),
    description: "Uptime",
    value: "uptime",
    labelValue: null,
    key: "uptime"
  },
  checkStatuses: {
    id: "checkStatuses",
    name: "checkStatuses",
    icon: /*#__PURE__*/(0, _jsxRuntime.jsx)(_im.ImUngroup, {}),
    description: "Health",
    value: "checkStatuses",
    labelValue: null,
    key: "checkStatuses"
  },
  latency: {
    id: "latency",
    name: "latency",
    icon: /*#__PURE__*/(0, _jsxRuntime.jsx)(_im.ImUngroup, {}),
    description: "Latency",
    value: "latency",
    labelValue: null,
    key: "latency"
  }
};
exports.defaultCellTypeSelections = defaultCellTypeSelections;

function getNonBooleanLabels(checks) {
  var nonBooleanLabels = checks.reduce(function (acc, check) {
    if (check && typeof check !== "string" && "labels" in check && check.labels !== null) {
      var labels = Object.entries(check.labels).reduce(function (accum, _ref) {
        var _ref2 = _slicedToArray(_ref, 2),
            k = _ref2[0],
            v = _ref2[1];

        var id = "canary:".concat(k, ":").concat(v);

        if (typeof v === "boolean" || v === "true" || v === "false") {
          return accum;
        }

        accum[id] = {
          key: k,
          value: v
        };
        return accum;
      }, {});
      return _objectSpread(_objectSpread({}, acc), labels);
    }

    return acc;
  }, {});
  return nonBooleanLabels;
}

function getUniqueNonBooleanLabelKeys(checks) {
  var nonBooleanLabels = getNonBooleanLabels(Object.values(checks));
  var uniqueLabels = new Set(Object.values(nonBooleanLabels).map(function (o) {
    return o.key;
  }));
  var uniqueLabelsWithValues = Array.from(uniqueLabels).reduce(function (acc, k) {
    acc[k] = Object.entries(nonBooleanLabels).filter(function (label) {
      return label[1].key === k;
    }).map(function (label) {
      return label[1].value;
    });
    return acc;
  }, {});
  return uniqueLabelsWithValues;
} // provide a list of tab selections that includes (non-boolean) labels, given a list of checks.


function getTabSelections(checks, defaultTabSelections) {
  var uniqueLabelKeys = getUniqueNonBooleanLabelKeys(checks);
  var newTabSelections = defaultTabSelections;
  Object.entries(uniqueLabelKeys).sort(function (a, b) {
    return b[0] > a[0] ? -1 : 1;
  }).forEach(function (o) {
    var k = o[0];
    var onlyAlphabets = k.replace(/[^a-zA-Z]/g, "");
    newTabSelections[k] = {
      id: k,
      name: onlyAlphabets,
      icon: /*#__PURE__*/(0, _jsxRuntime.jsx)(_bi.BiLabel, {}),
      description: k,
      value: k,
      labelValue: k,
      key: k
    };
  });
  return newTabSelections;
}

function getBooleanLabels(checks) {
  var booleanLabels = checks.reduce(function (acc, check) {
    if (check && typeof check !== "string" && "labels" in check && check.labels !== null) {
      var labels = Object.entries(check.labels).reduce(function (accum, _ref3) {
        var _ref4 = _slicedToArray(_ref3, 2),
            k = _ref4[0],
            v = _ref4[1];

        var id = "canary:".concat(k, ":").concat(v);

        if (!(typeof v === "boolean" || v === "true" || v === "false")) {
          return accum;
        }

        accum[id] = {
          key: k,
          value: v
        };
        return accum;
      }, {});
      return _objectSpread(_objectSpread({}, acc), labels);
    }

    return acc;
  }, {});
  return booleanLabels;
}