 Promise = require 'bluebird'
 slugify = require 'underscore.string/slugify'
 {SphereClient, ProductSync} = require 'sphere-node-sdk'
+{Repeater} = require 'sphere-node-utils'
 ProductImport = require './product-import'
 
 class PriceImport extends ProductImport

    super @logger, options
    @skuOfPerformedPrices = []
    @sync.config [{type: 'prices', group: 'white'}].concat(['base', 'references', 'attributes', 'images', 'variants', 'metaAttributes'].map (type) -> {type, group: 'black'})

  _processBatches: (prices) ->
    batchedList = _.batchList(prices, 30) # max parallel elements to process

              when 200 then @_summary.updated++
          Promise.resolve(@_summary)
    ,{concurrency: 1}

  _createOrUpdate: (productsToProcess, existingProducts) ->
    debug 'Products to process: %j', {toProcess: productsToProcess, existing: existingProducts}

    posts = _.map productsToProcess, (prodToProcess) =>
      existingProduct = @_isExistingEntry(prodToProcess, existingProducts)
      if existingProduct?
        synced = @sync.buildActions(prodToProcess, existingProduct)
        if synced.shouldUpdate()
          updateTask = (payload) =>
            @client.products.byId(synced.getUpdateId()).update(payload)
          @repeater.execute ->
            updateTask(synced.getUpdatePayload())
          , (e) =>
            if e.statusCode is 409
              debug 'retrying to update %s because of 409', synced.getUpdateId()
              newTask = =>
                @client.productProjections.staged(true)
                .byId(synced.getUpdateId())
                .fetch()
                .then (result) ->
                  newPayload = _.extend {}, synced.getUpdatePayload(), {version: result.body.version}
                  updateTask(newPayload)
              Promise.resolve(newTask)
            else Promise.reject(e) # do not retry in this case
        else
          Promise.resolve statusCode: 304
      else
        @_summary.unknownSKUCount++
        Promise.resolve statusCode: 404

    debug 'About to send %s requests', _.size(posts)
    Promise.all(posts)

  _extractUniqueSkus: (prices) ->
    _.map prices, (p) ->
