import "choerodon-ui/lib/spin/style";
import _Spin from "@hzero-front-ui/c7n-ui/lib/Spin";
import "choerodon-ui/lib/tabs/style";
import _Tabs from "choerodon-ui/lib/tabs";

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

import React, { lazy, Suspense, useEffect } from 'react';
import { runInAction } from 'mobx';
import { observer } from 'mobx-react-lite';
import { Permission } from '@choerodon/boot';
import { useEnvironmentStore } from "./stores";
import { useResourceStore } from "../../../stores";
import PageTitle from "../../../../../components/page-title";
import EnvItem from "../../../../../components/env-item";
import openWarnModal from "../../../../../utils/openWarnModal";
import Modals from "./modals";
import Tips from "../../../../../components/new-tips";
import Config from "./DeployConfig";
var TabPane = _Tabs.TabPane;
var SyncSituation = /*#__PURE__*/lazy(function () {
  return import("./sync-situation");
});
var Permissions = /*#__PURE__*/lazy(function () {
  return import("./Permissions");
});
var Polaris = /*#__PURE__*/lazy(function () {
  return import("./polaris");
});
var EnvContent = observer(function () {
  var _useResourceStore = useResourceStore(),
      prefixCls = _useResourceStore.prefixCls,
      intlPrefix = _useResourceStore.intlPrefix,
      treeDs = _useResourceStore.treeDs,
      resourceStore = _useResourceStore.resourceStore;

  var _useEnvironmentStore = useEnvironmentStore(),
      formatMessage = _useEnvironmentStore.intl.formatMessage,
      baseInfoDs = _useEnvironmentStore.baseInfoDs,
      _useEnvironmentStore$ = _useEnvironmentStore.tabs,
      SYNC_TAB = _useEnvironmentStore$.SYNC_TAB,
      CONFIG_TAB = _useEnvironmentStore$.CONFIG_TAB,
      ASSIGN_TAB = _useEnvironmentStore$.ASSIGN_TAB,
      POLARIS_TAB = _useEnvironmentStore$.POLARIS_TAB,
      envStore = _useEnvironmentStore.envStore;

  function handleChange(key) {
    envStore.setTabKey(key);
  }

  function refresh() {
    treeDs.query();
  }

  function getCurrent() {
    var record = baseInfoDs.current;

    if (record) {
      var id = record.get('id');
      var name = record.get('name');
      var active = record.get('active');
      var connect = record.get('connect');
      var clusterName = record.get('clusterName');
      return {
        id: id,
        name: name,
        active: active,
        connect: connect,
        clusterName: clusterName
      };
    }

    return null;
  }

  useEffect(function () {
    var currentBase = getCurrent();

    if (currentBase) {
      var id = currentBase.id,
          name = currentBase.name,
          active = currentBase.active,
          connect = currentBase.connect;
      var menuItem = treeDs.find(function (item) {
        return item.get('key') === String(id);
      });

      if (menuItem) {
        // 清除已经停用的环境
        if (!active) {
          openWarnModal(refresh, formatMessage);
        } else if (menuItem.get('connect') !== connect || menuItem.get('name') !== name) {
          runInAction(function () {
            menuItem.set({
              name: name,
              connect: connect
            });
            resourceStore.setSelectedMenu(_objectSpread(_objectSpread({}, resourceStore.getSelectedMenu), {}, {
              name: name,
              connect: connect
            }));
          });
        }
      }
    }
  });

  function getTitle() {
    var current = getCurrent();

    if (current) {
      return /*#__PURE__*/React.createElement(EnvItem, {
        isTitle: true,
        name: current.name,
        connect: current.connect,
        formatMessage: formatMessage,
        clusterName: current.clusterName
      });
    }

    return null;
  }

  function getFallBack() {
    var _resourceStore$getSel = resourceStore.getSelectedMenu,
        name = _resourceStore$getSel.name,
        connect = _resourceStore$getSel.connect;
    return /*#__PURE__*/React.createElement(EnvItem, {
      isTitle: true,
      name: name,
      connect: connect
    });
  }

  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-environment")
  }, /*#__PURE__*/React.createElement(PageTitle, {
    fallback: getFallBack(),
    content: getTitle()
  }), /*#__PURE__*/React.createElement(_Tabs, {
    animated: false,
    activeKey: envStore.getTabKey,
    onChange: handleChange
  }, /*#__PURE__*/React.createElement(TabPane, {
    key: SYNC_TAB,
    tab: formatMessage({
      id: "".concat(intlPrefix, ".environment.tabs.sync")
    })
  }, /*#__PURE__*/React.createElement(Suspense, {
    fallback: /*#__PURE__*/React.createElement(_Spin, null)
  }, /*#__PURE__*/React.createElement(SyncSituation, null))), /*#__PURE__*/React.createElement(TabPane, {
    key: CONFIG_TAB,
    tab: formatMessage({
      id: "".concat(intlPrefix, ".environment.tabs.config")
    })
  }, /*#__PURE__*/React.createElement(Suspense, {
    fallback: /*#__PURE__*/React.createElement(_Spin, null)
  }, /*#__PURE__*/React.createElement(Config, null))), /*#__PURE__*/React.createElement(TabPane, {
    key: POLARIS_TAB,
    tab: formatMessage({
      id: "".concat(intlPrefix, ".environment.tabs.polaris")
    })
  }, /*#__PURE__*/React.createElement(Suspense, {
    fallback: /*#__PURE__*/React.createElement(_Spin, null)
  }, /*#__PURE__*/React.createElement(Polaris, null))), envStore.getPermission && /*#__PURE__*/React.createElement(TabPane, {
    key: ASSIGN_TAB,
    tab: /*#__PURE__*/React.createElement(Tips, {
      helpText: formatMessage({
        id: "".concat(intlPrefix, ".permission.tab.tips")
      }),
      title: formatMessage({
        id: "".concat(intlPrefix, ".environment.tabs.assignPermissions")
      })
    })
  }, /*#__PURE__*/React.createElement(Suspense, {
    fallback: /*#__PURE__*/React.createElement(_Spin, null)
  }, /*#__PURE__*/React.createElement(Permissions, null)))), /*#__PURE__*/React.createElement(Modals, null));
});
export default EnvContent;