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/lib/input/style";
import _Input from "choerodon-ui/lib/input";
import "choerodon-ui/lib/icon/style";
import _Icon from "choerodon-ui/lib/icon";

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, { useEffect } from 'react';
import { injectIntl, FormattedMessage } from 'react-intl';
import { observer } from 'mobx-react-lite';
import { axios, Choerodon } from '@choerodon/boot';
import includes from 'lodash/includes';
import { handlePromptError } from "../../../../utils";
import Settings from "./Settings";
import { useEditAppServiceStore } from "./stores";
import Loading from "../../../../components/loading";
import "./index.less";
var FILE_TYPE = 'image/png, image/jpeg, image/gif, image/jpg';
var CreateForm = injectIntl(observer(function (props) {
  var _useEditAppServiceSto = useEditAppServiceStore(),
      modal = _useEditAppServiceSto.modal,
      store = _useEditAppServiceSto.store,
      _useEditAppServiceSto2 = _useEditAppServiceSto.AppState.currentMenuType,
      projectId = _useEditAppServiceSto2.projectId,
      organizationId = _useEditAppServiceSto2.organizationId,
      formatMessage = _useEditAppServiceSto.intl.formatMessage,
      intlPrefix = _useEditAppServiceSto.intlPrefix,
      prefixCls = _useEditAppServiceSto.prefixCls,
      refresh = _useEditAppServiceSto.refresh,
      formDs = _useEditAppServiceSto.formDs;

  var record = formDs.current;

  if (!record) {
    return /*#__PURE__*/React.createElement(Loading, {
      display: true
    });
  }

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

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

          case 2:
            _context.prev = 2;
            _context.t0 = record.get('chartType') === 'custom' && !record.get('chartStatus');

            if (!_context.t0) {
              _context.next = 8;
              break;
            }

            _context.next = 7;
            return handleTestChart();

          case 7:
            _context.t0 = !_context.sent;

          case 8:
            chartTestFailed = _context.t0;
            _context.t1 = !chartTestFailed;

            if (!_context.t1) {
              _context.next = 15;
              break;
            }

            _context.next = 13;
            return formDs.submit();

          case 13:
            _context.t2 = _context.sent;
            _context.t1 = _context.t2 !== false;

          case 15:
            if (!_context.t1) {
              _context.next = 18;
              break;
            }

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

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

          case 21:
            _context.prev = 21;
            _context.t3 = _context["catch"](2);
            return _context.abrupt("return", false);

          case 24:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, null, [[2, 21]]);
  })));
  /**
   * 触发上传按钮
   */

  function triggerFileBtn() {
    var ele = document.getElementById('file');
    ele.click();
  }
  /**
   * 选择文件
   * @param e
   */


  function selectFile(_x) {
    return _selectFile.apply(this, arguments);
  }

  function _selectFile() {
    _selectFile = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(e) {
      var formdata, img, data;
      return regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              formdata = new FormData();
              img = e.target.files[0];

              if (includes(FILE_TYPE, img.type)) {
                _context2.next = 5;
                break;
              }

              Choerodon.prompt(formatMessage({
                id: "".concat(intlPrefix, ".file.failed")
              }));
              return _context2.abrupt("return");

            case 5:
              formdata.append('file', e.target.files[0]);
              _context2.prev = 6;
              _context2.next = 9;
              return axios.post("/hfle/v1/".concat(organizationId, "/files/multipart?bucketName=devops-service&fileName=").concat(img.name.split('.')[0]), formdata, {
                header: {
                  'Content-Type': 'multipart/form-data'
                }
              });

            case 9:
              data = _context2.sent;

              if (data) {
                record.set('imgUrl', data);
              }

              _context2.next = 16;
              break;

            case 13:
              _context2.prev = 13;
              _context2.t0 = _context2["catch"](6);
              Choerodon.handleResponseError(e);

            case 16:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2, null, [[6, 13]]);
    }));
    return _selectFile.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(projectId, postData);

            case 8:
              res = _context3.sent;

              if (!handlePromptError(res, false)) {
                _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);
  }

  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-create-wrap")
  }, /*#__PURE__*/React.createElement("div", {
    style: {
      backgroundImage: record.get('imgUrl') ? "url('".concat(record.get('imgUrl'), "')") : ''
    },
    className: "".concat(prefixCls, "-create-img"),
    onClick: triggerFileBtn,
    role: "none"
  }, /*#__PURE__*/React.createElement("div", {
    className: "'edit-img-mask"
  }, /*#__PURE__*/React.createElement(_Icon, {
    type: "photo_camera",
    className: "edit-img-icon"
  })), !record.get('imgUrl') && /*#__PURE__*/React.createElement("div", {
    className: "edit-avatar"
  }, /*#__PURE__*/React.createElement("span", null, record.get('name') && record.get('name').slice(0, 1))), /*#__PURE__*/React.createElement(_Input, {
    id: "file",
    type: "file",
    accept: FILE_TYPE,
    onChange: selectFile,
    style: {
      display: 'none'
    }
  })), /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-create-text")
  }, /*#__PURE__*/React.createElement(FormattedMessage, {
    id: "".concat(intlPrefix, ".icon")
  })), /*#__PURE__*/React.createElement(_Form, {
    record: record
  }, /*#__PURE__*/React.createElement(_TextField, {
    name: "name",
    autoFocus: true
  })), /*#__PURE__*/React.createElement(Settings, {
    record: record,
    handleTestChart: handleTestChart
  }), /*#__PURE__*/React.createElement(_Divider, {
    className: "c7ncd-form-divider"
  }), /*#__PURE__*/React.createElement("div", {
    className: "content-settings-title"
  }, /*#__PURE__*/React.createElement("span", null, "POM\u4FE1\u606F")), /*#__PURE__*/React.createElement(_Form, {
    record: record
  }, /*#__PURE__*/React.createElement(_TextField, {
    name: "artifactId"
  }), /*#__PURE__*/React.createElement(_TextField, {
    name: "groupId"
  })));
}));
export default CreateForm;