"use strict";

var _superagent = _interopRequireDefault(require("superagent"));

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

// a proxy server can be specified using standard env variables (http(s)_proxy / all_proxy),
// see github.com/Rob--W/proxy-from-env for details
require('superagent-proxy')(_superagent["default"]);

var rateProviders = {
  bitstamp: {
    url: 'https://www.bitstamp.net/api/v2/ticker/btcusd',
    parser: function parser(r) {
      return r.body.last;
    }
  },
  wasabi: {
    url: 'http://wasabiukrxmkdgve5kynjztuovbg43uxcbcxn6y2okcrsg7gb6jdmbad.onion/api/v4/btc/Offchain/exchange-rates',
    parser: function parser(r) {
      return r.body[0].rate;
    }
  }
};

if (!process.env.NO_RATES) {
  var rateProvider = rateProviders[process.env.RATE_PROVIDER || 'bitstamp'];
  if (!rateProvider) throw new Error('Invalid rate provider');

  exports.fetchRate = function (_) {
    return _superagent["default"].get(rateProvider.url).type('json').proxy().then(rateProvider.parser);
  };
}