import { combineReducers } from 'redux'
import _ from 'lodash'
import * as actionTypes from './action_types'
import card from './card/reducer'
import collection from './collection/reducer'
import container from './container/reducer'
import filefield from './controls/filefield/reducer'
import form from './form/reducer'
import tabs from './tabs/reducer'
import tablefield from './controls/tablefield/reducer'
import tasks from './tasks/reducer'

const reducers = {
  card,
  collection,
  container,
  filefield,
  form,
  tabs,
  tablefield,
  tasks
}

export default (state, action) => {

  const namespace = action.type.split("/")[0]

  switch (action.type) {

    case actionTypes.ADD_COMPONENT:
      return  {
        ...state,
        [action.cid]: reducers[action.namespace](undefined, action)
      }

    case actionTypes.REMOVE_COMPONENT:
      return  _.omit(state, action.cid)

    default:
      if(state === undefined) {
        return {}
      } else if(reducers[namespace]) {
        if(action.cid) {
          return {
            ...state,
            [action.cid]: reducers[namespace](state[action.cid], action)
          }
        } else {
          return reducers[namespace](state, action)
        }
      } else {
        return state
      }

  }
}

@@ -121,16 +121,17 @@
  from '.
+.
 /card/re
@@ -162,16 +162,17 @@
  from '.
+.
 /collect
@@ -204,24 +204,25 @@
 iner from '.
+.
 /container/r
@@ -245,32 +245,33 @@
 ilefield from '.
+.
 /controls/filefi
@@ -302,16 +302,17 @@
  from '.
+.
 /form/re
@@ -337,16 +337,17 @@
  from '.
+.
 /tabs/re
@@ -378,16 +378,17 @@
  from '.
+.
 /control
@@ -429,16 +429,17 @@
  from '.
+.
 /tasks/r

fix reducer imports