import "choerodon-ui/lib/icon/style";
import _Icon from "choerodon-ui/lib/icon";
import "choerodon-ui/lib/collapse/style";
import _Collapse from "choerodon-ui/lib/collapse";
import React, { useState, Fragment, Suspense, useMemo } from 'react';
import { observer } from 'mobx-react-lite';
import map from 'lodash/map';
import isEmpty from 'lodash/isEmpty';
import { useEnvironmentStore } from "../../stores";
import { useResourceStore } from "../../../../../stores";
import "./index.less";
var Panel = _Collapse.Panel;
var collapseDetail = observer(function (_ref) {
  var loading = _ref.loading;

  var _useResourceStore = useResourceStore(),
      prefixCls = _useResourceStore.prefixCls;

  var _useEnvironmentStore = useEnvironmentStore(),
      formatMessage = _useEnvironmentStore.intl.formatMessage,
      istSummaryDs = _useEnvironmentStore.istSummaryDs,
      intlPrefix = _useEnvironmentStore.intlPrefix;

  var isLoading = useMemo(function () {
    return loading || istSummaryDs.status === 'loading';
  }, [loading, istSummaryDs.status]);

  function getIstHeader(record) {
    var _ref2 = record.toData() || {},
        instanceCode = _ref2.instanceCode,
        appServiceName = _ref2.appServiceName,
        hasErrors = _ref2.hasErrors;

    return /*#__PURE__*/React.createElement("div", {
      className: "".concat(prefixCls, "-polaris-tabs-header")
    }, /*#__PURE__*/React.createElement("div", {
      className: "".concat(prefixCls, "-polaris-tabs-header-item")
    }, /*#__PURE__*/React.createElement("span", {
      className: "".concat(prefixCls, "-polaris-tabs-header-text")
    }, formatMessage({
      id: 'instance'
    }), ":"), /*#__PURE__*/React.createElement("span", null, instanceCode), !isLoading && hasErrors && /*#__PURE__*/React.createElement(_Icon, {
      type: "cancel",
      className: "".concat(prefixCls, "-polaris-tabs-header-error")
    })), /*#__PURE__*/React.createElement("div", {
      className: "".concat(prefixCls, "-polaris-tabs-header-item")
    }, /*#__PURE__*/React.createElement("span", {
      className: "".concat(prefixCls, "-polaris-tabs-header-text")
    }, formatMessage({
      id: 'appService'
    }), ":"), /*#__PURE__*/React.createElement("span", null, appServiceName)));
  }

  function getIstContent(record) {
    var checked = record.get('checked');
    var items = record.get('items');

    if (isLoading) {
      return /*#__PURE__*/React.createElement("span", {
        className: "".concat(prefixCls, "-polaris-tabs-content")
      }, formatMessage({
        id: 'c7ncd.cluster.polaris.check.operating'
      }));
    }

    if (!checked) {
      return /*#__PURE__*/React.createElement("span", {
        className: "".concat(prefixCls, "-polaris-tabs-content")
      }, formatMessage({
        id: 'c7ncd.cluster.polaris.check.null'
      }));
    }

    if (isEmpty(items)) {
      return /*#__PURE__*/React.createElement("span", {
        className: "".concat(prefixCls, "-polaris-tabs-content")
      }, formatMessage({
        id: "".concat(intlPrefix, ".polaris.check.empty")
      }));
    }

    return map(items, function (_ref3, index) {
      var hasErrors = _ref3.hasErrors,
          kind = _ref3.kind,
          name = _ref3.name,
          podResult = _ref3.podResult;
      var containers = podResult ? podResult.containerResults : [];
      var results = podResult ? podResult.results : {};
      return /*#__PURE__*/React.createElement("div", {
        key: "".concat(name, "-").concat(index),
        className: "".concat(prefixCls, "-polaris-tabs-content")
      }, /*#__PURE__*/React.createElement("div", {
        className: "".concat(prefixCls, "-polaris-tabs-content-title-weight")
      }, /*#__PURE__*/React.createElement("span", null, "".concat(kind, ": ").concat(name)), hasErrors && /*#__PURE__*/React.createElement(_Icon, {
        type: "cancel",
        className: "".concat(prefixCls, "-polaris-tabs-header-error")
      })), /*#__PURE__*/React.createElement("div", {
        className: "".concat(prefixCls, "-polaris-tabs-content-title")
      }, /*#__PURE__*/React.createElement("span", null, "Pod Spec:")), isEmpty(results) ? /*#__PURE__*/React.createElement("div", {
        className: "".concat(prefixCls, "-polaris-tabs-content-env")
      }, /*#__PURE__*/React.createElement(_Icon, {
        type: "done",
        className: "".concat(prefixCls, "-polaris-tabs-content-icon-success")
      }), /*#__PURE__*/React.createElement("span", null, formatMessage({
        id: 'c7ncd.cluster.polaris.check.success'
      }))) : map(results, function (_ref4, resultIndex) {
        var message = _ref4.message,
            type = _ref4.type,
            severity = _ref4.severity;
        return /*#__PURE__*/React.createElement("div", {
          key: "".concat(type, "-").concat(resultIndex),
          className: "".concat(prefixCls, "-polaris-tabs-content-des")
        }, /*#__PURE__*/React.createElement(_Icon, {
          type: severity === 'warning' ? 'priority_high' : 'close',
          className: "".concat(prefixCls, "-polaris-tabs-content-icon-").concat(severity)
        }), /*#__PURE__*/React.createElement("span", null, message));
      }), map(containers, function (_ref5) {
        var containerName = _ref5.name,
            containerResults = _ref5.results;
        return /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement("div", {
          className: "".concat(prefixCls, "-polaris-tabs-content-title"),
          key: containerName
        }, /*#__PURE__*/React.createElement("span", null, "Container: ".concat(containerName))), isEmpty(containerResults) ? /*#__PURE__*/React.createElement("div", {
          className: "".concat(prefixCls, "-polaris-tabs-content-env")
        }, /*#__PURE__*/React.createElement(_Icon, {
          type: "done",
          className: "".concat(prefixCls, "-polaris-tabs-content-icon-success")
        }), /*#__PURE__*/React.createElement("span", null, formatMessage({
          id: 'c7ncd.cluster.polaris.check.success'
        }))) : map(containerResults, function (_ref6, containerIndex) {
          var message = _ref6.message,
              type = _ref6.type,
              severity = _ref6.severity;
          return /*#__PURE__*/React.createElement("div", {
            key: "".concat(type, "-").concat(containerIndex),
            className: "".concat(prefixCls, "-polaris-tabs-content-des")
          }, /*#__PURE__*/React.createElement(_Icon, {
            type: severity === 'warning' ? 'priority_high' : 'close',
            className: "".concat(prefixCls, "-polaris-tabs-content-icon-").concat(severity)
          }), /*#__PURE__*/React.createElement("span", null, message));
        }));
      }));
    });
  }

  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-polaris-tabs")
  }, /*#__PURE__*/React.createElement(_Collapse, {
    bordered: false
  }, map(istSummaryDs.data, function (istRecord) {
    return /*#__PURE__*/React.createElement(Panel, {
      header: getIstHeader(istRecord),
      key: istRecord.id
    }, getIstContent(istRecord));
  })));
});
export default collapseDetail;