import map from 'lodash/map';
import compact from 'lodash/compact';
import uniq from 'lodash/uniq';
import CodeManagerApis from "../../../../apis";
export default (function (_ref) {
  var projectId = _ref.projectId,
      formatMessage = _ref.formatMessage,
      appServiceId = _ref.appServiceId,
      objectVersionNumber = _ref.objectVersionNumber,
      branchName = _ref.branchName,
      projectOptionsDs = _ref.projectOptionsDs;
  return {
    autoCreate: false,
    autoQuery: false,
    selection: 'single',
    paging: false,
    fields: [{
      name: 'issue',
      type: 'object',
      textField: 'summary',
      valueField: 'issueId',
      label: formatMessage({
        id: 'branch.issueName'
      }),
      lookupAxiosConfig: function lookupAxiosConfig(_ref2) {
        var _project$id;

        var record = _ref2.record;
        var project = record === null || record === void 0 ? void 0 : record.get('project');
        var issue = record === null || record === void 0 ? void 0 : record.get('issue');
        var selectedProjectId = (_project$id = project === null || project === void 0 ? void 0 : project.id) !== null && _project$id !== void 0 ? _project$id : projectId;
        var issueId = issue === null || issue === void 0 ? void 0 : issue.issueId;
        return {
          url: CodeManagerApis.loadSummaryData(selectedProjectId, issueId !== null && issueId !== void 0 ? issueId : ''),
          method: 'get'
        };
      }
    }, {
      name: 'project',
      type: 'object',
      textField: 'name',
      valueField: 'id',
      label: formatMessage({
        id: 'branch.issue.source'
      }),
      options: projectOptionsDs
    }],
    transport: {
      submit: function submit(_ref3) {
        var dataSet = _ref3.dataSet;
        return {
          url: "/devops/v1/projects/".concat(projectId, "/app_service/").concat(appServiceId, "/git/update_branch_issue"),
          method: 'put',
          transformRequest: function transformRequest() {
            var issueIds = dataSet.map(function (record) {
              var _record$get;

              return record === null || record === void 0 ? void 0 : (_record$get = record.get('issue')) === null || _record$get === void 0 ? void 0 : _record$get.issueId;
            });
            var newIssueIds = uniq(compact(issueIds || []) || []);
            var postData = {
              appServiceId: appServiceId,
              issueIds: newIssueIds,
              objectVersionNumber: objectVersionNumber,
              branchName: branchName
            };
            return JSON.stringify(postData);
          }
        };
      }
    },
    events: {
      update: function update(_ref4) {
        var name = _ref4.name,
            value = _ref4.value,
            record = _ref4.record;

        if (name === 'project') {
          var field = record.getField('issue');
          record.get('issue') && record.set('issue', null);
          field.reset();

          if (value) {
            field.fetchLookup();
          }
        }
      }
    }
  };
});