__ = require('highland')
highlandDuplex = require 'highland-duplex'

makeTwoConnectedDuplexes = (firstDuplexDelay = 0, secondDuplexDelay = 0) ->
  firstSrc = __()
  firstDest = __()
  secondSrc = __()
  secondDest = __()

  firstDest.each((v)->
    setTimeout(()->
      secondSrc.write(v)
    ,
      firstDuplexDelay
    )
  )
  secondDest.each((v)->
    setTimeout(()->
      firstSrc.write(v)
    ,
      secondDuplexDelay
    )
  )

  firstDuplex = highlandDuplex.makeDuplexStream(
    ()->
      firstSrc
  , ()->
    firstDest
  )

  secondDuplex = highlandDuplex.makeDuplexStream(
    ()->
      secondSrc
  , ()->
    secondDest
  )

  first: firstDuplex, second: secondDuplex

module.exports =
  makeTwoConnectedDuplexes:makeTwoConnectedDuplexes