 
   return { targetUser, targetIndex }
 
+
+###*
+ * Returns an object with the necessary google drive maps and lists are set.
+ *
+ * TODO: `initialSnapshot` may be irrelevant here.
+ *
+ * @param {RealtimeManager} manager
+ * @param {string} channelId
+ * @param {object} initialSnapshot
+###
 getReferences = (manager, channelId, initialSnapshot) ->
+
   nickname          = getNick()
   watchMapName      = "#{nickname}WatchMap"
   snapshotName      = "#{nickname}Snapshot"

      newData.sessionId = collaborator.sessionId
      participants.remove index
      participants.insert index, newData

fetchParticipants = (channelId, callback) ->
  {socialapi} = kd.singletons
  socialapi.channel.listParticipants {channelId}, (err, participants) ->
    return callback err  if err
    idList = participants.map ({accountId}) -> accountId
    query  = socialApiId: $in: idList

    remote.api.JAccount.some query, {}
      .then (accounts) -> callback null, accounts

removeParticipantFromParticipantList = (references, nickname) ->
  { participants } = references
  return console.warn 'participants is not set'  unless participants

  for participant, index in participants.asArray()
    if participant.nickname is nickname
      participants.remove index
      break

removeParticipantFromMaps = (manager, nickname) ->
  myWatchMapName = "#{nickname}WatchMap"
  mySnapshotName = "#{nickname}Snapshot"

  manager.delete 'map', myWatchMapName
  manager.delete 'map', mySnapshotName

removeParticipantFromPermissions = (references, nickname) ->
  references.permissions.delete nickname

removeFromManager = (manager, references, nickname) ->
  removeParticipantFromParticipantList references, nickname
  removeParticipantFromMaps manager, nickname
  removeParticipantFromPermissions references, nickname

module.exports = {
  loadCollaborationFile

getNick = require 'app/util/nick'
_ = require 'lodash'

###*
 * Load file with given id into RealtimeManager instance.
 *
 * @param {RealtimeManager} manager
 * @param {string} id - file id to load
 * @param {function(err: object, result: object)} callback - to be called with loaded document.
###
loadCollaborationFile = (manager, id, callback) ->

  options = { id, preventEvent: yes }

  manager.getFile id, (err, doc) ->
    return callback err  if err

    manager.setRealtimeDoc doc
    manager.isReady = yes
    callback null, doc


###*
 * Creates a file in RealtimeManager instance with given title.
 *
 * @param {RealtimeManager} manager
 * @param {string} title
 * @param {function(err: object, result: object)} callback
###
createCollaborationFile = (manager, title, callback) ->

  options = { title, preventEvent: yes }

  manager.createFile title, (err, file) ->
    return callback err  if err

    callback null, file


###*
 * Deletes a file in RealtimeManager instance with given title.
 *
 * @param {RealtimeManager} manager
 * @param {string} title
 * @param {function(err: object)} callback
###
deleteCollaborationFile = (manager, title, callback) ->

  options = { title, preventEvent: yes }

  manager.deleteFile options, (err) ->
    if err
    then callback err
    else callback null


###*
 * Checks if the given RealtimeManager instance has an active session.
 *
 * @param {RealtimeManager} manager
 * @param {string} title
 * @param {function(isActive: boolean)} callback
###
isSessionActive = (manager, title, callback) ->

  options = { title, preventEvent: yes }

  manager.fetchFileByTitle options, (err, file) ->
    return callback no  if err

    if file.result.items.length > 0
    then callback yes, file.result.items[0]
    else callback no


###*
 * Returns the identifier of a file with given SocialChannel id.
 *
 * TODO: this method needs to return different results
 *       depending on the status of a user
 *       if he/she is a host or not.
 *
 * @param {string} id
 * @return {string} fileId
###
getFileIdentifier = (id) -> "#{getNick()}.#{id}"


###*
 * Returns a user, index tuple for given predicate value
 * from participants.
 *
 * Example:
 *     sessionId = 'xyz'
 *     {targetUser} = getTargetUser rtm, 'sessionId', sessionId
 *
 * @param {(object|Array)} participants
 * @param {string} field
 * @param {string} predicateValue
###
getTargetUser = (participants, field, predicateValue) ->

  targetIndex  = null
  targetUser   = null
  participants = participants.asArray()  if participants.asArray?

  for p, index in participants when p?[field] is predicateValue
    targetIndex = index
    targetUser  = p.nickname
