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 { Choerodon } from '@choerodon/boot';
import forEach from 'lodash/forEach';
import map from 'lodash/map';
import includes from 'lodash/includes';
import isEmpty from 'lodash/isEmpty';
export default (function (_ref) {
  var formatMessage = _ref.formatMessage,
      appServiceDs = _ref.appServiceDs,
      chartsDs = _ref.chartsDs,
      envDs = _ref.envDs,
      tableDs = _ref.tableDs;

  function handleUpdate(_x) {
    return _handleUpdate.apply(this, arguments);
  }

  function _handleUpdate() {
    _handleUpdate = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(_ref2) {
      var name, value, record, newValue, appServiceList, newAppServiceIds, ids;
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              name = _ref2.name, value = _ref2.value, record = _ref2.record;

              if (!(name === 'appServiceIds' && isEmpty(value))) {
                _context.next = 3;
                break;
              }

              return _context.abrupt("return");

            case 3:
              chartsDs.setQueryParameter(name, value);
              tableDs.setQueryParameter(name, value);

              if (name === 'appServiceIds' && value.length > 5) {
                newValue = value.splice(0, 5);
                chartsDs.setQueryParameter(name, newValue);
                tableDs.setQueryParameter(name, newValue);
                record.set('appServiceIds', newValue);
                Choerodon.prompt(formatMessage({
                  id: 'report.deploy-duration.apps'
                }));
              }

              if (!(name === 'envId')) {
                _context.next = 16;
                break;
              }

              appServiceDs.setQueryParameter(name, value);
              _context.next = 10;
              return appServiceDs.query();

            case 10:
              appServiceList = appServiceDs.toData();
              newAppServiceIds = [];
              ids = map(appServiceList, 'id');
              forEach(record.get('appServiceIds'), function (appServiceId) {
                if (appServiceId && includes(ids, appServiceId)) {
                  newAppServiceIds.push(appServiceId);
                }
              });

              if (!newAppServiceIds.length && appServiceList.length) {
                newAppServiceIds.push(appServiceList[0].id);
              }

              record.set('appServiceIds', newAppServiceIds);

            case 16:
              chartsDs.query();
              tableDs.query();

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

  return {
    autoCreate: true,
    fields: [{
      name: 'envId',
      type: 'string',
      textField: 'name',
      valueField: 'id',
      label: formatMessage({
        id: 'envName'
      }),
      options: envDs
    }, {
      name: 'appServiceIds',
      textField: 'name',
      valueField: 'id',
      multiple: true,
      label: formatMessage({
        id: 'deploy.appName'
      }),
      options: appServiceDs
    }],
    events: {
      update: handleUpdate
    }
  };
});