import external from './externalModules.js';
import store from './store/index.js';
import registerModule from './store/registerModule.js';
import { addTool, addToolForElement } from './store/addTool.js';
import { removeTool, removeToolForElement } from './store/removeTool.js';
import getToolForElement from './store/getToolForElement.js';
import {
  setToolOptions,
  setToolOptionsForElement
} from './store/setToolOptions.js';
import addEnabledElement from './store/internals/addEnabledElement.js';
import removeEnabledElement from './store/internals/removeEnabledElement.js';
import {
  setToolActive,
  setToolActiveForElement,
  setToolEnabled,
  setToolEnabledForElement,
  setToolDisabled,
  setToolDisabledForElement,
  setToolPassive,
  setToolPassiveForElement
} from './store/setToolMode.js';
import windowResizeHandler from './eventListeners/windowResizeHandler.js';

export default function (configuration) {
  _addCornerstoneEventListeners();
  _initModules();
  windowResizeHandler.enable();

  // Apply global configuration
  store.modules.globalConfiguration.state = Object.assign(
    {},
    store.modules.globalConfiguration.state,
    configuration
  );

  const cornerstoneToolsInstance = Object.freeze({
    store,
    addTool,
    addToolForElement,
    getToolForElement,
    removeTool,
    removeToolForElement,
    setToolOptions,
    setToolOptionsForElement,
    // Tool Modes
    setToolActive,
    setToolActiveForElement,
    setToolEnabled,
    setToolEnabledForElement,
    setToolDisabled,
    setToolDisabledForElement,
    setToolPassive,
    setToolPassiveForElement
  });

  // By assigning this here it is only accessible by initialised instances,
  // This prevents users of the library from trying to register modules before
  // Initialising cornerstoneTools.
  cornerstoneToolsInstance.store.registerModule = registerModule;

  return cornerstoneToolsInstance;
}

function _addCornerstoneEventListeners () {
  const cornerstone = external.cornerstone;
  const elementEnabledEvent = cornerstone.EVENTS.ELEMENT_ENABLED;
  const elementDisabledEvent = cornerstone.EVENTS.ELEMENT_DISABLED;

  cornerstone.events.addEventListener(elementEnabledEvent, addEnabledElement);
  cornerstone.events.addEventListener(
    elementDisabledEvent,
    removeEnabledElement
  );
}

/**
 * _initModules - Initialise all modules that have an onRegisterCallback.
 * TODO: Makes sure 3rd party modules get
 * Registered before we try to init them.
 *
 * @private
 * @param  {Object} enabledElement  The element on which to
 *                                  Initialise the modules.
 */
function _initModules () {
  const modules = store.modules;

  Object.keys(modules).forEach(function(key) {
    if (typeof modules[key].onRegisterCallback === 'function') {
      modules[key].onRegisterCallback();
    }
  });
}

@@ -874,16 +874,299 @@
 r.js';%0A%0A
+/**%0A * Merges the provided configuration with default values and returns a%0A * configured CornerstoneTools instance.%0A * @public @export @function%0A * @name init%0A * @param %7BObject%7D configuration%0A * @returns %7BObject%7D A configured CornerstoneTools instance with top level API members%0A */%0A
 export d
@@ -2186,16 +2186,150 @@
 nce;%0A%7D%0A%0A
+/**%0A * Wires up event listeners for the Cornerstone#ElementDisabled and%0A * Cornerstone#ElementEnabled events.%0A * @private @method%0A */%0A
 function
@@ -2726,130 +2726,234 @@
 %0A%0A/*
-*
 %0A * 
-_initModules - Initialise all modules that have an onRegisterCallback.%0A * TODO: Makes sure 3rd party modules get
+TODO: This could cause issues if the module was already initialized for%0A * the store. As there's nothing stopping implementers from calling %60init%60%0A * multiple times. Modules should self-check if they have already been
 %0A * 
-R
+r
 egis
@@ -2962,170 +2962,222 @@
 red 
-before we try to init them.%0A *%0A * @private%0A * @param  %7BObject%7D enabledElement  The element on which to%0A *                                  Initialise the modules.
+to prevent issues.%0A */%0A%0A/**%0A * Iterate over our store's modules. If the module has an %60onRegisterCallback%60%0A * call it. This hook can be used to setup any global store requirements per%0A * module.%0A * @private @method
 %0A */
@@ -3277,16 +3277,17 @@
 function
+ 
 (key) %7B%0A

Add some basic docs to `init`