"use strict";

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

var _react = _interopRequireDefault(require("react"));

var _hooks = require("../../../utils/hooks");

var _Badge = require("../../Badge");

var _AccordionBox = require("../../AccordionBox");

var _common = require("../../../utils/common");

var _mixinsModule = _interopRequireDefault(require("../../../utils/mixins.module.css"));

var _tabs = require("./tabs");

var _CheckStat = require("./CheckStat");

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

var _StatusHistory = require("./StatusHistory");

var _DetailField = require("./DetailField");

var _CanaryStatusChart = require("../CanaryStatusChart");

var _renderers = require("../renderers");

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

var _excluded = ["check"];

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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 _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 _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function CheckDetails(_ref) {
  var _validCheck$uptime, _validCheck$uptime2, _validCheck$uptime3, _validCheck$uptime4;

  var check = _ref.check,
      rest = _objectWithoutProperties(_ref, _excluded);

  var prevCheck = (0, _hooks.usePrevious)(check);
  var validCheck = check || prevCheck;

  if (validCheck == null) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {});
  }

  var uptimeValue = (0, _common.toFixedIfNecessary)((0, _utils.getUptimePercentage)(validCheck), 0);
  var validUptime = !Number.isNaN(validCheck === null || validCheck === void 0 ? void 0 : (_validCheck$uptime = validCheck.uptime) === null || _validCheck$uptime === void 0 ? void 0 : _validCheck$uptime.passed) && !Number.isNaN(validCheck === null || validCheck === void 0 ? void 0 : (_validCheck$uptime2 = validCheck.uptime) === null || _validCheck$uptime2 === void 0 ? void 0 : _validCheck$uptime2.failed);
  var severityValue = (validCheck === null || validCheck === void 0 ? void 0 : validCheck.severity) || "-";
  var statusHistoryList = validCheck === null || validCheck === void 0 ? void 0 : validCheck.checkStatuses;
  var details = {
    Name: (validCheck === null || validCheck === void 0 ? void 0 : validCheck.name) || (validCheck === null || validCheck === void 0 ? void 0 : validCheck.canaryName) || (validCheck === null || validCheck === void 0 ? void 0 : validCheck.endpoint) || "-",
    Type: (validCheck === null || validCheck === void 0 ? void 0 : validCheck.type) || "-",
    Labels: /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
      children: (validCheck === null || validCheck === void 0 ? void 0 : validCheck.labels) && Object.entries(validCheck === null || validCheck === void 0 ? void 0 : validCheck.labels).map(function (entry) {
        var key = entry[0];
        var value = entry[1];

        if (value === "true" || value === true) {
          value = "";
        }

        return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Badge.Badge, {
          className: "mr-1 mb-1",
          text: key,
          value: value
        }, key);
      })
    }),
    Owner: (validCheck === null || validCheck === void 0 ? void 0 : validCheck.owner) || "-",
    Interval: (validCheck === null || validCheck === void 0 ? void 0 : validCheck.interval) || "-",
    Location: (validCheck === null || validCheck === void 0 ? void 0 : validCheck.location) || "-",
    Schedule: (validCheck === null || validCheck === void 0 ? void 0 : validCheck.schedule) || "-"
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", _objectSpread(_objectSpread({}, rest), {}, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: "flex flex-row flex-wrap mb-2",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_CheckStat.CheckStat, {
        containerClass: "w-52 mb-4",
        title: "Uptime",
        value: !Number.isNaN(uptimeValue) ? "".concat((0, _common.toFixedIfNecessary)(uptimeValue, 2), "%") : "-",
        append: validUptime && !Number.isNaN(uptimeValue) && /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
          className: "flex flex-col justify-center mx-2 mt-0.5",
          children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("span", {
            className: "text-xs text-green-700 ",
            children: [validCheck === null || validCheck === void 0 ? void 0 : (_validCheck$uptime3 = validCheck.uptime) === null || _validCheck$uptime3 === void 0 ? void 0 : _validCheck$uptime3.passed, " passed"]
          }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("span", {
            className: "text-xs text-red-600",
            children: [validCheck === null || validCheck === void 0 ? void 0 : (_validCheck$uptime4 = validCheck.uptime) === null || _validCheck$uptime4 === void 0 ? void 0 : _validCheck$uptime4.failed, " failed"]
          })]
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_CheckStat.CheckStat, {
        containerClass: "w-40 mb-4",
        title: "Latency (95%)",
        value: /*#__PURE__*/(0, _jsxRuntime.jsx)(_renderers.Duration, {
          ms: validCheck.latency.p95
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_CheckStat.CheckStat, {
        containerClass: "w-40 mb-4",
        title: "Latency  (97%)",
        value: /*#__PURE__*/(0, _jsxRuntime.jsx)(_renderers.Duration, {
          ms: validCheck.latency.p97
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_CheckStat.CheckStat, {
        containerClass: "w-40 mb-4",
        title: "Latency  (99%)",
        value: /*#__PURE__*/(0, _jsxRuntime.jsx)(_renderers.Duration, {
          ms: validCheck.latency.p99
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_CheckStat.CheckStat, {
        containerClass: "w-40 mb-4",
        title: "Severity",
        value: (0, _common.capitalizeFirstLetter)(severityValue)
      })]
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: "mb-3",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: "flex justify-between items-center mb-2",
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
          className: "text-lg font-medium",
          children: "Health overview"
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: "w-full h-52 overflow-visible",
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_CanaryStatusChart.CanaryStatusChart, {
          check: validCheck
        })
      })]
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_tabs.PopupTabs, {
      shareHeight: true,
      style: {
        display: "flex",
        flexDirection: "column",
        overflowY: "hidden"
      },
      contentStyle: {
        marginTop: "-1px",
        display: "flex",
        flexDirection: "column",
        overflowY: "hidden",
        overflowX: "hidden"
      },
      tabs: {
        statusHistory: {
          label: "Status history",
          content: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
            className: "border border-b-0 border-gray-300 bg-gray-50 overflow-hidden overflow-y-auto h-full relative -mb-px ".concat(_mixinsModule.default.appleScrollbar),
            children: statusHistoryList && statusHistoryList.length > 0 ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_StatusHistory.StatusHistory, {
              check: validCheck,
              sticky: true
            }) : /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
              className: "h-64 flex items-center justify-center text-gray-400 text-md",
              children: "No status history available"
            })
          }, "status-history"),
          class: "flex flex-col overflow-y-hidden border-b h-full border-gray-300"
        },
        checkDetails: {
          label: "Check details",
          content: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
            className: "px-6 py-6",
            children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_AccordionBox.AccordionBox, {
              content: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
                className: "flex flex-row flex-wrap",
                children: Object.entries(details).map(function (_ref2) {
                  var _ref3 = _slicedToArray(_ref2, 2),
                      label = _ref3[0],
                      value = _ref3[1];

                  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_DetailField.DetailField, {
                    className: "w-1/2 mb-3 whitespace-nowrap",
                    label: label,
                    value: value
                  }, label);
                })
              })
            })
          }, "check-details"),
          class: "flex flex-col overflow-y-auto  border border-gray-300 ".concat(_mixinsModule.default.appleScrollbar)
        }
      }
    })]
  }));
}