function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import { map } from 'lodash';
import getTablePostData from "../../../../utils/getTablePostData";

function handleLoad(_ref) {
  var dataSet = _ref.dataSet;
  dataSet.forEach(function (record) {
    // eslint-disable-next-line no-param-reassign
    record.selectable = record.get('deleteFlag');
  });
}

export default (function (formatMessage, projectId, id) {
  return {
    autoQuery: true,
    pageSize: 10,
    selection: false,
    transport: {
      read: function read(_ref2) {
        var data = _ref2.data;
        var postData = getTablePostData(data);

        if (id) {
          return {
            url: "/devops/v1/projects/".concat(projectId, "/app_service_versions/page_by_options?app_service_id=").concat(id, "&deploy_only=false&do_page=true"),
            method: 'post',
            data: postData,
            transformResponse: function transformResponse(res) {
              var newRes = res;

              try {
                newRes = JSON.parse(newRes);
                newRes.content = newRes.content.map(function (i) {
                  return _objectSpread(_objectSpread({}, i), {}, {
                    checked: false
                  });
                });
                return newRes;
              } catch (e) {
                newRes.content = newRes.content.map(function (i) {
                  return _objectSpread(_objectSpread({}, i), {}, {
                    checked: false
                  });
                });
                return newRes;
              }
            }
          };
        }

        return undefined;
      },
      destroy: function destroy(_ref3) {
        var data = _ref3.data;
        return {
          url: "/devops/v1/projects/".concat(projectId, "/app_service_versions/batch?app_service_id=").concat(id),
          method: 'delete',
          data: map(data, function (item) {
            return item.id;
          })
        };
      }
    },
    fields: [{
      name: 'version',
      type: 'string',
      label: formatMessage({
        id: 'version'
      })
    }, {
      name: 'creationDate',
      type: 'dateTime',
      label: formatMessage({
        id: 'createDate'
      })
    }, {
      name: 'id',
      type: 'string'
    }],
    queryFields: [{
      name: 'version',
      type: 'string',
      label: formatMessage({
        id: 'version'
      })
    }],
    events: {
      load: handleLoad
    }
  };
});