function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

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); }); }; }

/* eslint-disable import/no-anonymous-default-export */
import uuidV1 from 'uuid/v1';
import { axios } from '@choerodon/boot';
import forEach from 'lodash/forEach';
import JSONbig from 'json-bigint';
import addCDTaskDataSetMap from "./addCDTaskDataSetMap";

function getDefaultInstanceName(appServiceCode) {
  return appServiceCode ? "".concat(appServiceCode.substring(0, 24), "-").concat(uuidV1().substring(0, 5)) : uuidV1().substring(0, 30);
}

function checkName(_x, _x2, _x3) {
  return _checkName.apply(this, arguments);
}

function _checkName() {
  _checkName = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(value, projectId, record) {
    var res;
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            if (record.get('envId')) {
              _context.next = 2;
              break;
            }

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

          case 2:
            if (record.get('type') === 'cdDeploy' && record.get('deployType') === 'create') {
              _context.next = 4;
              break;
            }

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

          case 4:
            _context.prev = 4;
            _context.next = 7;
            return axios.get("/devops/v1/projects/".concat(projectId, "/app_service_instances/check_name?env_id=").concat(record.get('envId'), "&instance_name=").concat(value));

          case 7:
            res = _context.sent;

            if (!(res && res.failed || !res)) {
              _context.next = 10;
              break;
            }

            return _context.abrupt("return", '格式有误');

          case 10:
            return _context.abrupt("return", true);

          case 13:
            _context.prev = 13;
            _context.t0 = _context["catch"](4);
            return _context.abrupt("return", '校验失败');

          case 16:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, null, [[4, 13]]);
  }));
  return _checkName.apply(this, arguments);
}

