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 isEmpty from 'lodash/isEmpty';
export default (function (_ref) {
  var optionsDs = _ref.optionsDs,
      ReportsStore = _ref.ReportsStore,
      tableDs = _ref.tableDs,
      chartDs = _ref.chartDs,
      formatMessage = _ref.formatMessage;

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

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

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

              return _context2.abrupt("return");

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

              if (name === 'pipelineIds' && value.length > 5) {
                newValue = value.splice(0, 5);
                chartDs.setQueryParameter(name, newValue);
                tableDs.setQueryParameter(name, newValue);
                record.set(name, newValue);
                Choerodon.prompt(formatMessage({
                  id: 'c7ncd.reports.pipeline.duration.failed'
                }));
              }

              chartDs.query();
              tableDs.query();

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

  return {
    autoCreate: true,
    autoQuery: false,
    paging: false,
    fields: [{
      name: 'pipelineIds',
      textField: 'name',
      valueField: 'id',
      multiple: true,
      options: optionsDs,
      label: '应用流水线'
    }],
    events: {
      create: function () {
        var _create = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(_ref3) {
          var record, ps, optionRecord, startTime, endTime;
          return regeneratorRuntime.wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  record = _ref3.record;
                  _context.next = 3;
                  return optionsDs.query();

                case 3:
                  ps = ['choerodon.code.project.deploy.app-deployment.pipeline.ps.create'];

                  if (optionsDs.length) {
                    optionRecord = optionsDs.get(0);

                    if (optionRecord && optionRecord.get('id')) {
                      startTime = ReportsStore.getStartTime.format('YYYY-MM-DD HH:mm:ss');
                      endTime = ReportsStore.getEndTime.format('YYYY-MM-DD HH:mm:ss');
                      chartDs.setQueryParameter('startTime', startTime);
                      chartDs.setQueryParameter('endTime', endTime);
                      tableDs.setQueryParameter('startTime', startTime);
                      tableDs.setQueryParameter('endTime', endTime);
                      record.set('pipelineIds', [optionRecord.get('id')]);
                    }
                  } else {
                    ReportsStore.judgeRole(ps);
                  }

                case 5:
                case "end":
                  return _context.stop();
              }
            }
          }, _callee);
        }));

        function create(_x2) {
          return _create.apply(this, arguments);
        }

        return create;
      }(),
      update: handleUpdate
    }
  };
});