'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.GaiaHubConfig = exports.BLOCKSTACK_GAIA_HUB_LABEL = exports.uploadToGaiaHub = exports.connectToGaiaHub = undefined;
exports.getFile = getFile;
exports.putFile = putFile;
exports.deleteFile = deleteFile;

var _hub = require('./hub');

var _encryption = require('../encryption');

var _auth = require('../auth');

var _keys = require('../keys');

/**
 * Retrieves the specified file from the app's data store.
 * @param {String} path - the path to the file to read
 * @param {Boolean} decrypt - try to decrypt the data with the app private key
 * @returns {Promise} that resolves to the raw data in the file
 * or rejects with an error
 */
function getFile(path) {
  var decrypt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;

  return (0, _hub.getOrSetLocalGaiaHubConnection)().then(function (gaiaHubConfig) {
    return fetch((0, _hub.getFullReadUrl)(path, gaiaHubConfig));
  }).then(function (response) {
    if (response.status !== 200) {
      if (response.status === 404) {
        console.log('getFile ' + path + ' returned 404, returning null');
        return null;
      } else {
        throw new Error('getFile ' + path + ' failed with HTTP status ' + response.status);
      }
    }
    var contentType = response.headers.get('Content-Type');
    if (contentType === null || decrypt || contentType.startsWith('text') || contentType === 'application/json') {
      return response.text();
    } else {
      return response.arrayBuffer();
    }
  }).then(function (storedContents) {
    if (decrypt) {
      var privateKey = (0, _auth.loadUserData)().appPrivateKey;
      var cipherObject = JSON.parse(storedContents);
      return (0, _encryption.decryptECIES)(privateKey, cipherObject);
    } else {
      return storedContents;
    }
  });
}

/**
 * Stores the data provided in the app's data store to to the file specified.
 * @param {String} path - the path to store the data in
 * @param {String|Buffer} content - the data to store in the file
 * @param {Boolean} encrypt - encrypt the data with the app private key
 * @return {Promise} that resolves if the operation succeed and rejects
 * if it failed
 */
function putFile(path, content) {
  var encrypt = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;

  var contentType = 'text/plain';
  if (typeof content !== 'string') {
    contentType = 'application/octet-stream';
  }
  if (encrypt) {
    var privateKey = (0, _auth.loadUserData)().appPrivateKey;
    var publicKey = (0, _keys.getPublicKeyFromPrivate)(privateKey);
    var cipherObject = (0, _encryption.encryptECIES)(publicKey, content);
    content = JSON.stringify(cipherObject);
    contentType = 'application/json';
  }
  return (0, _hub.getOrSetLocalGaiaHubConnection)().then(function (gaiaHubConfig) {
    return (0, _hub.uploadToGaiaHub)(path, content, gaiaHubConfig, contentType);
  });
}

/**
 * Deletes the specified file from the app's data store.
 * @param {String} path - the path to the file to delete
 * @returns {Promise} that resolves when the file has been removed
 * or rejects with an error
 */
function deleteFile(path) {
  throw new Error('Delete of ' + path + ' not supported by gaia hubs');
}

exports.connectToGaiaHub = _hub.connectToGaiaHub;
exports.uploadToGaiaHub = _hub.uploadToGaiaHub;
exports.BLOCKSTACK_GAIA_HUB_LABEL = _hub.BLOCKSTACK_GAIA_HUB_LABEL;
exports.GaiaHubConfig = _hub.GaiaHubConfig;