'use strict';

var _reactRefetch = require('react-refetch');

var _csv = require('csv');

var csvConnector = _reactRefetch.connect.defaults({
  handleResponse: function handleResponse(response) {
    if (response.headers.get('content-length') === '0' || response.status === 204) {
      return;
    }

    var csv = response.text();

    if (response.status >= 200 && response.status < 300) {
      return csv.then(function (text) {
        return new Promise(function (resolve, reject) {
          (0, _csv.parse)(text, function (err, data) {
            if (err) {
              reject(err);
            }
            resolve(data);
          });
        });
      });
    } else {
      return csv.then(function (cause) {
        return Promise.reject(new Error(cause));
      });
    }
  }
});

csvConnector(function (props) {
  return {
    userFetch: '/users/' + props.userId + '.csv'
  };
})(Profile);