import * as ActionTypes from './constants.js'
import { ActionTypes as EntityActionTypes } from '../entities'
import createReducer from '../createReducer.js'

const reducer = createReducer({
  tabs: [],
  activeTabKey: null,
  lastActiveTemplateKey: null,
  undockMode: false
})
export default reducer.export()

reducer.handleAction(ActionTypes.OPEN_TAB, (state, { tab }) => ({
  ...state,
  tabs: state.tabs.filter((t) => t.key === tab.key).length ? state.tabs : [...state.tabs, tab]
}))

reducer.handleAction(ActionTypes.OPEN_NEW_TAB, (state, { tab }) => ({
  ...state,
  activeTabKey: tab.key,
  tabs: [...state.tabs, tab],
  lastActiveTemplateKey: (tab.entitySet === 'templates') ? tab._id : state.lastActiveTemplateKey
}))

reducer.handleActions([EntityActionTypes.REMOVE, ActionTypes.CLOSE_TAB], (state, action) => {
  let newTabs = state.tabs.filter((t) => t.key !== action.key && (!action._id || t._id !== action._id))
  let newActivatTabKey = state.activeTabKey
  if (state.activeTabKey === action.key || state.activeTabKey === action._id) {
    newActivatTabKey = newTabs.length ? newTabs[newTabs.length - 1].key : null
  }

  const newActivatTab = newActivatTabKey ? newTabs.filter((t) => t.key === newActivatTabKey)[0] : null

  return {
    ...state,
    activeTabKey: newActivatTabKey,
    tabs: newTabs,
    lastActiveTemplateKey: (newActivatTab && newActivatTab.entitySet === 'templates') ? newActivatTab._id
      : (newTabs.filter((t) => t._id === state.lastActiveTemplateKey).length ? state.lastActiveTemplateKey : null)
  }
})

reducer.handleAction(ActionTypes.ACTIVATE_TAB, (state, action) => {
  const newTab = state.tabs.filter((t) => t.key === action.key)[0]

  return {
    ...state,
    activeTabKey: action.key,
    lastActiveTemplateKey: newTab.entitySet === 'templates' ? newTab._id : state.lastActiveTemplateKey
  }
})

reducer.handleAction(EntityActionTypes.SAVE_NEW, (state, action) => {
  let indexMap = {}
  let indexes = []
  let tabs = []

  // this code is necessary to support updating header/footer templates
  let modTabs = state.tabs.filter((t, idx) => {
    const keepItem = (t._id === action.oldId)

    if (keepItem) {
      indexes.push(idx)
      indexMap[idx] = indexes.length - 1
    }

    return keepItem
  }).map((tab) => {
    return Object.assign({}, tab, {
      _id: action.entity._id,
      key: tab.key.replace(action.oldId, action.entity._id)
    })
  })

  state.tabs.forEach(function (tab, idx) {
    if (indexes.indexOf(idx) !== -1) {
      return tabs.push(modTabs[indexMap[idx]])
    }

    tabs.push(tab)
  })

  return {
    ...state,
    tabs: tabs,
    activeTabKey: (state.lastActiveTemplateKey === action.oldId || state.activeTabKey === action.oldId) ? ((
      // looking if the last activeTabKey was a header/footer tab
      state.activeTabKey.indexOf(state.lastActiveTemplateKey) === 0 && state.activeTabKey !== state.lastActiveTemplateKey
    ) ? state.activeTabKey.replace(action.oldId, action.entity._id) : action.entity._id) : state.activeTabKey,
    lastActiveTemplateKey: state.lastActiveTemplateKey === action.oldId ? action.entity._id : state.lastActiveTemplateKey
  }
})

reducer.handleAction(EntityActionTypes.REPLACE, (state, action) => {
  if (!state.tabs.filter((t) => t._id === action.oldId).length) {
    return state
  }

  const tabs = state.tabs.map((t) => {
    if (t._id !== action.oldId) {
      return Object.assign({}, t)
    }

    return Object.assign({}, t, {
      _id: action.entity._id,
      key: t.key.replace(action.oldId, action.entity._id)
    })
  })

  return {
    ...state,
    tabs: tabs,
    activeTabKey: (state.activeTabKey && state.activeTabKey.indexOf(action.oldId) === 0) ? state.activeTabKey.replace(action.oldId, action.entity._id) : state.activeTabKey,
    lastActiveTemplateKey: state.lastActiveTemplateKey === action.oldId ? action.entity._id : state.lastActiveTemplateKey
  }
})

reducer.handleAction(ActionTypes.ACTIVATE_UNDOCK_MODE, (state, action) => {
  return {
    ...state,
    undockMode: true
  }
})

reducer.handleAction(ActionTypes.DESACTIVATE_UNDOCK_MODE, (state, action) => {
  return {
    ...state,
    undockMode: false
  }
})

@@ -851,24 +851,47 @@
 ilter((t) =%3E
+ %7B%0A    let shouldStay =
  t.key !== a
@@ -940,17 +940,191 @@
 ion._id)
-)
+%0A%0A    if (shouldStay && action.children) %7B%0A      shouldStay = action.children.every((childId) =%3E %7B%0A        return t._id !== childId%0A      %7D)%0A    %7D%0A%0A    return shouldStay%0A  %7D)%0A
 %0A  let n
@@ -1122,34 +1122,33 @@
 %0A%0A  let newActiv
-at
+e
 TabKey = state.a
@@ -1159,16 +1159,17 @@
 eTabKey%0A
+%0A
   if (st
@@ -1236,16 +1236,118 @@
 tion._id
+ %7C%7C (%0A    action.children &&%0A    action.children.some((childId) =%3E state.activeTabKey === childId)%0A  )
 ) %7B%0A    
@@ -1350,26 +1350,25 @@
     newActiv
-at
+e
 TabKey = new
@@ -1437,26 +1437,25 @@
 nst newActiv
-at
+e
 Tab = newAct
@@ -1452,26 +1452,25 @@
 b = newActiv
-at
+e
 TabKey ? new
@@ -1502,26 +1502,25 @@
 === newActiv
-at
+e
 TabKey)%5B0%5D :
@@ -1577,18 +1577,17 @@
 newActiv
-at
+e
 TabKey,%0A
@@ -1641,18 +1641,17 @@
 newActiv
-at
+e
 Tab && n
@@ -1653,26 +1653,25 @@
  && newActiv
-at
+e
 Tab.entitySe
@@ -1699,18 +1699,17 @@
 newActiv
-at
+e
 Tab._id%0A

fix issue with deleting hierarchy when some child entity is open in tab