import "choerodon-ui/pro/lib/form/style";
import _Form from "@hzero-front-ui/c7n-ui/lib/FormPro";
import "choerodon-ui/pro/lib/password/style";
import _Password from "choerodon-ui/pro/lib/password";
import "choerodon-ui/pro/lib/text-field/style";
import _TextField from "choerodon-ui/pro/lib/text-field";
import "choerodon-ui/pro/lib/url-field/style";
import _UrlField from "choerodon-ui/pro/lib/url-field";
import "choerodon-ui/pro/lib/select-box/style";
import _SelectBox from "choerodon-ui/pro/lib/select-box";
import "choerodon-ui/pro/lib/icon/style";
import _Icon from "choerodon-ui/pro/lib/icon";
import "choerodon-ui/pro/lib/button/style";
import _Button from "choerodon-ui/pro/lib/button";
import "choerodon-ui/pro/lib/select/style";
import _Select from "choerodon-ui/pro/lib/select";

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, { Fragment, useEffect } from 'react';
import { injectIntl, FormattedMessage } from 'react-intl';
import { observer } from 'mobx-react-lite';
import { withRouter } from 'react-router-dom';
import { handlePromptError } from "../../../utils";
import "./index.less";
var Option = _Select.Option;
export default withRouter(injectIntl(observer(function (_ref) {
  var record = _ref.record,
      dataSet = _ref.dataSet,
      store = _ref.store,
      id = _ref.id,
      formatMessage = _ref.intl.formatMessage,
      prefixCls = _ref.prefixCls,
      intlPrefix = _ref.intlPrefix,
      modal = _ref.modal,
      isProject = _ref.isProject,
      history = _ref.history,
      search = _ref.location.search;

  function refresh() {
    return _refresh.apply(this, arguments);
  }

  function _refresh() {
    _refresh = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return dataSet.query();

            case 2:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }));
    return _refresh.apply(this, arguments);
  }

  function handleSave() {
    return _handleSave.apply(this, arguments);
  }

  function _handleSave() {
    _handleSave = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
      var chartTestFailed;
      return regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              if (!(record.get('harborStatus') === 'failed' || record.get('chartStatus') === 'failed')) {
                _context2.next = 2;
                break;
              }

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

            case 2:
              _context2.t0 = record.get('chartCustom') === 'custom' && !record.get('chartStatus');

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

              _context2.next = 6;
              return handleTestChart();

            case 6:
              _context2.t0 = !_context2.sent;

            case 7:
              chartTestFailed = _context2.t0;
              _context2.t1 = !chartTestFailed;

              if (!_context2.t1) {
                _context2.next = 14;
                break;
              }

              _context2.next = 12;
              return dataSet.submit();

            case 12:
              _context2.t2 = _context2.sent;
              _context2.t1 = _context2.t2 !== false;

            case 14:
              if (!_context2.t1) {
                _context2.next = 17;
                break;
              }

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

            case 17:
              return _context2.abrupt("return", false);

            case 18:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    }));
    return _handleSave.apply(this, arguments);
  }

  function handleTestChart() {
    return _handleTestChart.apply(this, arguments);
  }

  function _handleTestChart() {
    _handleTestChart = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() {
      var postData, res;
      return regeneratorRuntime.wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              _context3.prev = 0;
              _context3.next = 3;
              return record.validate();

            case 3:
              if (_context3.sent) {
                _context3.next = 5;
                break;
              }

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

            case 5:
              postData = {
                url: record.get('url'),
                userName: record.get('password') && record.get('userName') ? record.get('userName') : null,
                password: record.get('password') && record.get('userName') ? record.get('password') : null
              };
              _context3.next = 8;
              return store.checkChart(id, postData);

            case 8:
              res = _context3.sent;

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

              record.set('chartStatus', 'success');
              return _context3.abrupt("return", true);

            case 12:
              record.set('chartStatus', 'failed');
              return _context3.abrupt("return", false);

            case 16:
              _context3.prev = 16;
              _context3.t0 = _context3["catch"](0);
              record.set('chartStatus', 'failed');
              return _context3.abrupt("return", false);

            case 20:
            case "end":
              return _context3.stop();
          }
        }
      }, _callee3, null, [[0, 16]]);
    }));
    return _handleTestChart.apply(this, arguments);
  }

  function renderTestButton(status, handleClick) {
    return /*#__PURE__*/React.createElement("div", {
      className: "".concat(prefixCls, "-form-btnContent")
    }, /*#__PURE__*/React.createElement(_Button, {
      onClick: handleClick,
      funcType: "raised",
      className: "".concat(prefixCls, "-form-button")
    }, /*#__PURE__*/React.createElement(FormattedMessage, {
      id: "".concat(intlPrefix, ".test")
    })), status && /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement(_Icon, {
      type: status === 'success' ? 'check_circle' : 'cancel',
      className: "".concat(prefixCls, "-form-test-").concat(status)
    }), formatMessage({
      id: "".concat(intlPrefix, ".test.").concat(status)
    })));
  }

  function handleLink() {
    history.push("/rdupm/product-lib".concat(search));
  }

  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-form-wrap")
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-form-info")
  }, /*#__PURE__*/React.createElement(_Icon, {
    type: "info",
    className: "".concat(prefixCls, "-form-info-icon")
  }), /*#__PURE__*/React.createElement(FormattedMessage, {
    id: "".concat(intlPrefix, ".info")
  })), isProject ? /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("span", {
    className: "".concat(prefixCls, "-form-config-title")
  }, formatMessage({
    id: "".concat(intlPrefix, ".harbor.config")
  })), /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-empty-page")
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-empty-page-image")
  }), /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-empty-page-text")
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-empty-page-title")
  }, formatMessage({
    id: "".concat(intlPrefix, ".empty.title")
  })), /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-empty-page-des")
  }, formatMessage({
    id: "".concat(intlPrefix, ".empty.des")
  })), /*#__PURE__*/React.createElement(_Button, {
    color: "primary",
    onClick: handleLink,
    funcType: "raised"
  }, formatMessage({
    id: "".concat(intlPrefix, ".empty.link")
  }))))) : null, /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-form")
  }, /*#__PURE__*/React.createElement("span", {
    className: "".concat(prefixCls, "-form-config-title")
  }, formatMessage({
    id: "".concat(intlPrefix, ".chart.config")
  })), /*#__PURE__*/React.createElement(_Form, {
    record: record
  }, /*#__PURE__*/React.createElement(_SelectBox, {
    name: "chartCustom"
  }, /*#__PURE__*/React.createElement(Option, {
    value: "default"
  }, formatMessage({
    id: "".concat(intlPrefix, ".chart.default")
  })), /*#__PURE__*/React.createElement(Option, {
    value: "custom"
  }, formatMessage({
    id: "".concat(intlPrefix, ".chart.custom")
  }))), record.get('chartCustom') === 'custom' && [/*#__PURE__*/React.createElement(_UrlField, {
    name: "url"
  }), /*#__PURE__*/React.createElement(_TextField, {
    name: "userName"
  }), /*#__PURE__*/React.createElement(_Password, {
    name: "password"
  }), renderTestButton(record.get('chartStatus'), handleTestChart)])), /*#__PURE__*/React.createElement("div", {
    style: {
      display: 'flex'
    }
  }, /*#__PURE__*/React.createElement(_Button, {
    color: "primary",
    funcType: "raised",
    onClick: handleSave,
    style: {
      marginRight: '.12rem'
    }
  }, formatMessage({
    id: 'save'
  })), /*#__PURE__*/React.createElement(_Button, {
    funcType: "raised",
    onClick: refresh
  }, /*#__PURE__*/React.createElement("span", {
    style: {
      color: '#3f51b5'
    }
  }, formatMessage({
    id: 'cancel'
  })))));
})));