/**!
 *
 * Copyright (c) 2015-2017 Cisco Systems, Inc. See LICENSE file.
 * @private
 */

// Note: this file is written using commonjs instead of import/export to
// simplify consumption by those less familiar with the current state of
// JavaScript modularization

/* eslint camelcase: [0] */

require(`@ciscospark/plugin-authorization`);
require(`@ciscospark/plugin-phone`);
// explicitly load wdm, since we're relying on preDiscoveryServices and the
// url interceptor
require(`@ciscospark/internal-plugin-wdm`);
require(`@ciscospark/plugin-logger`);
require(`@ciscospark/plugin-people`);

const {merge} = require(`lodash`);
const {registerPlugin} = require(`@ciscospark/spark-core`);
const SparkCore = require(`@ciscospark/spark-core`).default;
const Memberships = require(`./plugins/memberships`).default;
const Messages = require(`./plugins/messages`).default;
const Rooms = require(`./plugins/rooms`).default;
const Teams = require(`./plugins/teams`).default;
const TeamMemberships = require(`./plugins/team-memberships`).default;
const Webhooks = require(`./plugins/webhooks`).default;
const config = require(`./config`);

registerPlugin(`memberships`, Memberships);
registerPlugin(`messages`, Messages);
registerPlugin(`rooms`, Rooms);
registerPlugin(`teams`, Teams);
registerPlugin(`teamMemberships`, TeamMemberships);
registerPlugin(`webhooks`, Webhooks);

// documentation.js puts hashes in relative urls, so need to specify full urls
// here
/**
 * See {@link https://ciscospark.github.io/spark-js-sdk/example/browsers|Browser Guide} and
 * {@link https://ciscospark.github.io/spark-js-sdk/example/servers|Server Guide}
 * @see {@link /example/browsers|Browser Guide}
 * @see {@link /example/servers|Server Guide}
 * @class CiscoSpark
 * @extends SparkCore
 */
const CiscoSpark = SparkCore.extend({
  ciscospark: true,
  version: PACKAGE_VERSION
});

CiscoSpark.version = PACKAGE_VERSION;

/**
 * Create a new ciscospark instance.
 *
 * Note: ciscospark.init() really only applies to node apps at this time. In web
 * browsers, you'll want to stick with manipulating the ciscospark instance you
 * get from `require('ciscospark')`.
 *
 * In addition to creating spark instances when you need more than one at a time
 * (for example, on an Express route handler), you can use `ciscospark.init()`
 * to create instances with an alternate config object. You'll typically do this
 * when you want to provide an alternate storage backend:
 * ```javascript
 * const spark = ciscospark.init({
 *   config: {
 *     storage: {
 *       boundedAdapter: youCustomAdapter
 *     }
 *   }
 * })
 * ```
 * Previous versions of the sdk suggested you should pass credentials here.
 * While that still possible, we no longer document it because you should rely
 * on the storage layer instead.
 * @param {Object} attrs
 * @param {Object} attrs.config (optional)
 * @memberof CiscoSpark
 * @returns {CiscoSpark}
 */
CiscoSpark.init = function init(attrs = {}) {
  attrs.config = merge({}, config, attrs.config);
  return new CiscoSpark(attrs);
};

module.exports = CiscoSpark;

/**!
 *
 * Copyright (c) 2015-2017 Cisco Systems, Inc. See LICENSE file.
 * @private
 */

// Note: this file is written using commonjs instead of import/export to
// simplify consumption by those less familiar with the current state of
// JavaScript modularization

/* eslint camelcase: [0] */

require(`@ciscospark/plugin-authorization`);
require(`@ciscospark/plugin-phone`);
// explicitly load wdm, since we're relying on preDiscoveryServices and the
// url interceptor
require(`@ciscospark/internal-plugin-wdm`);
require(`@ciscospark/plugin-logger`);
require(`@ciscospark/plugin-people`);

const {merge} = require(`lodash`);
const {registerPlugin} = require(`@ciscospark/spark-core`);
const SparkCore = require(`@ciscospark/spark-core`).default;
const Memberships = require(`./plugins/memberships`).default;
const Messages = require(`./plugins/messages`).default;
const Rooms = require(`./plugins/rooms`).default;
const Teams = require(`./plugins/teams`).default;
const TeamMemberships = require(`./plugins/team-memberships`).default;
const Webhooks = require(`./plugins/webhooks`).default;
const config = require(`./config`);

registerPlugin(`memberships`, Memberships);
registerPlugin(`messages`, Messages);
registerPlugin(`rooms`, Rooms);
registerPlugin(`teams`, Teams);
registerPlugin(`teamMemberships`, TeamMemberships);
registerPlugin(`webhooks`, Webhooks);

// documentation.js puts hashes in relative urls, so need to specify full urls
// here
/**
 * See {@link https://ciscospark.github.io/spark-js-sdk/example/browsers|Browser Guide} and
 * {@link https://ciscospark.github.io/spark-js-sdk/example/servers|Server Guide}
 * @see {@link /example/browsers|Browser Guide}
 * @see {@link /example/servers|Server Guide}
 * @class CiscoSpark
 * @extends SparkCore
 */
const CiscoSpark = SparkCore.extend({
  ciscospark: true,
  version: PACKAGE_VERSION
});

CiscoSpark.version = PACKAGE_VERSION;

/**
 * Create a new ciscospark instance
 *
 * @example
 * <caption>Create a new CiscoSpark instance configured for your OAuth client</caption>
 * const ciscospark = CiscoSpark.init({
 *   config: {
 *     credentials: {
 *       authorizationString: `<AUTHORIZATION URL FROM DEVELOPER PORTAL>`
 *     }
 *   }
 * });
 *
 * @example
 * <caption>Create a new CiscoSpark instance configured for a Bot</caption>
 * const ciscospark = CiscoSpark.init({
 *   config: {
 *     credentials: `<BOT TOKEN FROM DEVELOPER PORTAL>`
 *   }
 * });
 *
 *
 * @param {Object} attrs
 * @param {Object} attrs.config (optional)
 * @memberof CiscoSpark
 * @returns {CiscoSpark}
 */
CiscoSpark.init = function init(attrs = {}) {
  attrs.config = merge({}, config, attrs.config);
  return new CiscoSpark(attrs);
};

module.exports = CiscoSpark;

remove misleading docs for CiscoSpark.init()