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, { Fragment, lazy, Suspense, useEffect } from 'react';
import { runInAction } from 'mobx';
import { observer } from 'mobx-react-lite';
import pick from 'lodash/pick';
import isEqual from 'lodash/isEqual';
import { useEnvironmentStore } from "../../../stores";
import { useDetailStore } from "./stores";
import PageTitle from "../../../../../components/page-title";
import EnvItem from "../../../../../components/env-item";
import Modals from "./modals";
import Tips from "../../../../../components/new-tips";
var TabPane = _Tabs.TabPane;
var SyncSituation = /*#__PURE__*/lazy(function () {
  return import("./sync");
});
var Config = /*#__PURE__*/lazy(function () {
  return import("./DeployConfig");
});
var Permissions = /*#__PURE__*/lazy(function () {
  return import("./Permissions");
});
var EnvContent = observer(function () {
  var _useEnvironmentStore = useEnvironmentStore(),
      envStore = _useEnvironmentStore.envStore,
      treeDs = _useEnvironmentStore.treeDs;

  var _useDetailStore = useDetailStore(),
      intlPrefix = _useDetailStore.intlPrefix,
      formatMessage = _useDetailStore.intl.formatMessage,
      _useDetailStore$tabs = _useDetailStore.tabs,
      SYNC_TAB = _useDetailStore$tabs.SYNC_TAB,
      CONFIG_TAB = _useDetailStore$tabs.CONFIG_TAB,
      ASSIGN_TAB = _useDetailStore$tabs.ASSIGN_TAB,
      detailStore = _useDetailStore.detailStore,
      baseDs = _useDetailStore.baseDs;

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

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

    if (record) {
      var id = record.get('id');
      var name = record.get('name');
      var active = record.get('active');
      var connect = record.get('connect');
      var synchro = record.get('synchronize');
      var failed = record.get('fail');
      var clusterName = record.get('clusterName');
      return {
        id: id,
        name: name,
        active: active,
        connect: connect,
        synchro: synchro,
        clusterName: clusterName,
        failed: failed || false
      };
    }

    return null;
  }

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

    if (update) {
      var menuItem = treeDs.find(function (item) {
        return item.get('id') === update.id;
      });

      if (menuItem) {
        var previous = pick(menuItem.toData(), ['id', 'active', 'name', 'connect', 'synchro', 'failed', 'clusterName']);

        if (!isEqual(previous, update)) {
          runInAction(function () {
            menuItem.set(update);

            var newState = _objectSpread(_objectSpread({}, envStore.getSelectedMenu), update);

            envStore.setSelectedMenu(newState);
          });
        }
      }
    }
  });

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

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

    return null;
  }

  function getFallBack() {
    var _envStore$getSelected = envStore.getSelectedMenu,
        name = _envStore$getSelected.name,
        connect = _envStore$getSelected.connect,
        active = _envStore$getSelected.active;
    return /*#__PURE__*/React.createElement(EnvItem, {
      isTitle: true,
      name: name,
      connect: connect,
      active: active
    });
  }

  return /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement(PageTitle, {
    content: getTitle(),
    fallback: getFallBack()
  }), /*#__PURE__*/React.createElement(_Tabs, {
    animated: false,
    activeKey: detailStore.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: 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;