'use strict';
var BraintreeError = require('../lib/braintree-error');
var Client = require('./client');
var getConfiguration = require('./get-configuration').getConfiguration;
var VERSION = process.env.npm_package_version;
var Promise = require('../lib/promise');
var wrapPromise = require('@braintree/wrap-promise');
var sharedErrors = require('../lib/errors');
var cachedClients = {};
function create(options) {
if (!options.authorization) {
return Promise.reject(new BraintreeError({
type: sharedErrors.INSTANTIATION_OPTION_REQUIRED.type,
code: sharedErrors.INSTANTIATION_OPTION_REQUIRED.code,
message: 'options.authorization is required when instantiating a client.'
}));
}
if (cachedClients[options.authorization]) {
return Promise.resolve(cachedClients[options.authorization]);
}
return getConfiguration(options).then(function (configuration) {
var client;
if (options.debug) {
configuration.isDebug = true;
}
client = new Client(configuration);
cachedClients[options.authorization] = client;
return client;
});
}
function clearCache() {
cachedClients = {};
}
module.exports = {
create: wrapPromise(create),
VERSION: VERSION,
_clearCache: clearCache
};