import React, { useState } from 'react';
import forEach from 'lodash/forEach';
export default (function (_ref) {
  var formatMessage = _ref.formatMessage,
      intlPrefix = _ref.intlPrefix,
      projectId = _ref.projectId,
      appServiceId = _ref.appServiceId,
      upgradeStore = _ref.upgradeStore,
      versionId = _ref.versionId;

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

    if (dataSet.totalCount) {
      var record = dataSet.find(function (item) {
        return item.get('id') === versionId;
      });
      record && record.set('version', "".concat(record.get('version'), " (").concat(formatMessage({
        id: "".concat(intlPrefix, ".instance.current.version")
      }), ")"));
    }
  }

  return {
    autoCreate: false,
    autoQuery: false,
    selection: 'single',
    pageSize: 40,
    transport: {
      read: function read(_ref3) {
        var data = _ref3.data;
        var url = '';
        forEach(['version', 'app_service_version_id'], function (item) {
          var value = data[item];

          if (value) {
            url = "".concat(url, "&").concat(item, "=").concat(value);
          }
        });
        return {
          url: "/devops/v1/projects/".concat(projectId, "/app_service_versions/page_by_options?app_service_id=").concat(appServiceId, "&deploy_only=true&do_page=true").concat(url),
          method: 'post',
          data: null
        };
      }
    },
    fields: [{
      name: 'id',
      type: 'string'
    }, {
      name: 'version',
      type: 'string'
    }],
    events: {
      load: handleLoad
    }
  };
});