var Swagger = require('swagger-client');


/**
 * Creates a swagger client from the provided swagger specification.
 * Once the client is ready, it is stored on the window object.
 * The provided callback is called in case of success and failure.
 *
 * @param {string} swaggerJsonUrl  URL to the swagger json specification.
 * @param {Function} callback  The callback that should be called when ready.
 */
export default function initSwagger(swaggerJsonUrl, callback) {
  let jsonUrl = swaggerJsonUrl;
  // Internet Explorer requires this:
  if (swaggerJsonUrl.indexOf('http') !== 0) {
    const { protocol, host } = window.location;
    jsonUrl = `${protocol}//${host}${swaggerJsonUrl}`;
  }
  new Swagger({ url: jsonUrl, usePromise: true }).then(client => {
    window.__swaggerClient = client;
    callback();
  }).catch(reason => {
    /* eslint-disable no-console */
    console.log(reason);
    callback();
  });
}

var Swagger = require('swagger-client');


/**
 * Creates a swagger client from the provided swagger specification.
 * Once the client is ready, it is stored on the window object.
 * The provided callback is called in case of success and failure.
 *
 * @param {string} swaggerJsonUrl  URL to the swagger json specification.
 * @param {Function} callback  The callback that should be called when ready.
 */
export default function initSwagger(swaggerJsonUrl, callback) {
  let jsonUrl = swaggerJsonUrl;
  // Internet Explorer requires this:
  if (swaggerJsonUrl.indexOf('http') !== 0) {
    const { protocol, host } = window.location;
    jsonUrl = `${protocol}//${host}${swaggerJsonUrl}`;
  }
  new Swagger({ url: jsonUrl, usePromise: true, enableCookies: true }).then(client => {
    window.__swaggerClient = client;
    callback();
  }).catch(reason => {
    /* eslint-disable no-console */
    console.log(reason);
    callback();
  });
}

enable cookies for the swagger client