'use strict';
var BraintreeError = require('../lib/error');
var Client = require('./client');
var getConfiguration = require('./get-configuration').getConfiguration;
var throwIfNoCallback = require('../lib/throw-if-no-callback');
var packageVersion = require('package.version');
var deferred = require('../lib/deferred');
var sharedErrors = require('../errors');
function create(options, callback) {
throwIfNoCallback(callback, 'create');
callback = deferred(callback);
if (!options.authorization) {
callback(new BraintreeError({
type: sharedErrors.INSTANTIATION_OPTION_REQUIRED.type,
code: sharedErrors.INSTANTIATION_OPTION_REQUIRED.code,
message: 'options.authorization is required when instantiating a client.'
}));
return;
}
getConfiguration(options, function (err, configuration) {
var client;
if (err) {
callback(err);
return;
}
try {
client = new Client(configuration);
} catch (clientCreationError) {
callback(clientCreationError);
return;
}
callback(null, client);
});
}
module.exports = {
create: create,
VERSION: packageVersion
};