import "choerodon-ui/pro/lib/modal/style";
import _Modal from "choerodon-ui/pro/lib/modal";

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

/* eslint-disable max-len */
import React, { useMemo } from 'react';
import { observer } from 'mobx-react-lite';
import { HeaderButtons } from '@choerodon/master';
import { useClusterContentStore } from "../stores";
import { useClusterStore } from "../../../../stores";
import { useClusterMainStore } from "../../../stores";
import { useModalStore } from "./stores";
import CreateCluster from "./create-cluster";
import CreateClusterByHost from "./create-clusterByHost";
import CreateNodes from "./create-nodes";
import PermissionManage from "./permission-manage";
import Tips from "../../../../../../components/new-tips";
import "../../../../../../components/dynamic-select/style/index.less";

var modalKey1 = _Modal.key();

var modalKey2 = _Modal.key();

var ClusterModals = observer(function () {
  var modalStyle = useMemo(function () {
    return {
      width: 380
    };
  }, []);

  var _useClusterStore = useClusterStore(),
      intlPrefix = _useClusterStore.intlPrefix,
      prefixCls = _useClusterStore.prefixCls,
      formatMessage = _useClusterStore.intl.formatMessage,
      id = _useClusterStore.clusterStore.getSelectedMenu.id,
      clusterStore = _useClusterStore.clusterStore,
      projectId = _useClusterStore.AppState.currentMenuType.id,
      treeDs = _useClusterStore.treeDs,
      clusterId = _useClusterStore.clusterId,
      nodesTypeDs = _useClusterStore.nodesTypeDs,
      addNodesDs = _useClusterStore.addNodesDs,
      formDs = _useClusterStore.formDs,
      nodesDs = _useClusterStore.nodesDs,
      publicNodeDs = _useClusterStore.publicNodeDs,
      createHostClusterStore = _useClusterStore.createHostClusterStore;

  var _useClusterContentSto = useClusterContentStore(),
      contentStore = _useClusterContentSto.contentStore,
      _useClusterContentSto2 = _useClusterContentSto.tabs,
      NODE_TAB = _useClusterContentSto2.NODE_TAB,
      ASSIGN_TAB = _useClusterContentSto2.ASSIGN_TAB,
      COMPONENT_TAB = _useClusterContentSto2.COMPONENT_TAB,
      MONITOR_TAB = _useClusterContentSto2.MONITOR_TAB,
      PermissionDs = _useClusterContentSto.PermissionDs,
      NodeListDs = _useClusterContentSto.NodeListDs,
      ClusterDetailDs = _useClusterContentSto.ClusterDetailDs;

  var getTabKey = contentStore.getTabKey;

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

  var _useModalStore = useModalStore(),
      modalStore = _useModalStore.modalStore,
      NonPermissionDs = _useModalStore.NonPermissionDs;

  function permissionUpdate(data) {
    var record = ClusterDetailDs.current;

    if (record) {
      var objectVersionNumber = record.get('objectVersionNumber');

      var PermissionData = _objectSpread({
        projectId: projectId,
        clusterId: id,
        objectVersionNumber: objectVersionNumber
      }, data);

      return modalStore.permissionUpdate(PermissionData);
    }

    return false;
  }

  function refresh() {
    resreshTree();
    ClusterDetailDs.query();

    switch (getTabKey) {
      case NODE_TAB:
        NodeListDs.query();
        break;

      case ASSIGN_TAB:
        PermissionDs.query();
        break;

      case COMPONENT_TAB:
        contentStore.loadComponentList(projectId, id);
        break;

      case MONITOR_TAB:
        contentStore.loadGrafanaUrl(projectId, id);
        break;

      default:
    }
  }

  function resreshTree() {
    treeDs.query();
    mainStore.checkCreate(projectId);
  }

  function refreshPermission() {
    contentStore.setTabKey(ASSIGN_TAB);
    ClusterDetailDs.query();
    NonPermissionDs.query();
  }

  function openCreateByNodes() {
    _Modal.open({
      key: _Modal.key(),
      title: formatMessage({
        id: "".concat(intlPrefix, ".modal.createByNodes")
      }),
      children: /*#__PURE__*/React.createElement(CreateNodes, {
        prefixCls: prefixCls,
        intlPrefix: intlPrefix,
        formatMessage: formatMessage,
        mainStore: mainStore,
        projectId: projectId,
        afterOk: resreshTree,
        nodesTypeDs: nodesTypeDs,
        nodesDs: addNodesDs,
        modalStore: modalStore,
        clusterId: clusterId,
        createHostClusterMainStore: createHostClusterStore
      }),
      drawer: true,
      className: "".concat(prefixCls, "-nodesCreate-modal"),
      style: {
        width: '740px'
      },
      okText: formatMessage({
        id: 'add'
      }),
      onCancel: function onCancel() {
        addNodesDs.reset();
      }
    });
  }

  var renderClusterHostTitle = function renderClusterHostTitle(title, text) {
    return /*#__PURE__*/React.createElement("div", {
      style: {
        display: 'flex',
        alignItems: 'center'
      }
    }, /*#__PURE__*/React.createElement("span", {
      style: {
        marginRight: '4px'
      }
    }, title), /*#__PURE__*/React.createElement(Tips, {
      helpText: text
    }));
  };

  function openCreateByHost() {
    _Modal.open({
      key: _Modal.key(),
      className: "".concat(prefixCls, "-createByHost-modal"),
      title: renderClusterHostTitle(formatMessage({
        id: "".concat(intlPrefix, ".modal.createByHost")
      }), '此方式通过维护节点来新建集群'),
      children: /*#__PURE__*/React.createElement(CreateClusterByHost, {
        formDs: formDs,
        afterOk: resreshTree,
        prefixCls: prefixCls,
        intlPrefix: intlPrefix,
        formatMessage: formatMessage,
        projectId: projectId,
        nodesTypeDs: nodesTypeDs,
        nodesDs: nodesDs,
        createHostClusterMainStore: createHostClusterStore,
        publicNodeDs: publicNodeDs
      }),
      drawer: true,
      style: {
        width: '740px'
      },
      destroyOnClose: true,
      okText: formatMessage({
        id: 'create'
      }),
      onCancel: function onCancel() {
        nodesDs.reset();
        formDs.reset();
      }
    });
  }

  function openCreate() {
    _Modal.open({
      key: modalKey1,
      title: renderClusterHostTitle(formatMessage({
        id: "".concat(intlPrefix, ".modal.create")
      }), '用于连接已有的k8s集群'),
      children: /*#__PURE__*/React.createElement(CreateCluster, {
        afterOk: resreshTree,
        prefixCls: prefixCls,
        intlPrefix: intlPrefix,
        formatMessage: formatMessage,
        mainStore: mainStore,
        projectId: projectId
      }),
      drawer: true,
      style: modalStyle,
      okText: formatMessage({
        id: "".concat(intlPrefix, ".modal.connect")
      })
    });
  }

  function openPermission() {
    _Modal.open({
      key: modalKey2,
      title: /*#__PURE__*/React.createElement(Tips, {
        helpText: formatMessage({
          id: "".concat(intlPrefix, ".permission.tips")
        }),
        title: formatMessage({
          id: "".concat(intlPrefix, ".modal.permission")
        })
      }),
      drawer: true,
      style: modalStyle,
      className: 'c7ncd-modal-wrapper',
      children: /*#__PURE__*/React.createElement(PermissionManage, {
        refreshPermission: refreshPermission,
        onOk: permissionUpdate,
        clusterDetail: ClusterDetailDs.current,
        intlPrefix: intlPrefix,
        prefixCls: prefixCls,
        formatMessage: formatMessage,
        PermissionDs: PermissionDs,
        NonPermissionDs: NonPermissionDs
      })
    });
  }

  function getButtons() {
    var getCanCreate = mainStore.getCanCreate;
    return [{
      name: formatMessage({
        id: "".concat(intlPrefix, ".modal.create")
      }),
      permissions: ['choerodon.code.project.deploy.cluster.cluster-management.ps.create'],
      icon: 'link',
      handler: openCreate,
      display: true,
      disabled: !getCanCreate,
      tooltipsConfig: {
        title: !getCanCreate ? formatMessage({
          id: "".concat(intlPrefix, ".modal.create.disabled")
        }) : '',
        placement: 'bottom'
      },
      disabledMessage: formatMessage({
        id: "".concat(intlPrefix, ".modal.create.disabled")
      })
    }, {
      name: formatMessage({
        id: "".concat(intlPrefix, ".modal.createByHost")
      }),
      permissions: ['choerodon.code.project.deploy.cluster.cluster-management.ps.createByHost'],
      icon: 'playlist_add',
      handler: openCreateByHost,
      display: true,
      disabled: !getCanCreate,
      tooltipsConfig: {
        title: !getCanCreate ? formatMessage({
          id: "".concat(intlPrefix, ".modal.create.disabled")
        }) : '',
        placement: 'bottom'
      },
      disabledMessage: formatMessage({
        id: "".concat(intlPrefix, ".modal.create.disabled")
      })
    }, {
      name: formatMessage({
        id: "".concat(intlPrefix, ".modal.permission")
      }),
      permissions: ['choerodon.code.project.deploy.cluster.cluster-management.ps.permission-manage'],
      icon: 'settings-o',
      handler: openPermission,
      display: true
    }, {
      name: formatMessage({
        id: "".concat(intlPrefix, ".modal.createByNodes")
      }),
      icon: 'playlist_add',
      handler: openCreateByNodes,
      display: clusterStore.getSelectedMenu.type === 'created' && clusterStore.getSelectedMenu.status !== 'failed',
      disabled: !getCanCreate,
      permissions: ['choerodon.code.project.deploy.cluster.cluster-management.ps.createNodes'],
      group: 1
    }, {
      icon: 'refresh',
      handler: refresh,
      display: true,
      group: 1
    }];
  }

  return /*#__PURE__*/React.createElement(HeaderButtons, {
    items: getButtons(),
    showClassName: true
  });
});
export default ClusterModals;