import "choerodon-ui/pro/lib/form/style";
import _Form from "@hzero-front-ui/c7n-ui/lib/FormPro";
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, { useEffect, useState, Fragment, useMemo } from 'react';
import { FormattedMessage } from 'react-intl';
import { observer } from 'mobx-react-lite';
import { Choerodon } from '@choerodon/boot';
import map from 'lodash/map';
import find from 'lodash/find';
import Tips from "../../../../../components/new-tips";
import { useShareFormStore } from "./stores";
import "./index.less";
var Option = _Select.Option;
export default observer(function () {
  var _useShareFormStore = useShareFormStore(),
      formatMessage = _useShareFormStore.intl.formatMessage,
      intlPrefix = _useShareFormStore.intlPrefix,
      prefixCls = _useShareFormStore.prefixCls,
      VERSION_TYPE = _useShareFormStore.VERSION_TYPE,
      modal = _useShareFormStore.modal,
      formDs = _useShareFormStore.formDs,
      refresh = _useShareFormStore.refresh;

  var record = useMemo(function () {
    return formDs.current;
  }, [formDs.current]);
  modal.handleOk( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            if (!(!record.get('version') && !record.get('versionType'))) {
              _context.next = 3;
              break;
            }

            record.set('hasFailed', true);
            return _context.abrupt("return", false);

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

          case 6:
            _context.t0 = _context.sent;

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

            refresh();
            _context.next = 12;
            break;

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

          case 12:
            _context.next = 18;
            break;

          case 14:
            _context.prev = 14;
            _context.t1 = _context["catch"](3);
            Choerodon.handleResponseError(_context.t1);
            return _context.abrupt("return", false);

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

  function fetchLookup(_x) {
    return _fetchLookup.apply(this, arguments);
  }

  function _fetchLookup() {
    _fetchLookup = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(field) {
      var res;
      return regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              _context2.next = 2;
              return field.fetchLookup();

            case 2:
              res = _context2.sent;

              if (record.get('versionType') && record.get('version') && !find(res, function (_ref2) {
                var version = _ref2.version;
                return version === record.get('version');
              })) {
                record.set('version', null);
              }

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

  if (!record) {
    return;
  }

  return /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement(_Form, {
    record: record
  }, /*#__PURE__*/React.createElement(_Select, {
    name: "versionType",
    combo: true,
    addonAfter: /*#__PURE__*/React.createElement(Tips, {
      helpText: formatMessage({
        id: "".concat(intlPrefix, ".detail.type.tips")
      })
    })
  }, map(VERSION_TYPE, function (item) {
    return /*#__PURE__*/React.createElement(Option, {
      value: item,
      key: item
    }, item);
  })), /*#__PURE__*/React.createElement(_Select, {
    name: "version",
    searchable: true,
    searchMatcher: "version",
    addonAfter: /*#__PURE__*/React.createElement(Tips, {
      helpText: formatMessage({
        id: "".concat(intlPrefix, ".detail.version.tips")
      })
    })
  }), /*#__PURE__*/React.createElement(_Select, {
    name: "shareLevel",
    searchable: true,
    searchMatcher: "name",
    addonAfter: /*#__PURE__*/React.createElement(Tips, {
      helpText: formatMessage({
        id: "".concat(intlPrefix, ".detail.scope.tips")
      })
    })
  })), record.get('hasFailed') && /*#__PURE__*/React.createElement("span", {
    className: "".concat(prefixCls, "-share-failed")
  }, formatMessage({
    id: "".concat(intlPrefix, ".share.failed")
  })));
});