"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

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

var _clsx = _interopRequireDefault(require("clsx"));

var _lodash = require("lodash");

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

var _reactRouterDom = require("react-router-dom");

var _topology2 = require("../../api/services/topology");

var _HealthSummary = require("../HealthSummary");

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

var _Loading = require("../Loading");

require("./index.css");

var _metrics = require("./metrics");

var _property = require("./property");

var _topologyMenu = require("./topology-menu");

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { 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; }

var CARD_SIZE = {
  SMALL: "small",
  MEDIUM: "medium",
  LARGE: "large",
  EXTRA_LARGE: "extra-large"
};

function TopologyCard(_ref) {
  var size = _ref.size,
      topology = _ref.topology,
      topologyId = _ref.topologyId,
      selectionMode = _ref.selectionMode,
      depth = _ref.depth,
      selected = _ref.selected,
      onSelectionChange = _ref.onSelectionChange;

  var _useState = (0, _react.useState)(topology),
      _useState2 = _slicedToArray(_useState, 2),
      _topology = _useState2[0],
      setTopology = _useState2[1];

  (0, _react.useEffect)(function () {
    if (topologyId != null && _topology == null) {
      (0, _topology2.getTopology)({
        id: topologyId,
        depth: depth
      }).then(function (topology) {
        setTopology(topology.data[0]);
      });
    }
  });
  var selectionModeRootProps = null;

  if (selectionMode) {
    selectionModeRootProps = {
      role: "button",
      onClick: function onClick(state, event) {
        onSelectionChange(state, event);
      }
    };
  }

  var metricsInFooter = (0, _react.useMemo)(function () {
    return size === CARD_SIZE.SMALL || size === CARD_SIZE.MEDIUM;
  }, [size]);

  if (_topology == null) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Loading.Loading, {
      text: "Loading ".concat(topologyId)
    });
  }

  _topology.properties = _topology.properties || [];
  var properties = (0, _lodash.filter)(_topology.properties, function (i) {
    return !i.headline;
  });
  var heading = (0, _lodash.filter)(_topology.properties, function (i) {
    return i.headline;
  });
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", _objectSpread(_objectSpread({
    className: (0, _clsx.default)("rounded-8px mb-3 mr-3 shadow-card card topology-card bg-lightest-gray", _topology.status, selectionMode ? "cursor-pointer" : "", "topology-card-".concat(size))
  }, selectionModeRootProps), {}, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: "flex flex-row flex-nowrap topology-card-header rounded-t-8px bg-white",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
        className: "flex pr-1 pt-2.5 pb-3.5 pl-5 overflow-hidden",
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: "text-gray-color m-auto mr-2.5 flex-initial max-w-1/4 leading-1.21rel",
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)("h3", {
            className: "text-gray-color text-2xsi leading-1.21rel",
            children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Icon.Icon, {
              name: _topology.icon,
              size: "2xl"
            })
          })
        }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
          className: "flex-1 m-auto overflow-hidden",
          children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("p", {
            className: "font-bold overflow-hidden truncate align-middle text-15pxinrem leading-1.21rel",
            title: _topology.name,
            children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactRouterDom.Link, {
              to: "/topology/".concat(_topology.id),
              children: _topology.text || _topology.name
            })
          }), _topology.description != null || _topology.id != null && /*#__PURE__*/(0, _jsxRuntime.jsx)("h3", {
            className: "text-gray-color overflow-hidden truncate text-2xsi leading-1.21rel font-medium",
            children: _topology.description || _topology.id
          })]
        })]
      }), !metricsInFooter && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: "flex ml-auto pl-1 pr-1.5 pb-3.5 pt-3",
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_metrics.Metrics, {
          items: heading,
          row: size === CARD_SIZE.EXTRA_LARGE
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: "flex ml-auto pl-1 pr-1.5 pb-3.5 pt-3",
        children: selectionMode ? /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: "pr-1.5 pt-1 flex min-w-7 justify-end items-start",
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)("input", {
            type: "checkbox",
            className: "h-4 w-4 text-dark-blue outline-none rounded-4px focus:outline-none",
            checked: selected,
            readOnly: true
          })
        }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_topologyMenu.TopologyDropdownMenu, {
          topology: _topology
        })
      })]
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: "flex flex-nowrap bg-lightest-gray rounded-b-8px",
      children: metricsInFooter ? /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: "flex py-4 flex-1",
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_metrics.Metrics, {
          items: heading
        })
      }) : /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: "w-med-card-left py-4 pl-5 pr-1 overflow-auto",
          children: properties.map(function (property, index) {
            return /*#__PURE__*/(0, _jsxRuntime.jsx)(_property.Property, {
              property: property,
              className: index === _topology.properties.length - 1 ? "mb-0" : "mb-2.5"
            }, property.name);
          })
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: "w-med-card-right pl-1 py-4 pr-5 overflow-y-auto",
          children: _topology.components && _topology.components.map(function (component, index) {
            return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
              className: index === _topology.components.length - 1 ? "mb-0" : "mb-2.5",
              children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_HealthSummary.HealthSummary, {
                component: component
              })
            }, component.id);
          })
        })]
      })
    })]
  }));
} // TopologyCard.propTypes = {
//   size: PropTypes.string,
//   selectionMode: PropTypes.bool,
//   selected: PropTypes.bool,
//   onSelectionChange: PropTypes.func,
//   topologyId: PropTypes.string,
//   topology: PropTypes.shape({
//     name: PropTypes.string,
//     status: PropTypes.string,
//     icon: PropTypes.string,
//     properties: PropTypes.arrayOf(
//       PropTypes.shape({
//         name: PropTypes.string,
//         text: PropTypes.string
//         value
//       }).isRequired
//     )
//   })
// };
// TopologyCard.defaultProps = {
//   selectionMode: false,
//   selected: false,
//   topology: null,
//   topologyId: null,
//   size: "md",
//   onSelectionChange: () => { }
// };