import "choerodon-ui/pro/lib/form/style";
import _Form from "@hzero-front-ui/c7n-ui/lib/FormPro";
import "choerodon-ui/pro/lib/spin/style";
import _Spin from "choerodon-ui/pro/lib/spin";
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/select/style";
import _Select from "choerodon-ui/pro/lib/select";
import "choerodon-ui/pro/lib/select-box/style";
import _SelectBox from "choerodon-ui/pro/lib/select-box";

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, { useMemo } from 'react';
import { observer } from 'mobx-react-lite';
import Tips from "../../../../components/new-tips";
import { useAddTemplateStore } from "./stores";
import { mapping } from "./stores/FormDataSet";
import "./index.less";
export default observer(function () {
  var _useAddTemplateStore = useAddTemplateStore(),
      formDs = _useAddTemplateStore.formDs,
      templateId = _useAddTemplateStore.templateId,
      modal = _useAddTemplateStore.modal,
      refresh = _useAddTemplateStore.refresh,
      organizationId = _useAddTemplateStore.organizationId,
      prefixCls = _useAddTemplateStore.prefixCls,
      formatMessage = _useAddTemplateStore.formatMessage,
      intlPrefix = _useAddTemplateStore.intlPrefix;

  var record = useMemo(function () {
    return formDs.current;
  }, [formDs.current]);
  modal.handleOk( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
    var res;
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.prev = 0;
            _context.next = 3;
            return formDs.submit();

          case 3:
            res = _context.sent;

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

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

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

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

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

  var renderCreateWayDom = function renderCreateWayDom() {
    if (!record) {
      return '';
    }

    var createWayValue = record.get(mapping.createWay.name);
    var urlTips = /*#__PURE__*/React.createElement(Tips, {
      helpText: formatMessage({
        id: "".concat(intlPrefix, ".url.tips")
      })
    });

    switch (createWayValue) {
      case 'template':
        return [organizationId ? /*#__PURE__*/React.createElement(_SelectBox, {
          name: mapping.templateSource.name
        }) : null, /*#__PURE__*/React.createElement(_Select, {
          name: mapping.appTemplate.name,
          searchable: true,
          searchMatcher: "param",
          addonAfter: /*#__PURE__*/React.createElement(Tips, {
            helpText: formatMessage({
              id: "".concat(intlPrefix, ".tips")
            })
          })
        })];
        break;

      case 'gitlab':
        return [/*#__PURE__*/React.createElement(_TextField, {
          name: mapping.gitlabAddress.name,
          addonAfter: urlTips
        }), /*#__PURE__*/React.createElement(_Password, {
          name: mapping.token.name
        })];
        break;

      case 'github':
        return [/*#__PURE__*/React.createElement(_TextField, {
          name: mapping.githubAddress.name,
          addonAfter: urlTips
        })];

      default:
        return '';
    }
  }; // @ts-ignore


  if (!record || record.status === 'loading') {
    return /*#__PURE__*/React.createElement(_Spin, null);
  }

  return /*#__PURE__*/React.createElement(_Form, {
    className: "".concat(prefixCls),
    dataSet: formDs
  }, /*#__PURE__*/React.createElement(_TextField, {
    name: mapping.templateName.name,
    autoFocus: true
  }), /*#__PURE__*/React.createElement(_TextField, {
    name: mapping.templateCode.name,
    disabled: !!templateId
  }), !templateId && [/*#__PURE__*/React.createElement(_SelectBox, {
    name: mapping.createWay.name
  }), renderCreateWayDom()]);
});