"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.CanaryStatus = CanaryStatus;
exports.toFormattedDuration = toFormattedDuration;
exports.Duration = Duration;
exports.Latency = Latency;
exports.Percentage = Percentage;
exports.Title = Title;
exports.Uptime = Uptime;
exports.StatusList = StatusList;
exports.empty = void 0;

var _Icon = require("../Icon");

var _Status = require("../Status");

var _utils = require("./utils");

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

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; }

var empty = /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
  className: "text-gray-500 text-light text-xs",
  children: "-"
});
exports.empty = empty;

function CanaryStatus(_ref) {
  var status = _ref.status,
      className = _ref.className;

  if (status.mixed) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Status.Status, {
      mixed: status.mixed,
      className: className
    });
  }

  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Status.Status, {
    good: status.status,
    className: className
  });
}

function toFormattedDuration(ms) {
  if (ms == null || ms === 0) {
    return ["", ""];
  }

  try {
    ms = Number(ms);
  } catch (e) {
    // eslint-disable-next-line no-console
    console.error("invalid number format", ms);
    return ["", ""];
  }

  var val;
  var unit;

  if (ms < 1000) {
    val = ms.toFixed();
    unit = "ms";
  } else if (ms < 1000 * 60) {
    val = ms / 1000;
    unit = "s";
  } else if (ms < 1000 * 60 * 60) {
    val = ms / 1000 / 60;
    unit = "m";
  } else {
    val = ms / 1000 / 60 / 6;
    unit = "h";
  }

  if (val != null && Math.round(val) !== val) {
    val = Number(val).toFixed(0);
  }

  return [val, unit];
}

function Duration(_ref2) {
  var ms = _ref2.ms;

  var _toFormattedDuration = toFormattedDuration(ms),
      _toFormattedDuration2 = _slicedToArray(_toFormattedDuration, 2),
      val = _toFormattedDuration2[0],
      unit = _toFormattedDuration2[1];

  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
      className: "text-md",
      children: val
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
      className: "text-gray-500 text-light text-xs ml-0.5",
      children: unit
    })]
  });
}

function Latency(_ref3) {
  var check = _ref3.check;

  if (check == null || check.latency == null) {
    return empty;
  }

  if ((0, _utils.isEmpty)(check.latency.p95)) {
    return "";
  }

  return /*#__PURE__*/(0, _jsxRuntime.jsx)(Duration, {
    ms: check.latency.p95
  });
}

function Percentage(_ref4) {
  var val = _ref4.val,
      upper = _ref4.upper,
      lower = _ref4.lower;

  if ((0, _utils.isEmpty)(lower) && (0, _utils.isEmpty)(val) || upper === 0 && lower === 0) {
    return empty;
  }

  if (upper != null && lower != null) {
    val = lower / upper * 100;
  }

  if (val != null && Math.round(val) !== val) {
    try {
      val = Number(val).toFixed(1);
    } catch (e) {
      return empty;
    }
  }

  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
      className: "text-md",
      title: "".concat(lower, "/").concat(upper),
      children: val
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
      className: "text-gray-500 text-light text-xs ml-0.5",
      children: "%"
    })]
  });
}

function Title(_ref5) {
  var icon = _ref5.icon,
      title = _ref5.title;
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [icon && /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
      className: "w-6 flex-shrink-0 mr-2",
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Icon.Icon, {
        name: icon,
        className: "inline",
        size: "xl"
      })
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
      className: "text-sm",
      children: title
    })]
  });
}

function Uptime(_ref6) {
  var check = _ref6.check;

  if (check == null || check.uptime == null) {
    return "";
  }

  return /*#__PURE__*/(0, _jsxRuntime.jsx)(Percentage, {
    upper: check.uptime.passed + check.uptime.failed,
    lower: check.uptime.passed
  });
}

function StatusList(_ref7) {
  var check = _ref7.check,
      checkStatuses = _ref7.checkStatuses;

  if (check && check.checkStatuses && check.checkStatuses.length > 0) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
      children: check.checkStatuses.map(function (status, idx) {
        return (
          /*#__PURE__*/
          // Can't think of stable keys for this
          // eslint-disable-next-line react/no-array-index-key
          (0, _jsxRuntime.jsx)(CanaryStatus, {
            status: status,
            className: "mr-0.5"
          }, idx)
        );
      })
    });
  }

  if (checkStatuses && checkStatuses.length > 0) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
      children: checkStatuses.map(function (status, idx) {
        return (
          /*#__PURE__*/
          // Can't think of stable keys for this
          // eslint-disable-next-line react/no-array-index-key
          (0, _jsxRuntime.jsx)(CanaryStatus, {
            status: status,
            className: "mr-0.5"
          }, idx)
        );
      })
    });
  }

  return empty;
}