import "choerodon-ui/pro/lib/icon/style";
import _Icon from "choerodon-ui/pro/lib/icon";

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

import React, { Fragment } from 'react';
import { observer } from 'mobx-react-lite';
import TreeItemName from "../../../../../components/treeitem-name";
import EnvItem from "../../../../../components/env-item";
import { useResourceStore } from "../../../stores";
import { useMainStore } from "../../stores";
import InstanceItem from "./InstanceItem";
import AppItem from "./AppItem";
import NetworkItem from "./NetworkItem";
import CustomItem from "./CustomItem";
import CertItem from "./CertItem";
import IngressItem from "./IngressItem";
import ConfigItem from "./ConfigItem";
import SecretItem from "./SecretItem";
var TreeItem = observer(function (_ref) {
  var record = _ref.record,
      search = _ref.search;

  var _useResourceStore = useResourceStore(),
      intlPrefix = _useResourceStore.intlPrefix,
      _useResourceStore$ite = _useResourceStore.itemTypes,
      ENV_ITEM = _useResourceStore$ite.ENV_ITEM,
      APP_ITEM = _useResourceStore$ite.APP_ITEM,
      IST_ITEM = _useResourceStore$ite.IST_ITEM,
      SERVICES_ITEM = _useResourceStore$ite.SERVICES_ITEM,
      INGRESS_ITEM = _useResourceStore$ite.INGRESS_ITEM,
      CERT_ITEM = _useResourceStore$ite.CERT_ITEM,
      MAP_ITEM = _useResourceStore$ite.MAP_ITEM,
      CIPHER_ITEM = _useResourceStore$ite.CIPHER_ITEM,
      CUSTOM_ITEM = _useResourceStore$ite.CUSTOM_ITEM;

  var _useMainStore = useMainStore(),
      podColor = _useMainStore.podColor;

  function getNormalItem(type, param) {
    var _mappings;

    var mappings = (_mappings = {}, _defineProperty(_mappings, SERVICES_ITEM, function () {
      return /*#__PURE__*/React.createElement(NetworkItem, param);
    }), _defineProperty(_mappings, INGRESS_ITEM, function () {
      return /*#__PURE__*/React.createElement(IngressItem, param);
    }), _defineProperty(_mappings, CERT_ITEM, function () {
      return /*#__PURE__*/React.createElement(CertItem, param);
    }), _defineProperty(_mappings, MAP_ITEM, function () {
      return /*#__PURE__*/React.createElement(ConfigItem, param);
    }), _defineProperty(_mappings, CIPHER_ITEM, function () {
      return /*#__PURE__*/React.createElement(SecretItem, param);
    }), _defineProperty(_mappings, CUSTOM_ITEM, function () {
      return /*#__PURE__*/React.createElement(CustomItem, param);
    }), _mappings);
    return mappings[type]();
  }

  function getItem() {
    var itemName = record.get('name');
    var name = /*#__PURE__*/React.createElement(TreeItemName, {
      name: itemName,
      search: search
    });
    var isExpand = record.isExpanded;
    var isGroup = record.get('isGroup');

    if (isGroup) {
      return /*#__PURE__*/React.createElement(React.Fragment, null, isExpand ? /*#__PURE__*/React.createElement(_Icon, {
        type: "folder_open2"
      }) : /*#__PURE__*/React.createElement(_Icon, {
        type: "folder_open"
      }), name);
    }

    var type = record.get('itemType');
    var param = {
      record: record,
      name: name,
      intlPrefix: intlPrefix
    };
    var treeItem;

    switch (type) {
      case ENV_ITEM:
        {
          treeItem = /*#__PURE__*/React.createElement(EnvItem, {
            name: name,
            connect: record.get('connect')
          });
          break;
        }

      case SERVICES_ITEM:
      case INGRESS_ITEM:
      case CERT_ITEM:
      case MAP_ITEM:
      case CIPHER_ITEM:
      case CUSTOM_ITEM:
        treeItem = getNormalItem(type, param);
        break;

      case IST_ITEM:
        {
          treeItem = /*#__PURE__*/React.createElement(InstanceItem, Object.assign({}, param, {
            podColor: podColor
          }));
          break;
        }

      case APP_ITEM:
        treeItem = /*#__PURE__*/React.createElement(AppItem, param);
        break;

      default:
        treeItem = null;
    }

    return treeItem;
  }

  return getItem();
});
TreeItem.defaultProps = {
  record: {}
};
export default TreeItem;