export default (function (projectId, PipelineCreateFormDataSet, organizationId, useStore, appServiceCode, random) {
  return {
    autoCreate: true,
    fields: [{
      name: 'type',
      type: 'string',
      label: '任务类型',
      required: true,
      defaultValue: 'cdDeploy'
    }, {
      name: 'name',
      type: 'string',
      label: '任务名称',
      required: true
    }, {
      name: 'glyyfw',
      type: 'string',
      label: '关联应用服务',
      required: true,
      disabled: true
    }, {
      name: addCDTaskDataSetMap.apiTestMission,
      type: 'string',
      label: 'API测试任务',
      textField: 'name',
      valueField: 'id',
      dynamicProps: {
        required: function required(_ref) {
          var record = _ref.record;
          return record.get('type') === addCDTaskDataSetMap.apiTest;
        }
      },
      lookupAxiosConfig: function lookupAxiosConfig() {
        return {
          method: 'get',
          url: "/test/v1/projects/".concat(projectId, "/api_test/tasks/paging?random=").concat(random),
          transformResponse: function transformResponse(res) {
            var newRes = res;

            try {
              newRes = JSON.parse(res);
              useStore.setApiTestArray(newRes.content);
              return newRes;
            } catch (e) {
              return newRes;
            }
          }
        };
      }
    }, {
      name: addCDTaskDataSetMap.relativeMission,
      type: 'string',
      label: '关联部署任务'
    }, {
      name: 'triggerType',
      type: 'string',
      label: '匹配类型',
      required: true,
      defaultValue: 'refs'
    }, {
      name: 'envId',
      type: 'string',
      label: '环境名称',
      required: true,
      textField: 'name',
      valueField: 'id',
      dynamicProps: {
        required: function required(_ref2) {
          var record = _ref2.record;
          return record.get('type') === 'cdDeploy';
        }
      },
      lookupAxiosConfig: function lookupAxiosConfig() {
        return {
          method: 'get',
          url: "/devops/v1/projects/".concat(projectId, "/envs/list_by_active?active=true&random=").concat(random),
          transformResponse: function transformResponse(res) {
            var newRes = res;

            try {
              newRes = JSONbig.parse(res);
              useStore.setValueIdList(newRes.filter(function (r) {
                return r.permission;
              }));
              return newRes.filter(function (r) {
                return r.permission;
              });
            } catch (e) {
              return newRes;
            }
          }
        };
      }
    }, {
      name: addCDTaskDataSetMap.triggersTasks.name,
      type: 'boolean',
      label: '是否允许非环境人员触发此部署任务',
      defaultValue: addCDTaskDataSetMap.triggersTasks.values[0]
    }, {
      // name: 'bsms',
      name: 'deployType',
      type: 'string',
      label: '部署模式',
      defaultValue: 'create'
    }, {
      name: 'instanceName',
      type: 'string',
      label: '实例名称',
      validator: function validator(value, name, record) {
        return checkName(value, projectId, record);
      },
      dynamicProps: {
        required: function required(_ref3) {
          var record = _ref3.record;
          return record.get('type') === 'cdDeploy' && record.get('deployType') === 'create';
        }
      },
      defaultValue: getDefaultInstanceName(appServiceCode)
    }, {
      name: 'instanceId',
      type: 'string',
      label: '选择要替换的实例',
      textField: 'code',
      valueField: 'id',
      dynamicProps: {
        required: function required(_ref4) {
          var record = _ref4.record;
          return record.get('deployType') === 'update';
        },
        disabled: function disabled(_ref5) {
          var record = _ref5.record;
          return !record.get('envId');
        },
        lookupAxiosConfig: function lookupAxiosConfig(_ref6) {
          var record = _ref6.record;
          return {
            method: 'get',
            url: record.get('envId') && "/devops/v1/projects/".concat(projectId, "/app_service_instances/list_running_and_failed?app_service_id=").concat(PipelineCreateFormDataSet.current.get('appServiceId'), "&env_id=").concat(record.get('envId'), "&random=").concat(random),
            transformResponse: function transformResponse(res) {
              var newRes = res;

              try {
                newRes = JSONbig.parse(newRes);
                useStore.setInstanceList(newRes);
                return newRes;
              } catch (e) {
                return newRes;
              }
            }
          };
        }
      }
    }, {
      name: 'valueId',
      type: 'string',
      label: '部署配置',
      textField: 'name',
      valueField: 'id',
      dynamicProps: {
        required: function required(_ref7) {
          var record = _ref7.record;
          return record.get('type') === 'cdDeploy';
        },
        disabled: function disabled(_ref8) {
          var record = _ref8.record;
          return !record.get('envId');
        },
        lookupAxiosConfig: function lookupAxiosConfig(_ref9) {
          var record = _ref9.record;
          return {
            method: 'get',
            url: record.get('envId') && "/devops/v1/projects/".concat(projectId, "/deploy_value/list_by_env_and_app?app_service_id=").concat(PipelineCreateFormDataSet.current.get('appServiceId'), "&env_id=").concat(record.get('envId'), "&random=").concat(random, "&createValueRandom=").concat(useStore.getValueIdRandom),
            transformResponse: function transformResponse(res) {
              var newRes = res;

              try {
                newRes = JSON.parse(res);
                newRes.push({
                  name: '创建部署配置',
                  id: 'create'
                });
                useStore.setValueIdList(newRes);
                return newRes;
              } catch (e) {
                newRes.push({
                  name: '创建部署配置',
                  id: 'create'
                });
                useStore.setValueIdList(newRes);
                return newRes;
              }
            }
          };
        }
      }
    }, {
      name: addCDTaskDataSetMap.hostSource,
      type: 'string',
      label: '主机来源',
      defaultValue: addCDTaskDataSetMap.alreadyhost
    }, {
      name: addCDTaskDataSetMap.host,
      type: 'string',
      label: '主机',
      textField: 'name',
      valueField: 'id',
      lookupAxiosConfig: function lookupAxiosConfig() {
        return {
          method: 'post',
          url: "/devops/v1/projects/".concat(projectId, "/hosts/page_by_options?random=").concat(random),
          data: {
            searchParam: {
              type: 'deploy'
            },
            params: []
          },
          transformResponse: function transformResponse(res) {
            var newRes = res;

            try {
              newRes = JSONbig.parse(newRes);
              useStore.setHostList(newRes.content);
              return newRes;
            } catch (e) {
              return newRes;
            }
          }
        };
      }
    }, {
      name: 'hostIp',
      type: 'string',
      label: 'IP',
      dynamicProps: {
        disabled: function disabled(_ref10) {
          var record = _ref10.record;
          return record.get(addCDTaskDataSetMap.hostSource) === addCDTaskDataSetMap.alreadyhost;
        },
        required: function required(_ref11) {
          var record = _ref11.record;
          return record.get('type') === 'cdHost';
        }
      }
    }, {
      name: 'hostPort',
      type: 'string',
      label: '端口',
      dynamicProps: {
        disabled: function disabled(_ref12) {
          var record = _ref12.record;
          return record.get(addCDTaskDataSetMap.hostSource) === addCDTaskDataSetMap.alreadyhost;
        },
        required: function required(_ref13) {
          var record = _ref13.record;
          return record.get('type') === 'cdHost';
        }
      }
    }, {
      name: 'authType',
      type: 'string',
      label: '账号配置',
      defaultValue: 'accountPassword'
    }, {
      name: 'username',
      type: 'string',
      label: '用户名',
      dynamicProps: {
        required: function required(_ref14) {
          var record = _ref14.record;
          return record.get('type') === 'cdHost' && record.get(addCDTaskDataSetMap.hostSource) === addCDTaskDataSetMap.customhost;
        }
      }
    }, {
      name: 'password',
      type: 'string',
      label: '密码',
      dynamicProps: {
        required: function required(_ref15) {
          var record = _ref15.record;
          return record.get('type') === 'cdHost' && record.get('authType') === 'accountPassword' && record.get(addCDTaskDataSetMap.hostSource) === addCDTaskDataSetMap.customhost;
        }
      }
    }, {
      name: 'hostDeployType',
      type: 'string',
      label: '部署模式',
      defaultValue: 'image'
    }, {
      name: 'deploySource',
      type: 'string',
      label: '部署来源',
      defaultValue: 'matchDeploy',
      dynamicProps: {
        required: function required(_ref16) {
          var record = _ref16.record;
          return record.get('type') === 'cdHost' && (record.get('hostDeployType') === 'image' || record.get('hostDeployType') === 'jar');
        }
      }
    }, {
      name: 'workingPath',
      type: 'string',
      label: '工作目录',
      defaultValue: './'
    }, {
      name: 'pipelineTask',
      type: 'string',
      label: '关联构建任务',
      textField: 'pipelineTask',
      valueField: 'pipelineTask',
      dynamicProps: {
        required: function required(_ref17) {
          var record = _ref17.record;
          return record.get('type') === 'cdHost' && (record.get('hostDeployType') === 'image' && record.get('deploySource') === 'pipelineDeploy' || record.get('hostDeployType') === 'jar' && record.get('deploySource') === 'pipelineDeploy');
        }
      }
    }, {
      name: 'repoId',
      type: 'string',
      label: '项目镜像仓库',
      textField: 'repoName',
      valueField: 'repoId',
      lookupAxiosConfig: function lookupAxiosConfig() {
        return {
          method: 'get',
          url: "/rdupm/v1/harbor-choerodon-repos/listImageRepo?projectId=".concat(projectId),
          transformResponse: function transformResponse(data) {
            var newData = data;

            try {
              newData = JSON.parse(newData);
              useStore.setRepoList(newData);
              return newData;
            } catch (e) {
              useStore.setRepoList(newData);
              return newData;
            }
          }
        };
      },
      dynamicProps: {
        required: function required(_ref18) {
          var record = _ref18.record;
          return record.get('type') === 'cdHost' && record.get('hostDeployType') === 'image' && record.get('deploySource') === 'matchDeploy';
        }
      }
    }, {
      name: 'imageId',
      type: 'string',
      label: '镜像',
      textField: 'imageName',
      valueField: 'imageId',
      dynamicProps: {
        disabled: function disabled(_ref19) {
          var record = _ref19.record;
          return !record.get('repoId');
        },
        required: function required(_ref20) {
          var record = _ref20.record;
          return record.get('type') === 'cdHost' && record.get('hostDeployType') === 'image' && record.get('deploySource') === 'matchDeploy';
        },
        lookupAxiosConfig: function lookupAxiosConfig(_ref21) {
          var record = _ref21.record;
          return {
            method: 'get',
            url: record.get('repoId') && "rdupm/v1/harbor-choerodon-repos/listHarborImage?repoId=".concat(record.get('repoId'), "&repoType=").concat(function () {
              var _lookup$find;

              var _record$getField = record.getField('repoId'),
                  lookup = _record$getField.lookup;

              return lookup === null || lookup === void 0 ? void 0 : (_lookup$find = lookup.find(function (l) {
                return String(l.repoId) === String(record.get('repoId'));
              })) === null || _lookup$find === void 0 ? void 0 : _lookup$find.repoType;
            }()),
            transformResponse: function transformResponse(data) {
              var newData = data;

              try {
                newData = JSON.parse(newData);
                useStore.setImageList(newData);
                return newData;
              } catch (e) {
                useStore.setImageList(newData);
                return newData;
              }
            }
          };
        }
      }
    }, {
      name: 'matchType',
      type: 'string',
      label: '匹配类型',
      dynamicProps: {
        required: function required(_ref22) {
          var record = _ref22.record;
          return record.get('type') === 'cdHost' && record.get('hostDeployType') === 'image' && record.get('deploySource') === 'matchDeploy';
        }
      }
    }, {
      name: 'matchContent',
      type: 'string',
      label: '镜像版本匹配',
      dynamicProps: {
        required: function required(_ref23) {
          var record = _ref23.record;
          return record.get('type') === 'cdHost' && record.get('hostDeployType') === 'image' && record.get('deploySource') === 'matchDeploy';
        }
      }
    }, {
      name: 'containerName',
      type: 'string',
      label: '容器名称',
      required: true,
      dynamicProps: {
        required: function required(_ref24) {
          var record = _ref24.record;
          return record.get('type') === 'cdHost' && record.get('hostDeployType') === 'image' && (record.get('deploySource') === 'matchDeploy' || record.get('deploySource') === 'pipelineDeploy');
        }
      }
    }, {
      name: 'serverName',
      type: 'string',
      label: 'Nexus服务',
      dynamicProps: {
        required: function required(_ref25) {
          var record = _ref25.record;
          return record.get('type') === 'cdHost' && record.get('hostDeployType') === 'jar' && record.get('deploySource') === 'matchDeploy';
        }
      },
      textField: 'serverName',
      valueField: 'configId',
      lookupAxiosConfig: function lookupAxiosConfig() {
        return {
          method: 'get',
          url: "/devops/v1/nexus/choerodon/".concat(organizationId, "/project/").concat(projectId, "/nexus/server/list")
        };
      }
    }, {
      name: 'repositoryId',
      type: 'string',
      label: '项目制品库',
      textField: 'neRepositoryName',
      valueField: 'repositoryId',
      dynamicProps: {
        required: function required(_ref26) {
          var record = _ref26.record;
          return record.get('type') === 'cdHost' && record.get('hostDeployType') === 'jar' && record.get('deploySource') === 'matchDeploy';
        },
        disabled: function disabled(_ref27) {
          var record = _ref27.record;
          return !record.get('serverName');
        },
        lookupAxiosConfig: function lookupAxiosConfig(_ref28) {
          var record = _ref28.record;
          return {
            method: 'get',
            url: record.get('serverName') && "rdupm/v1/nexus-repositorys/choerodon/".concat(organizationId, "/project/").concat(projectId, "/repo/maven/list?configId=").concat(record.get('serverName'))
          };
        }
      }
    }, {
      name: 'groupId',
      type: 'string',
      label: 'groupID',
      textField: 'name',
      valueField: 'value',
      dynamicProps: {
        disabled: function disabled(_ref29) {
          var record = _ref29.record;
          return !record.get('repositoryId');
        },
        lookupAxiosConfig: function lookupAxiosConfig(_ref30) {
          var record = _ref30.record;
          return {
            method: 'get',
            url: record.get('repositoryId') && "/rdupm/v1/nexus-repositorys/choerodon/".concat(organizationId, "/project/").concat(projectId, "/repo/maven/groupId?repositoryId=").concat(record.get('repositoryId')),
            transformResponse: function transformResponse(data) {
              try {
                var array = JSON.parse(data);
                return array.map(function (i) {
                  return {
                    value: i,
                    name: i
                  };
                });
              } catch (e) {
                return data;
              }
            }
          };
        },
        required: function required(_ref31) {
          var record = _ref31.record;
          return record.get('type') === 'cdHost' && record.get('hostDeployType') === 'jar' && record.get('deploySource') === 'matchDeploy';
        }
      }
    }, {
      name: 'artifactId',
      type: 'string',
      label: 'artifactID',
      textField: 'name',
      valueField: 'value',
      dynamicProps: {
        disabled: function disabled(_ref32) {
          var record = _ref32.record;
          return !record.get('repositoryId');
        },
        required: function required(_ref33) {
          var record = _ref33.record;
          return record.get('type') === 'cdHost' && record.get('hostDeployType') === 'jar' && record.get('deploySource') === 'matchDeploy';
        },
        lookupAxiosConfig: function lookupAxiosConfig(_ref34) {
          var record = _ref34.record;
          return {
            method: 'get',
            url: record.get('repositoryId') && "/rdupm/v1/nexus-repositorys/choerodon/".concat(organizationId, "/project/").concat(projectId, "/repo/maven/artifactId?repositoryId=").concat(record.get('repositoryId')),
            transformResponse: function transformResponse(data) {
              try {
                var array = JSON.parse(data);
                return array.map(function (i) {
                  return {
                    value: i,
                    name: i
                  };
                });
              } catch (e) {
                return data;
              }
            }
          };
        }
      }
    }, {
      name: 'versionRegular',
      type: 'string',
      label: 'jar包版本正则匹配',
      dynamicProps: {
        required: function required(_ref35) {
          var record = _ref35.record;
          return record.get('type') === 'cdHost' && record.get('hostDeployType') === 'jar' && record.get('deploySource') === 'matchDeploy';
        }
      }
    }, {
      name: 'triggerValue',
      type: 'string',
      label: '触发分支'
    }, {
      name: addCDTaskDataSetMap.pipelineCallbackAddress,
      type: 'string',
      label: '流水线回调地址',
      required: false,
      disabled: true
    }, {
      name: addCDTaskDataSetMap.externalAddress,
      type: 'string',
      label: '外部地址',
      required: true,
      dynamicProps: {
        required: function required(_ref36) {
          var record = _ref36.record;
          return record.get('type') === addCDTaskDataSetMap.externalStuck;
        }
      }
    }, {
      name: addCDTaskDataSetMap.externalToken,
      type: 'string',
      label: 'Token'
    }, {
      name: addCDTaskDataSetMap.missionDes,
      type: 'string',
      label: '任务描述',
      maxLength: 100
    }, {
      name: 'pageSize',
      type: 'number',
      defaultValue: 20
    }, {
      name: 'cdAuditUserIds',
      type: 'object',
      label: '审核人员',
      dynamicProps: {
        required: function required(_ref37) {
          var record = _ref37.record;
          return record.get('type') === 'cdAudit';
        }
      },
      textField: 'realName',
      multiple: true,
      valueField: 'id',
      lookupAxiosConfig: function lookupAxiosConfig(_ref38) {
        var _dataSet$current;

        var params = _ref38.params,
            dataSet = _ref38.dataSet;
        var cdAuditIdsArrayObj = (_dataSet$current = dataSet.current) === null || _dataSet$current === void 0 ? void 0 : _dataSet$current.get('cdAuditUserIds');
        var cdAuditIds = [];
        forEach(cdAuditIdsArrayObj, function (obj) {
          if (typeof obj === 'string') {
            cdAuditIds.push(obj);
          } else if (typeof obj === 'object') {
            cdAuditIds.push(obj === null || obj === void 0 ? void 0 : obj.id);
          }
        });

        if (params.realName && params.id) {
          cdAuditIds = [].concat(_toConsumableArray(cdAuditIds), [params.id]);
        }

        return {
          method: 'post',
          url: "/devops/v1/projects/".concat(projectId, "/users/app_services/").concat(PipelineCreateFormDataSet.current.get('appServiceId'), "?page=0&size=20"),
          data: {
            userName: params.realName || '',
            ids: cdAuditIds || []
          },
          transformResponse: function transformResponse(res) {
            var newRes;

            try {
              newRes = JSON.parse(res);

              if (newRes.totalElements % 20 === 0 && newRes.content.length !== 0) {
                newRes.content.push({
                  id: 'more',
                  realName: '加载更多'
                });
              }

              return newRes;
            } catch (e) {
              return res;
            }
          }
        };
      }
    }, {
      name: 'countersigned',
      type: 'number',
      label: '审核模式',
      dynamicProps: {
        required: function required(_ref39) {
          var _record$get;

          var record = _ref39.record;
          return record.get('type') === 'cdAudit' && ((_record$get = record.get('cdAuditUserIds')) === null || _record$get === void 0 ? void 0 : _record$get.length) > 1;
        }
      }
    }, {
      name: addCDTaskDataSetMap.whetherBlock,
      type: 'boolean',
      label: '执行成功率低于阈值后是否阻塞',
      defaultValue: true
    }, {
      name: addCDTaskDataSetMap.alarm,
      type: 'boolean',
      label: '是否启用告警设置',
      defaultValue: false
    }, {
      name: addCDTaskDataSetMap.threshold,
      type: 'string',
      label: '执行阈值',
      dynamicProps: {
        required: function required(_ref40) {
          var record = _ref40.record;
          return record.get(addCDTaskDataSetMap.alarm);
        },
        disabled: function disabled(_ref41) {
          var record = _ref41.record;
          return !record.get(addCDTaskDataSetMap.alarm);
        }
      }
    }, {
      name: addCDTaskDataSetMap.notifyObject,
      type: 'string',
      label: '通知对象',
      dynamicProps: {
        required: function required(_ref42) {
          var record = _ref42.record;
          return record.get(addCDTaskDataSetMap.alarm);
        },
        disabled: function disabled(_ref43) {
          var record = _ref43.record;
          return !record.get(addCDTaskDataSetMap.alarm);
        }
      },
      textField: 'realName',
      valueField: 'id',
      multiple: true,
      lookupAxiosConfig: {
        url: "/iam/choerodon/v1/projects/".concat(projectId, "/users?page=0&size=20"),
        method: 'get'
      }
    }, {
      name: addCDTaskDataSetMap.notifyWay,
      type: 'string',
      label: '通知方式',
      multiple: ',',
      dynamicProps: {
        disabled: function disabled(_ref44) {
          var record = _ref44.record;
          return !record.get(addCDTaskDataSetMap.alarm);
        }
      }
    }]
  };
});