import { LayoutManager } from 'react-viewerbase';
import { MODULE_TYPES } from 'ohif-core';
import { connect } from 'react-redux';

const mapStateToProps = state => {
  const viewportPluginIds = state.plugins.availablePlugins
    .filter(plugin => plugin.type === MODULE_TYPES.VIEWPORT)
    .map(plugin => plugin.id);

  const availablePlugins = {};
  viewportPluginIds.forEach(id => {
    const plugin = OHIF.plugins.availablePlugins.find(
      plugin => plugin.id === id
    );
    if (plugin) {
      availablePlugins[id] = plugin.component;
    }
  });

  // TODO Use something like state.plugins.defaultPlugin[MODULE_TYPES.VIEWPORT]
  let defaultPlugin;
  if (viewportPluginIds && viewportPluginIds.length) {
    defaultPlugin = viewportPluginIds[0];
  }

  return {
    layout: state.viewports.layout,
    activeViewportIndex: state.viewports.activeViewportIndex,
    availablePlugins,
    defaultPlugin,
  };
};

const ConnectedLayoutManager = connect(
  mapStateToProps,
  null
)(LayoutManager);

export default ConnectedLayoutManager;

@@ -128,428 +128,346 @@
 x';%0A
-%0Aconst mapStateToProps = state =%3E %7B%0A  const viewportPluginIds = state.plugins.availablePlugins%0A    .filter(plugin =%3E plugin.type === MODULE_TYPES.VIEWPORT)%0A    .map(plugin =%3E plugin.id);%0A%0A  const availablePlugins = %7B%7D;%0A  viewportPluginIds.forEach(id =%3E %7B%0A    const plugin = OHIF.plugins.availablePlugins.find(%0A      plugin =%3E plugin.id === id%0A    );%0A    if (plugin) %7B%0A      availablePlugins%5Bid%5D = plugin.component;%0A    %7D
+import %7B extensionManager %7D from './../App.js';%0A%0Aconst mapStateToProps = state =%3E %7B%0A  const availableViewportModules = %7B%7D;%0A  const viewportModules = extensionManager.modules%5BMODULE_TYPES.VIEWPORT%5D;%0A%0A  viewportModules.forEach(moduleDefinition =%3E %7B%0A    availableViewportModules%5BmoduleDefinition.extensionId%5D =%0A      moduleDefinition.module;
 %0A  %7D
@@ -479,16 +479,17 @@
  // TODO
+:
  Use som
@@ -590,37 +590,14 @@
 port
-PluginIds && viewportPluginId
+Module
 s.le
@@ -636,20 +636,30 @@
 port
-PluginIds%5B0%5D
+Modules%5B0%5D.extensionId
 ;%0A  
@@ -778,25 +778,142 @@
     
-availablePlugins,
+// TODO: rename %60availableViewportModules%60%0A    availablePlugins: availableViewportModules,%0A    // TODO: rename %60defaultViewportModule%60
 %0A   

fix connected layout manager to use extensionManager