import "choerodon-ui/pro/lib/tooltip/style";
import _Tooltip from "choerodon-ui/pro/lib/tooltip";
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; }

/* eslint-disable max-len */
import React, { Fragment, useMemo } from 'react';
import { observer } from 'mobx-react-lite';
import toUpper from 'lodash/toUpper';
import { Action } from '@choerodon/boot';
import ClusterItem from "./ClusterItem";
import { useClusterStore } from "../../../stores";
import { useClusterMainStore } from "../../stores";
import "./index.less";

function getName(name, search, prefixCls, errorMes) {
  var index = toUpper(name).indexOf(toUpper(search));
  var beforeStr = name.substr(0, index);
  var currentStr = name.substr(index, search.length);
  var afterStr = name.substr(index + search.length);
  return /*#__PURE__*/React.createElement("span", {
    className: "".concat(prefixCls, "-tree-text")
  }, index > -1 ? /*#__PURE__*/React.createElement(React.Fragment, null, beforeStr, /*#__PURE__*/React.createElement("span", {
    className: "".concat(prefixCls, "-tree-text-highlight")
  }, currentStr), afterStr) : name, errorMes && /*#__PURE__*/React.createElement(_Tooltip, {
    title: errorMes
  }, /*#__PURE__*/React.createElement(_Icon, {
    type: "info",
    style: {
      color: '#f77a70',
      marginLeft: '8px',
      fontSize: '15px'
    }
  })));
}

var TreeItem = observer(function (_ref) {
  var record = _ref.record,
      search = _ref.search;

  var _useClusterStore = useClusterStore(),
      prefixCls = _useClusterStore.prefixCls,
      intlPrefix = _useClusterStore.intlPrefix,
      _useClusterStore$item = _useClusterStore.itemType,
      CLU_ITEM = _useClusterStore$item.CLU_ITEM,
      NODE_ITEM = _useClusterStore$item.NODE_ITEM;

  var _useClusterMainStore = useClusterMainStore(),
      podColor = _useClusterMainStore.podColor;

  var name = useMemo(function () {
    var itemName = record.get('name');
    var errorMes = record.get('errorMessage');
    return getName(itemName, search, prefixCls, errorMes);
  }, [record, search]);

  function getIconItem(type) {
    var iconMappings = _defineProperty({}, NODE_ITEM, 'adjust');

    var iconType = iconMappings[type];
    return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(_Icon, {
      type: iconType
    }), name);
  }

  function getCluIcon() {
    return /*#__PURE__*/React.createElement(ClusterItem, {
      name: name,
      record: record,
      intlPrefix: intlPrefix,
      prefixCls: prefixCls
    });
  }

  function getItem() {
    var type = record.get('itemType');
    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 treeItem;

    switch (type) {
      case CLU_ITEM:
        treeItem = getCluIcon();
        break;

      case NODE_ITEM:
        treeItem = getIconItem(type);
        break;

      default:
        treeItem = null;
    }

    return /*#__PURE__*/React.createElement(React.Fragment, null, treeItem);
  }

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