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 { useKeyValueStore } from "../stores";
import KeyValueModal from "../../application/modals/key-value/KeyValueProIndex";

var modalKey = _Modal.key();

var modalStyle = {
  width: 'calc(100vw - 3.52rem)'
};
var KeyValueModals = observer(function () {
  var _useResourceStore = useResourceStore(),
      intlPrefix = _useResourceStore.intlPrefix,
      prefixCls = _useResourceStore.prefixCls,
      formatMessage = _useResourceStore.intl.formatMessage,
      resourceStore = _useResourceStore.resourceStore,
      treeDs = _useResourceStore.treeDs;

  var _useKeyValueStore = useKeyValueStore(),
      formStore = _useKeyValueStore.formStore,
      SecretTableDs = _useKeyValueStore.SecretTableDs;

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

  var parentId = resourceStore.getSelectedMenu.parentId;

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

  function openModal() {
    _Modal.open({
      key: modalKey,
      style: modalStyle,
      drawer: true,
      title: formatMessage({
        id: "".concat(intlPrefix, ".cipher.create")
      }),
      children: /*#__PURE__*/React.createElement(KeyValueModal, {
        title: "cipher",
        envId: parentId,
        store: formStore,
        intlPrefix: intlPrefix,
        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.cipher'],
      name: formatMessage({
        id: "".concat(intlPrefix, ".create.cipher")
      }),
      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 KeyValueModals;