   addParticipantsByNames
   loadChannelByName
   loadChannelById
+  loadChannelByParticipants
   loadChannel
   loadFollowedPrivateChannels
   loadFollowedPublicChannels

_ = require 'lodash'
kd                      = require 'kd'
whoami                  = require 'app/util/whoami'
actionTypes             = require './actiontypes'

    dispatch LOAD_CHANNEL_SUCCESS, { channelId: channel.id, channel }


loadChannelByParticipants = (participants, options = {}) ->

  { LOAD_CHANNEL_BY_PARTICIPANTS_BEGIN
    LOAD_CHANNEL_BY_PARTICIPANTS_FAIL
    LOAD_CHANNEL_SUCCESS } = actionTypes

  new Promise (resolve, reject) ->

    dispatch LOAD_CHANNEL_BY_PARTICIPANTS_BEGIN, { participants }

    _options = _.assign {}, options, { participants }

    kd.singletons.socialapi.channel.byParticipants _options, (err, channels) ->
      if err
        dispatch LOAD_CHANNEL_BY_PARTICIPANTS_FAIL, { participants }
        reject err
        return

      kd.singletons.reactor.batch ->
        channels.forEach (channel) ->
          dispatch LOAD_CHANNEL_SUCCESS, { channelId: channel.id, channel }

        resolve { channels }


###*
 * Helper function to convert 'private' and 'public' types to internally
 * understandable type names.
