import "choerodon-ui/pro/lib/modal/style";
import _Modal from "choerodon-ui/pro/lib/modal";
import React, { Fragment, useState } from 'react';
import { observer } from 'mobx-react-lite';
import { HeaderButtons } from '@choerodon/master';
import { useResourceStore } from "../../../../stores";
import { useModalStore } from "./stores";
import { useIngressStore } from "../stores";
import DomainForm from "../../../components/domain-form";
import { useMainStore } from "../../../stores";

var modalKey = _Modal.key();

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

  var _useIngressStore = useIngressStore(),
      ingressDs = _useIngressStore.ingressDs;

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

  var _useModalStore = useModalStore(),
      permissions = _useModalStore.permissions;

  var parentId = resourceStore.getSelectedMenu.parentId;

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

  function openModal() {
    _Modal.open({
      key: modalKey,
      style: modalStyle,
      drawer: true,
      title: formatMessage({
        id: 'domain.create.head'
      }),
      children: /*#__PURE__*/React.createElement(DomainForm, {
        envId: parentId,
        refresh: refresh,
        intlPrefix: intlPrefix,
        prefixCls: prefixCls,
        ingressStore: ingressStore
      }),
      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.domain'],
      name: formatMessage({
        id: "".concat(intlPrefix, ".create.ingress")
      }),
      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;