import "choerodon-ui/pro/lib/password/style";
import _Password from "choerodon-ui/pro/lib/password";
import "choerodon-ui/lib/alert/style";
import _Alert from "choerodon-ui/lib/alert";
import "choerodon-ui/lib/divider/style";
import _Divider from "choerodon-ui/lib/divider";
import "choerodon-ui/pro/lib/form/style";
import _Form from "@hzero-front-ui/c7n-ui/lib/FormPro";
import "choerodon-ui/pro/lib/text-field/style";
import _TextField from "choerodon-ui/pro/lib/text-field";
import "choerodon-ui/pro/lib/select-box/style";
import _SelectBox from "choerodon-ui/pro/lib/select-box";
import "choerodon-ui/pro/lib/spin/style";
import _Spin from "choerodon-ui/pro/lib/spin";
import "choerodon-ui/pro/lib/message/style";
import _message from "choerodon-ui/pro/lib/message";
import "choerodon-ui/lib/tabs/style";
import _Tabs from "choerodon-ui/lib/tabs";

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

import React from 'react';
import { observer } from 'mobx-react-lite';
import pick from 'lodash/pick';
import HostConfigApis from "../../apis";
import YamlEditor from "../../../../components/yamlEditor";
import Tips from "../../../../components/new-tips";
import { useCreateHostStore } from "./stores";
import JmeterGuide from "./components/jmeter-guide";
import TestConnect from "./components/test-connect";
var TabPane = _Tabs.TabPane;
var CreateHost = observer(function () {
  var _formDs$current2, _formDs$current2$getF, _formDs$current3, _formDs$current3$getF;

  var _useCreateHostStore = useCreateHostStore(),
      prefixCls = _useCreateHostStore.prefixCls,
      formDs = _useCreateHostStore.formDs,
      intlPrefix = _useCreateHostStore.intlPrefix,
      modal = _useCreateHostStore.modal,
      formatMessage = _useCreateHostStore.formatMessage,
      projectId = _useCreateHostStore.projectId,
      refresh = _useCreateHostStore.refresh,
      hostId = _useCreateHostStore.hostId,
      showTestTab = _useCreateHostStore.showTestTab;

  modal.handleOk( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
    var record, selectedTabKey, res;
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.prev = 0;
            record = formDs.current;
            selectedTabKey = record === null || record === void 0 ? void 0 : record.get('type');

            if (!(record && !(record.get('hostIp') || record.get('sshPort') || record.get('privateIp') || record.get('privatePort')))) {
              _context.next = 6;
              break;
            }

            _message.error('外部SSH认证与内部SSH认证至少填写一个模块');

            return _context.abrupt("return", false);

          case 6:
            _context.next = 8;
            return formDs.submit();

          case 8:
            res = _context.sent;

            if (!res) {
              _context.next = 12;
              break;
            }

            refresh(selectedTabKey);
            return _context.abrupt("return", true);

          case 12:
            return _context.abrupt("return", false);

          case 15:
            _context.prev = 15;
            _context.t0 = _context["catch"](0);
            return _context.abrupt("return", false);

          case 18:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, null, [[0, 15]]);
  })));

  var handleTestConnection = /*#__PURE__*/function () {
    var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
      var validate, record, postData, res, hostStatus, jmeterStatus, hostCheckError, jmeterCheckError, status, _record;

      return regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              _context2.prev = 0;
              _context2.next = 3;
              return formDs.validate();

            case 3:
              validate = _context2.sent;
              record = formDs.current; // @ts-ignore
              // 单独再次校验密码是因为：修改时无任何操作，formDs.validate() 返回为true

              _context2.t0 = !validate || !record;

              if (_context2.t0) {
                _context2.next = 11;
                break;
              }

              _context2.next = 9;
              return record.getField('password').checkValidity();

            case 9:
              _context2.t1 = _context2.sent;
              _context2.t0 = _context2.t1 === false;

            case 11:
              if (!_context2.t0) {
                _context2.next = 13;
                break;
              }

              return _context2.abrupt("return", false);

            case 13:
              postData = pick(record.toData(), ['type', 'authType', 'hostIp', 'sshPort', 'username', 'password', 'jmeterPort', 'jmeterPath']);
              modal.update({
                okProps: {
                  disabled: true
                }
              });
              record.set('status', 'operating');

              if (postData.type === 'deploy') {
                postData.jmeterPort = null;
              } else {
                record.set('jmeterStatus', 'operating');
              }

              record.set('hostStatus', 'operating');
              _context2.next = 20;
              return HostConfigApis.testConnection(projectId, postData, postData.type);

            case 20:
              res = _context2.sent;
              modal.update({
                okProps: {
                  disabled: false
                }
              });

              if (res) {
                hostStatus = res.hostStatus, jmeterStatus = res.jmeterStatus, hostCheckError = res.hostCheckError, jmeterCheckError = res.jmeterCheckError; // eslint-disable-next-line no-nested-ternary

                status = [hostStatus, jmeterStatus].includes('failed') ? 'failed' : hostStatus === 'success' && jmeterStatus === 'success' ? 'success' : 'operating';
                record.set('hostStatus', hostStatus);
                record.set('jmeterStatus', jmeterStatus);
                record.set('hostCheckError', hostCheckError);
                record.set('jmeterCheckError', jmeterCheckError);
                record.set('status', postData.type === 'deploy' ? hostStatus : status);
              }

              return _context2.abrupt("return", true);

            case 26:
              _context2.prev = 26;
              _context2.t2 = _context2["catch"](0);
              _record = formDs.current;

              if (_record) {
                _record.set('hostStatus', 'wait');

                _record.set('jmeterStatus', 'wait');

                _record.set('status', 'wait');
              }

              modal.update({
                okProps: {
                  disabled: false
                }
              });
              return _context2.abrupt("return", false);

            case 32:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2, null, [[0, 26]]);
    }));

    return function handleTestConnection() {
      return _ref2.apply(this, arguments);
    };
  }();

  if (formDs && formDs.status === 'loading') {
    return /*#__PURE__*/React.createElement(_Spin, {
      spinning: true
    });
  }

  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls)
  }, showTestTab && /*#__PURE__*/React.createElement(Tips, {
    title: formatMessage({
      id: "".concat(intlPrefix, ".type")
    }),
    helpText: "\u6D4B\u8BD5\u4E3B\u673A\u53EF\u7528\u4E8E\u6267\u884C\u6027\u80FD\u6D4B\u8BD5\u65F6\u4F7F\u7528\uFF1B\u90E8\u7F72\u4E3B\u673A\u5219\u7528\u4E8E\u4E3B\u673A\u90E8\u7F72\u64CD\u4F5C\u65F6\u4F7F\u7528\u3002",
    className: "".concat(prefixCls, "-type-tips")
  }), /*#__PURE__*/React.createElement(_Form, {
    dataSet: formDs,
    className: "".concat(prefixCls, "-form")
  }, showTestTab && /*#__PURE__*/React.createElement(_SelectBox, {
    name: "type",
    disabled: !!hostId
  }), /*#__PURE__*/React.createElement(_TextField, {
    name: "name",
    style: {
      marginTop: '-10px'
    }
  })), formDs && formDs.current && formDs.current.get('type') === 'deploy' ? [/*#__PURE__*/React.createElement(_Divider, {
    className: "".concat(prefixCls, "-divider")
  }), /*#__PURE__*/React.createElement(_Alert, {
    type: "info",
    showIcon: true,
    message: "\u5916\u90E8SSH\u8BA4\u8BC1\u4E0E\u5185\u90E8SSH\u8BA4\u8BC1\u81F3\u5C11\u586B\u5199\u4E00\u4E2A\u6A21\u5757",
    className: "".concat(prefixCls, "-alert")
  }), /*#__PURE__*/React.createElement(_Tabs, {
    defaultActiveKey: "external"
  }, /*#__PURE__*/React.createElement(TabPane, {
    tab: /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("span", {
      className: "".concat(prefixCls, "-tab-text")
    }, "\u5916\u90E8SSH\u8BA4\u8BC1"), /*#__PURE__*/React.createElement(Tips, {
      helpText: "\u5916\u90E8SSH\u8BA4\u8BC1\u7528\u4E8E\u8FDE\u901AChoerodon\u4E0E\u5F53\u524D\u4E3B\u673A\uFF1B\u8BBE\u7F6E\u540E\uFF0CChoerodon\u624D\u80FD\u8BBF\u95EE\u5230\u8BE5\u4E3B\u673A\u3002",
      className: "".concat(prefixCls, "-tab-tips")
    })),
    key: "external"
  }, /*#__PURE__*/React.createElement(_Form, {
    dataSet: formDs,
    className: "".concat(prefixCls, "-form")
  }, /*#__PURE__*/React.createElement(_TextField, {
    name: "hostIp"
  }), /*#__PURE__*/React.createElement(_TextField, {
    name: "sshPort"
  }))), /*#__PURE__*/React.createElement(TabPane, {
    tab: /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("span", {
      className: "".concat(prefixCls, "-tab-text")
    }, "\u5185\u90E8SSH\u8BA4\u8BC1"), /*#__PURE__*/React.createElement(Tips, {
      helpText: "\u5185\u90E8SSH\u8BA4\u8BC1\u7528\u4E8E\u5B9E\u73B0\u5185\u90E8\u4E3B\u673A\u4E4B\u95F4\u7684\u8FDE\u901A\uFF1B\u8BBE\u7F6E\u540E\uFF0C\u76F8\u5E94\u7684\u4E3B\u673A\u4E4B\u95F4\u4FBF\u80FD\u5B9E\u73B0\u76F8\u4E92\u8FDE\u901A\u3002"
    })),
    key: "internal"
  }, /*#__PURE__*/React.createElement(_Form, {
    dataSet: formDs,
    className: "".concat(prefixCls, "-form")
  }, /*#__PURE__*/React.createElement(_TextField, {
    name: "privateIp"
  }), /*#__PURE__*/React.createElement(_TextField, {
    name: "privatePort"
  }))))] : /*#__PURE__*/React.createElement(_Form, {
    dataSet: formDs,
    className: "".concat(prefixCls, "-form")
  }, /*#__PURE__*/React.createElement(_TextField, {
    name: "hostIp"
  }), /*#__PURE__*/React.createElement(_TextField, {
    name: "sshPort"
  })), /*#__PURE__*/React.createElement(_Divider, {
    className: "".concat(prefixCls, "-divider")
  }), /*#__PURE__*/React.createElement(_Form, {
    dataSet: formDs,
    className: "".concat(prefixCls, "-form")
  }, /*#__PURE__*/React.createElement(_SelectBox, {
    name: "authType"
  }), /*#__PURE__*/React.createElement(_TextField, {
    name: "username",
    style: {
      marginTop: '-10px'
    }
  }), formDs && formDs.current && formDs.current.get('authType') === 'publickey' ? [/*#__PURE__*/React.createElement("span", {
    className: "".concat(prefixCls, "-form-password")
  }, "\u5BC6\u94A5"), /*#__PURE__*/React.createElement(YamlEditor, {
    readOnly: false,
    newLine: true,
    value: formDs.current.get('password'),
    onValueChange: function onValueChange(valueYaml) {
      var _formDs$current;

      return (_formDs$current = formDs.current) === null || _formDs$current === void 0 ? void 0 : _formDs$current.set('password', valueYaml);
    },
    modeChange: false,
    showError: false
  }), ((_formDs$current2 = formDs.current) === null || _formDs$current2 === void 0 ? void 0 : (_formDs$current2$getF = _formDs$current2.getField('password')) === null || _formDs$current2$getF === void 0 ? void 0 : _formDs$current2$getF.getValidationMessage()) ? /*#__PURE__*/React.createElement("span", {
    className: "".concat(prefixCls, "-form-password-error")
  }, (_formDs$current3 = formDs.current) === null || _formDs$current3 === void 0 ? void 0 : (_formDs$current3$getF = _formDs$current3.getField('password')) === null || _formDs$current3$getF === void 0 ? void 0 : _formDs$current3$getF.getValidationMessage()) : null] : /*#__PURE__*/React.createElement(_Password, {
    name: "password",
    reveal: false
  })), formDs && formDs.current && formDs.current.get('type') === 'distribute_test' && [/*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(_Divider, {
    className: "".concat(prefixCls, "-divider")
  }), /*#__PURE__*/React.createElement("span", null, formatMessage({
    id: "".concat(intlPrefix, ".jmeter")
  }))), /*#__PURE__*/React.createElement(_Form, {
    dataSet: formDs,
    className: "".concat(prefixCls, "-form")
  }, /*#__PURE__*/React.createElement(_TextField, {
    name: "jmeterPort"
  }), /*#__PURE__*/React.createElement(_TextField, {
    name: "jmeterPath"
  }))], formDs && formDs.current && formDs.current.get('type') === 'distribute_test' && /*#__PURE__*/React.createElement(JmeterGuide, null), /*#__PURE__*/React.createElement(TestConnect, {
    handleTestConnection: handleTestConnection
  }));
});
export default CreateHost;