import "choerodon-ui/pro/lib/modal/style";
import _Modal from "choerodon-ui/pro/lib/modal";
import React from 'react';
import { observer } from 'mobx-react-lite';
import { HeaderButtons } from '@choerodon/master';
import { useResourceStore } from "../../../../stores";
import { useModalStore } from "./stores";
import { useNetworkStore } from "../stores";
import { useMainStore } from "../../../stores";
import CreateNetwork from "./network-operation";

var modalKey = _Modal.key();

var modalStyle = {
  width: 740
};
var EnvModals = observer(function () {
  var _useResourceStore = useResourceStore(),
      intlPrefix = _useResourceStore.intlPrefix,
      prefixCls = _useResourceStore.prefixCls,
      formatMessage = _useResourceStore.intl.formatMessage,
      resourceStore = _useResourceStore.resourceStore,
      treeDs = _useResourceStore.treeDs;

  var _useNetworkStore = useNetworkStore(),
      networkDs = _useNetworkStore.networkDs;

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

  var _useModalStore = useModalStore(),
      permissions = _useModalStore.permissions,
      projectId = _useModalStore.AppState.currentMenuType.projectId;

  var parentId = resourceStore.getSelectedMenu.parentId;

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

  function openModal() {
    _Modal.open({
      key: modalKey,
      style: modalStyle,
      drawer: true,
      title: formatMessage({
        id: 'network.header.create'
      }),
      children: /*#__PURE__*/React.createElement(CreateNetwork, {
        envId: parentId,
        store: networkStore,
        refresh: refresh
      }),
      okText: formatMessage({
        id: 'create'
      })
    });
  }

  function getButtons() {
    var envRecord = treeDs.find(function (record) {
      return record.get('key') === parentId;
    });
    var connect = envRecord.get('connect');
    var disabled = !connect;
    return [{
      permissions: ['choerodon.code.project.deploy.app-deployment.resource.ps.network'],
      name: formatMessage({
        id: "".concat(intlPrefix, ".create.network")
      }),
      icon: 'playlist_add',
      handler: openModal,
      display: true,
      service: permissions,
      disabled: disabled
    }, {
      icon: 'refresh',
      handler: refresh,
      display: true
    }];
  }

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