import "choerodon-ui/pro/lib/modal/style";
import _Modal from "choerodon-ui/pro/lib/modal";
import React, { useMemo, useCallback, useEffect } from 'react';
import { observer } from 'mobx-react-lite';
import { HeaderButtons } from '@choerodon/master';
import { useResourceStore } from "../../../../stores";
import { useModalStore } from "./stores";
import { useIstListStore } from "../stores";
import useDeployStore from "../../../../../deployment/stores/useStore";
import Deploy from "../../../../../deployment/modals/deploy";
import BatchDeploy from "../../../../../deployment/modals/batch-deploy";
var modalStyle = {
  width: '26%'
};

var deployKey = _Modal.key();

var batchDeployKey = _Modal.key();

var CustomModals = observer(function () {
  var _useResourceStore = useResourceStore(),
      formatMessage = _useResourceStore.intl.formatMessage,
      parentId = _useResourceStore.resourceStore.getSelectedMenu.parentId,
      treeDs = _useResourceStore.treeDs;

  var _useIstListStore = useIstListStore(),
      istListDs = _useIstListStore.istListDs,
      envId = _useIstListStore.envId;

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

  var deployStore = useDeployStore();
  var intlPrefixDeploy = 'c7ncd.deploy';
  var configModalStyle = useMemo(function () {
    return {
      width: 'calc(100vw - 3.52rem)',
      minWidth: '2rem'
    };
  }, []);

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

  function openDeploy() {
    _Modal.open({
      key: deployKey,
      style: configModalStyle,
      drawer: true,
      title: formatMessage({
        id: "".concat(intlPrefixDeploy, ".manual")
      }),
      children: /*#__PURE__*/React.createElement(Deploy, {
        deployStore: deployStore,
        refresh: refresh,
        intlPrefix: intlPrefixDeploy,
        prefixCls: "c7ncd-deploy",
        envId: envId
      }),
      afterClose: function afterClose() {
        deployStore.setCertificates([]);
        deployStore.setAppService([]);
        deployStore.setConfigValue('');
      },
      okText: formatMessage({
        id: 'deployment'
      })
    });
  }

  function openBatchDeploy() {
    _Modal.open({
      key: batchDeployKey,
      style: configModalStyle,
      drawer: true,
      title: formatMessage({
        id: "".concat(intlPrefixDeploy, ".batch")
      }),
      children: /*#__PURE__*/React.createElement(BatchDeploy, {
        deployStore: deployStore,
        refresh: refresh,
        intlPrefix: intlPrefixDeploy,
        prefixCls: "c7ncd-deploy",
        envId: envId
      }),
      afterClose: function afterClose() {
        deployStore.setCertificates([]);
        deployStore.setAppService([]);
        deployStore.setShareAppService([]);
        deployStore.setConfigValue('');
      },
      okText: formatMessage({
        id: 'deployment'
      })
    });
  }

  var buttons = useMemo(function () {
    var envRecord = treeDs.find(function (record) {
      return record.get('key') === parentId;
    });
    var connect = envRecord && envRecord.get('connect');
    var configDisabled = !connect;
    return [{
      permissions: ['choerodon.code.project.deploy.app-deployment.resource.ps.resource-deploy'],
      disabled: configDisabled,
      name: formatMessage({
        id: "".concat(intlPrefixDeploy, ".manual")
      }),
      icon: 'cloud_done-o',
      handler: openDeploy,
      display: true
    }, {
      permissions: ['choerodon.code.project.deploy.app-deployment.resource.ps.resource-batch'],
      disabled: configDisabled,
      name: formatMessage({
        id: "".concat(intlPrefixDeploy, ".batch")
      }),
      icon: 'cloud_done-o',
      handler: openBatchDeploy,
      display: true
    }, {
      icon: 'refresh',
      handler: refresh,
      display: true
    }];
  }, [formatMessage, refresh]);
  return /*#__PURE__*/React.createElement(HeaderButtons, {
    items: buttons,
    showClassName: true
  });
});
export default CustomModals;