/* eslint-disable consistent-return */
import isEmpty from 'lodash/isEmpty';

function handleLoad(_ref) {
  var dataSet = _ref.dataSet;

  if (!dataSet.length) {
    dataSet.loadData([{
      key: null,
      value: null
    }]);
  }
}

function handleUpdate(_ref2) {
  var value = _ref2.value,
      name = _ref2.name,
      record = _ref2.record,
      dataSet = _ref2.dataSet;

  if (name === 'key' && value) {
    dataSet.forEach(function (eachRecord) {
      if (record.id !== eachRecord.id) {
        eachRecord.getField('key').checkValidity();
      }
    });
  }
}

function handleRemove(_ref3) {
  var dataSet = _ref3.dataSet;
  dataSet.forEach(function (record) {
    record.getField('key').checkValidity();
  });
}

export default (function (_ref4) {
  var formatMessage = _ref4.formatMessage,
      intlPrefix = _ref4.intlPrefix,
      projectId = _ref4.projectId,
      appServiceId = _ref4.appServiceId,
      store = _ref4.store;
  var urlParams = appServiceId ? "app_service_id=".concat(appServiceId, "&level=app") : 'level=project';

  function checkKey(value, name, record) {
    var p = /^([_A-Za-z0-9])+$/;

    if (!value && !record.get('value')) {
      return;
    }

    if (!value && record.get('value')) {
      return formatMessage({
        id: "".concat(intlPrefix, ".settings.check.empty")
      });
    }

    if (p.test(value)) {
      var dataSet = record.dataSet;
      var repeatRecord = dataSet.find(function (eachRecord) {
        return eachRecord.id !== record.id && eachRecord.get('key') === value;
      });

      if (repeatRecord) {
        return formatMessage({
          id: "".concat(intlPrefix, ".settings.check.exist")
        });
      }
    } else {
      return formatMessage({
        id: "".concat(intlPrefix, ".settings.check.failed")
      });
    }
  }

  return {
    autoCreate: false,
    autoQuery: true,
    selection: false,
    paging: false,
    dataKey: null,
    transport: {
      read: {
        url: "devops/v1/projects/".concat(projectId, "/ci_variable/values?").concat(urlParams),
        method: 'get'
      },
      submit: function submit(_ref5) {
        var dataSet = _ref5.dataSet;
        var res = [];
        dataSet.toData().forEach(function (item) {
          if (!isEmpty(item) && item.key) {
            if (!item.value) {
              // eslint-disable-next-line no-param-reassign
              item.value = '';
            }

            res.push(item);
          }
        });
        return {
          url: "devops/v1/projects/".concat(projectId, "/ci_variable?").concat(urlParams),
          method: 'post',
          data: res
        };
      }
    },
    fields: [{
      name: 'key',
      type: 'string',
      label: formatMessage({
        id: 'key'
      }),
      validator: checkKey
    }, {
      name: 'value',
      type: 'string',
      label: formatMessage({
        id: 'value'
      })
    }],
    events: {
      load: handleLoad,
      update: handleUpdate,
      remove: handleRemove
    }
  };
});