import "choerodon-ui/pro/lib/tooltip/style";
import _Tooltip from "choerodon-ui/pro/lib/tooltip";

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, useEffect, Fragment } from 'react';
import { observer } from 'mobx-react-lite';
import { injectIntl } from 'react-intl';
import { Choerodon } from '@choerodon/boot';
import { some, map, findIndex } from 'lodash';
import DynamicSelect from "../../../../../../../components/dynamic-select-new";
import { handlePromptError } from "../../../../../../../utils";
import "./index.less";
var LinkService = observer(function (props) {
  var modal = props.modal,
      store = props.store,
      tree = props.tree,
      onOk = props.onOk,
      intlPrefix = props.intlPrefix,
      formatMessage = props.intl.formatMessage,
      modalStores = props.modalStores;
  var linkServiceDs = modalStores.linkServiceDs,
      linkServiceOptionsDs = modalStores.linkServiceOptionsDs;
  useEffect(function () {
    linkServiceDs.getField('appServiceId').set('options', linkServiceOptionsDs);
    linkServiceOptionsDs.query();
  }, []);
  modal.handleOk( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
    var servers, res;
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            servers = map(linkServiceDs.created, function (_ref2) {
              var appServiceId = _ref2.data.appServiceId;
              return appServiceId;
            });

            if (!(findIndex(servers, function (item) {
              return !item;
            }) > -1)) {
              _context.next = 3;
              break;
            }

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

          case 3:
            if (!(!servers || servers.length === 0)) {
              _context.next = 5;
              break;
            }

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

          case 5:
            _context.prev = 5;
            _context.next = 8;
            return onOk(servers);

          case 8:
            res = _context.sent;

            if (handlePromptError(res)) {
              _context.next = 11;
              break;
            }

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

          case 11:
            tree.query();
            _context.next = 18;
            break;

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

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

  function optionsFilter(optionRecord) {
    var flag = some(linkServiceDs.created, function (item) {
      return item.get('appServiceId') === optionRecord.get('id');
    });
    return !flag;
  }

  function optionsRenderer(_ref3) {
    var record = _ref3.record;
    var name = record.get('name');
    return /*#__PURE__*/React.createElement(_Tooltip, {
      title: name
    }, name);
  }

  return /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement(DynamicSelect, {
    selectDataSet: linkServiceDs,
    optionsFilter: optionsFilter,
    optionsRenderer: optionsRenderer,
    selectName: "appServiceId",
    addText: formatMessage({
      id: "".concat(intlPrefix, ".environment.add.service")
    })
  }));
});
export default injectIntl(LinkService);