/**
 * Build meta reducers for store configuration
 */
/**
 * decorate action object
 * with the store lookup and a predefined actionResult object for reliable result handling in effects
 */
interface MoveData {
  item: {}
  from: string
  to: string
}
interface ActionResult {
  moved: MoveData | {}
  added: any[]
  created: any[]
  updated: {
    item?: any
    itemBefore?: any
    items?: {
      [key: string]: any // Current state of items, stored by their IDs
    }
    itemsBefore?: {
      [key: string]: any // Previous state of items, stored by their IDs
    }
  }
  deleted: {
    soft?: {
      [key: string]: any
    }
    hard?: {
      [key: string]: any
    }
  }
  relationships: {
    created: any[]
    deleted: any[]
    resolved: any[]
  }
}
const decorateAction = (reducer) => (state, action) => {
  action.storeLookup = {
    ...state,
  }
  const actionResult: ActionResult = {
    moved: [],
    added: [],
    created: [],
    updated: { itemsBefore: {}, items: {} },
    deleted: { soft: {}, hard: {} },
    relationships: {
      created: [],
      deleted: [],
      resolved: [],
    },
  }

  action.actionResult = actionResult

  return reducer(state, action)
}
const metaReducers = [decorateAction]

export default metaReducers
