import "choerodon-ui/lib/popover/style";
import _Popover from "choerodon-ui/lib/popover";
import "choerodon-ui/lib/icon/style";
import _Icon from "choerodon-ui/lib/icon";
import "choerodon-ui/lib/button/style";
import _Button from "choerodon-ui/lib/button";

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

import React, { Fragment, useMemo, useState } from 'react';
import { FormattedMessage } from 'react-intl';
import { observer } from 'mobx-react-lite';
import map from 'lodash/map';
import compact from 'lodash/compact';
import isEmpty from 'lodash/isEmpty';
import { useResourceStore } from "../../../stores";
import { useNetworkDetailStore } from "./stores";
import TimePopover from "../../../../../components/timePopover";
import LogSidebar from "../../../../../components/log-siderbar";
var PodList = observer(function () {
  var _useResourceStore = useResourceStore(),
      prefixCls = _useResourceStore.prefixCls,
      intlPrefix = _useResourceStore.intlPrefix;

  var _useNetworkDetailStor = useNetworkDetailStore(),
      baseInfoDs = _useNetworkDetailStor.baseInfoDs,
      formatMessage = _useNetworkDetailStor.intl.formatMessage;

  var record = baseInfoDs.current;

  var _useState = useState(false),
      _useState2 = _slicedToArray(_useState, 2),
      visible = _useState2[0],
      setVisible = _useState2[1];

  var _useState3 = useState(),
      _useState4 = _slicedToArray(_useState3, 2),
      data = _useState4[0],
      setData = _useState4[1];

  function openLog() {
    setVisible(true);
  }

  function closeLog() {
    setVisible(false);
  }

  function handleLogClick(index, containerIndex) {
    setData(_objectSpread(_objectSpread({}, record.get('podLiveInfos')[index]), {}, {
      containerIndex: containerIndex
    }));
    openLog();
  }

  function renderRegistry(containers, index) {
    var list = map(containers, function (_ref, containerIndex) {
      var name = _ref.name,
          ready = _ref.ready,
          registry = _ref.registry;
      return /*#__PURE__*/React.createElement("div", {
        key: name
      }, /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("span", null, name), /*#__PURE__*/React.createElement(_Button, {
        icon: "find_in_page",
        shape: "circle",
        onClick: function onClick() {
          handleLogClick(index, containerIndex);
        }
      })), /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("span", {
        className: "service-detail-pod-item-key"
      }, formatMessage({
        id: "".concat(intlPrefix, ".registry")
      }), ":", registry)));
    });
    return /*#__PURE__*/React.createElement("div", {
      className: "service-detail-pod-registry"
    }, list[0], list.length > 1 && /*#__PURE__*/React.createElement(_Popover, {
      content: list
    }, /*#__PURE__*/React.createElement(_Icon, {
      type: "expand_more",
      className: "service-detail-pod-item-icon"
    })));
  }

  return /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-detail-content-section-title")
  }, /*#__PURE__*/React.createElement(FormattedMessage, {
    id: "".concat(intlPrefix, ".pods")
  })), /*#__PURE__*/React.createElement("div", {
    className: "detail-content-section-detail"
  }, !isEmpty(record.get('podLiveInfos')) ? map(compact(record.get('podLiveInfos')), function (_ref2, index) {
    var containers = _ref2.containers,
        cpuUsedList = _ref2.cpuUsedList,
        memoryUsedList = _ref2.memoryUsedList,
        timeList = _ref2.timeList,
        nodeIp = _ref2.nodeIp,
        nodeName = _ref2.nodeName,
        podName = _ref2.podName,
        podId = _ref2.podId,
        podIp = _ref2.podIp,
        creationDate = _ref2.creationDate;
    return /*#__PURE__*/React.createElement("ul", {
      className: "service-detail-pod-list",
      key: podId
    }, /*#__PURE__*/React.createElement("li", {
      className: "service-detail-pod-item"
    }, /*#__PURE__*/React.createElement("span", {
      className: "service-detail-pod-item-name"
    }, podName), /*#__PURE__*/React.createElement("span", {
      className: "service-detail-pod-item-time"
    }, /*#__PURE__*/React.createElement(TimePopover, {
      content: creationDate
    }))), /*#__PURE__*/React.createElement("li", {
      className: "service-detail-pod-item"
    }, /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("span", {
      className: "service-detail-pod-item-key",
      style: {
        whiteSpace: 'nowrap'
      }
    }, formatMessage({
      id: "".concat(intlPrefix, ".instance.ip")
    }), ":", podIp || '-')), /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("span", {
      className: "service-detail-pod-item-key",
      style: {
        whiteSpace: 'nowrap'
      }
    }, formatMessage({
      id: "".concat(intlPrefix, ".node")
    }), ": ", nodeIp ? "".concat(nodeName, "(").concat(nodeIp, ")") : '-'))), /*#__PURE__*/React.createElement("li", {
      className: "service-detail-pod-item"
    }, renderRegistry(containers, index)));
  }) : /*#__PURE__*/React.createElement("span", {
    style: {
      color: 'rgba(0,0,0,.65)'
    }
  }, formatMessage({
    id: 'nodata'
  }))), visible && /*#__PURE__*/React.createElement(LogSidebar, {
    visible: visible,
    onClose: closeLog,
    record: data
  }));
});
export default PodList;