import Promise from 'bluebird'
import { omit, defaults } from 'lodash/object'
import { partialRight } from 'lodash/partialRight'
import * as defaultTransformers from './transformers'

const spaceEntities = [
  'contentTypes', 'entries', 'assets', 'locales', 'webhooks', 'roles'
]

/**
 * Run transformer methods on each item for each kind of entity, in case there
 * is a need to transform data when copying it to the destination space
 */
export default function (
  sourceSpace, destinationSpace, customTransformers, entities = spaceEntities
) {
  const transformers = defaults(customTransformers, defaultTransformers)
  const newSpace = omit(sourceSpace, ...entities)
  // Use bluebird collection methods to support async transforms.
  return Promise.reduce(entities, (newSpace, type) => {
    const transformer = transformers[type]
    const sourceEntities = sourceSpace[type]
    const destinationEntities = destinationSpace[type]
    const typeTransform = partialRight(applyTransformer, transformer,
      destinationEntities, destinationSpace)

    return Promise.map(sourceEntities, typeTransform).then((entities) => {
      newSpace[type] = entities
      return newSpace
    })
  }, newSpace)
}

function applyTransformer (entity, transformer, destinationEntities, destinationSpace) {
  return Promise.props({
    original: entity,
    transformed: transformer(entity, destinationEntities, destinationSpace)
  })
}

@@ -75,59 +75,8 @@
 ct'%0A
-import %7B partialRight %7D from 'lodash/partialRight'%0A
 impo
@@ -411,22 +411,16 @@
 on (%0A  s
-ourceS
 pace, de
@@ -557,16 +557,67 @@
 ormers)%0A
+  // TODO maybe we don't need promises here at all%0A
   const 
@@ -633,22 +633,16 @@
 = omit(s
-ourceS
 pace, ..
@@ -658,205 +658,93 @@
 )%0A  
-// Use bluebird collection methods to support async transforms.%0A  return Promise.reduce(entities, (newSpace, type) =%3E %7B%0A    const transformer = transformers%5Btype%5D%0A    const sourceEntities = sourceS
+return Promise.reduce(entities, (newSpace, type) =%3E %7B%0A    return Promise.map(%0A      s
 pace
@@ -753,109 +753,81 @@
 ype%5D
+,
 %0A    
-const destinationEntities = destinationSpace%5Btype%5D%0A    const typeTransform = partialRight(applyT
+  (entity) =%3E Promise.resolve(%7B%0A        original: entity,%0A        t
 rans
@@ -831,18 +831,18 @@
 ansforme
-r,
+d:
  transfo
@@ -849,35 +849,22 @@
 rmer
-,%0A      destinationE
+s%5Btype%5D(e
 ntit
-ies
+y
 , de
@@ -881,64 +881,37 @@
 pace
-)%0A%0A    return Promise.map(sourceEntities, typeTransform)
+%5Btype%5D)%0A      %7D)%0A    )%0A      
 .the
@@ -934,16 +934,18 @@
 %7B%0A      
+ 
+ 
 newSpace
@@ -964,24 +964,26 @@
 ities%0A      
+  
 return newSp
@@ -986,24 +986,26 @@
 ewSpace%0A    
+  
 %7D)%0A  %7D, newS
@@ -1012,228 +1012,8 @@
 pace
-)%0A%7D%0A%0Afunction applyTransformer (entity, transformer, destinationEntities, destinationSpace) %7B%0A  return Promise.props(%7B%0A    original: entity,%0A    transformed: transformer(entity, destinationEntities, destinationSpace)%0A  %7D
 )%0A%7D%0A

Revert Enable async entity transforms