'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.sendFileToS3 = undefined;
exports.getUploadUrl = getUploadUrl;

var _constants = require('../../constants');

var _Fetcher = require('../util/Fetcher');

var _Fetcher2 = _interopRequireDefault(_Fetcher);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function getUploadUrl(url) {
  var urlFetcher = new _Fetcher2.default({ callType: _constants.FILE_UPLOAD_URL_CALL });
  return urlFetcher.get(url);
}

// This fxn requires a secondary fxn as an argument.
// It gets the url to post the file to S3 from the request and returns a promise.
var sendFileToS3 = exports.sendFileToS3 = function sendFileToS3(file, onSuccess, onError, altText) {
  var fileFetcher = new _Fetcher2.default({
    asyncResponseHandler: function asyncResponseHandler() {},
    callType: _constants.FILE_UPLOAD_CALL,
    headers: { 'Content-Type': file.type },
    onError: onError
  });

  return function (res) {
    // After uploading, the signed portion of the URL is useless and incorrect
    var s3url = res.url.split(/[?#]/)[0];
    return fileFetcher.put('' + res.url, { body: file }).then(function (ref) {
      onSuccess(s3url, altText);
    });
  };
};