"use strict";

var _crossFetch = _interopRequireWildcard(require("cross-fetch"));

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

var utils = require('./utils');

var errors = require('./errors');

var FormData = require('form-data');

var StreamFileStore = function StreamFileStore() {
  this.initialize.apply(this, arguments);
};

StreamFileStore.prototype = {
  initialize: function initialize(client, token) {
    this.client = client;
    this.token = token;
  },
  upload: function upload(uri, name) {
    var data = new FormData();
    var fileField;

    if (utils.isReadableStream(uri)) {
      fileField = uri;
    } else {
      fileField = {
        uri: uri,
        type: 'file',
        name: name || uri.split('/').reverse()[0]
      };
    }

    data.append('file', fileField);
    return (0, _crossFetch.default)("".concat(this.client.enrichUrl('files/'), "?api_key=").concat(this.client.apiKey), {
      method: 'post',
      body: data,
      headers: new _crossFetch.Headers({
        'Authorization': this.token
      })
    }).then(function (r) {
      return r.json();
    });
  },
  delete: function _delete(uri) {
    return this.client.delete({
      url: "files/".concat(uri),
      signature: this.token
    });
  }
};
module.exports = StreamFileStore;