<commit_before>_                        = require 'lodash'
ChannelParticipantsModel = require './channelparticipants'
videoConstants           = require 'app/videocollaboration/constants'

module.exports = class CollaborationChannelParticipantsModel extends ChannelParticipantsModel

  defaultState:
    videoActive         : no
    selectedParticipant : null
    talkingParticipants : []
    videoParticipants   : []

  constructor: (options = {}, data) ->

    super options, data

    @state = _.assign {}, @defaultState, options.state


  ###*
   * Computes preview participants depending on the video active state.
   *
   *     - Delegate to super class when video is not active.
   *     - Return video-active participants when video is active.
   *
   * @param {Immutable.OrderedMap} participants - all participants
   * @return {Immutable.List} computedPreviewParticipants
  ###
  computePreviewParticipants: (participants) ->

    if @state.videoActive
      filterList = getOrderedVideoParticipants @state
      actives = participants.filter (participant) ->
        participant.profile.nickname in filterList

      return actives.toList()

    super


  ###*
   * Computes hidden participants depending on the video active state.
   *
   *     - Delegate to super class when video is not active.
   *     - Return non-video-active participants when video is active.
   *
   * @param {Immutable.OrderedMap} participants - all participants
   * @return {Immutable.List} computedPreviewParticipants
  ###
  computeHiddenParticipants: (participants) ->

    if @state.videoActive
      filterList = getOrderedVideoParticipants @state
      inactives = participants.filter (participant) ->
        not (participant.profile.nickname in filterList)

      return inactives.toList()

    super


  ###*
   * Defensively adds a video participant. Emits change afterwards if wanted.
   *
   * @param {string} nickname
   * @param {boolean=} emitEvent
  ###
  addVideoParticipant: (nickname, emitEvent = yes) ->

    pushToCollection @state.videoParticipants, nickname, =>
      @emitChange()  if emitEvent


  ###*
   * Defensively removes a video participant. Emits change afterwards if wanted.
   *
   * @param {string} nickname
   * @param {boolean=} emitEvent
  ###
  removeVideoParticipant: (nickname, emitEvent = yes) ->

    removeFromCollection @state.videoParticipants, nickname, =>
      @emitChange()  if emitEvent


  ###*
   * Sets given username as selected participant. It's a noop if user is not
   * online. Emits change afterwards.
   *
   * @param {string} nickname
   * @param {boolean} isOnline
  ###
  setVideoSelectedParticipant: (nickname) ->

    @state.selectedParticipant = nickname

    @emitChange()


  ###*
   * Defensively add talking participant.
   *
   * @param {string} nickname
  ###
  addTalkingParticipant: (nickname) ->

    pushToCollection @state.talkingParticipants, nickname, =>
      @emitChange()


  ###*
   * Defensively remove talking participant.
   *
   * @param {string} nickname
  ###
  removeTalkingParticipant: (nickname) ->

    removeFromCollection @state.talkingParticipants, nickname, =>
      @emitChange()


  ###*
   * Defensively add active video participant.
   *
   * @param {string} nickname
  ###
  addVideoActiveParticipant: (nickname) ->

    pushToCollection @state.videoParticipants, nickname, =>
      @emitChange()


  ###*
   * Defensively add active video participant.
   *
   * @param {string} nickname
  ###
  removeVideoActiveParticipant: (nickname) ->

    removeFromCollection @state.videoParticipants, nickname, =>
      @emitChange()


  ###*
   * Defensively add connected video participant.
   *
   * @param {string} nickname
  ###
  addVideoConnectedParticipant: (nickname) ->

    pushToCollection @state.connectedParticipants, nickname, =>
      @emitChange()


  ###*
   * Defensively add connected video participant.
   *
   * @param {string} nickname
  ###
  removeVideoConnectedParticipant: (nickname) ->

    removeFromCollection @state.connectedParticipants, nickname, =>
      @emitChange()


  ###*
   * Difference between this and super method, is that this one emits the state
   * with lists as well. This may be better to be moved to super class.
  ###
  emitChange: -> @emit 'change', @getLists(), @state


  ###*
   * Sets video state to given state.
   *
   *     - if resulting state is `active` it will use second parameter as
   *       initial participant list.
   *
   * @param {boolean} state
   * @param {array.<string>} participants
  ###<commit_msg>participants/collab: use bounded version for handlers<commit_after>_                        = require 'lodash'
ChannelParticipantsModel = require './channelparticipants'
videoConstants           = require 'app/videocollaboration/constants'

module.exports = class CollaborationChannelParticipantsModel extends ChannelParticipantsModel

  defaultState:
    videoActive         : no
    selectedParticipant : null
    talkingParticipants : []
    videoParticipants   : []

  constructor: (options = {}, data) ->

    super options, data

    @state = _.assign {}, @defaultState, options.state


  ###*
   * Computes preview participants depending on the video active state.
   *
   *     - Delegate to super class when video is not active.
   *     - Return video-active participants when video is active.
   *
   * @param {Immutable.OrderedMap} participants - all participants
   * @return {Immutable.List} computedPreviewParticipants
  ###
  computePreviewParticipants: (participants) ->

    if @state.videoActive
      filterList = getOrderedVideoParticipants @state
      actives = participants.filter (participant) ->
        participant.profile.nickname in filterList

      return actives.toList()

    super


  ###*
   * Computes hidden participants depending on the video active state.
   *
   *     - Delegate to super class when video is not active.
   *     - Return non-video-active participants when video is active.
   *
   * @param {Immutable.OrderedMap} participants - all participants
   * @return {Immutable.List} computedPreviewParticipants
  ###
  computeHiddenParticipants: (participants) ->

    if @state.videoActive
      filterList = getOrderedVideoParticipants @state
      inactives = participants.filter (participant) ->
        not (participant.profile.nickname in filterList)

      return inactives.toList()

    super


  ###*
   * Defensively adds a video participant. Emits change afterwards if wanted.
   *
   * @param {string} nickname
   * @param {boolean=} emitEvent
  ###
  addVideoParticipant: (nickname, emitEvent = yes) ->

    pushToCollection @state.videoParticipants, nickname, =>
      @emitChange()  if emitEvent


  ###*
   * Defensively removes a video participant. Emits change afterwards if wanted.
   *
   * @param {string} nickname
   * @param {boolean=} emitEvent
  ###
  removeVideoParticipant: (nickname, emitEvent = yes) ->

    removeFromCollection @state.videoParticipants, nickname, =>
      @emitChange()  if emitEvent


  ###*
   * Sets given username as selected participant. It's a noop if user is not
   * online. Emits change afterwards.
   *
   * @param {string} nickname
   * @param {boolean} isOnline
  ###
  setVideoSelectedParticipant: (nickname) ->

    @state.selectedParticipant = nickname

    @emitChange()


  ###*
   * Defensively add talking participant.
   *
   * @param {string} nickname
  ###
  addTalkingParticipant: (nickname) ->

    pushToCollection @state.talkingParticipants, nickname, @bound 'emitChange'


  ###*
   * Defensively remove talking participant.
   *
   * @param {string} nickname
  ###
  removeTalkingParticipant: (nickname) ->

    removeFromCollection @state.talkingParticipants, nickname, @bound 'emitChange'


  ###*
   * Defensively add active video participant.
   *
   * @param {string} nickname
  ###
  addVideoActiveParticipant: (nickname) ->

    pushToCollection @state.videoParticipants, nickname, @bound 'emitChange'


  ###*
   * Defensively add active video participant.
   *
   * @param {string} nickname
  ###
  removeVideoActiveParticipant: (nickname) ->

    removeFromCollection @state.videoParticipants, nickname, @bound 'emitChange'


  ###*
   * Defensively add connected video participant.
   *
   * @param {string} nickname
  ###
  addVideoConnectedParticipant: (nickname) ->

    pushToCollection @state.connectedParticipants, nickname, @bound 'emitChange'


  ###*
   * Defensively add connected video participant.
   *
   * @param {string} nickname
  ###
  removeVideoConnectedParticipant: (nickname) ->

    removeFromCollection @state.connectedParticipants, nickname, @bound 'emitChange'


  ###*
   * Difference between this and super method, is that this one emits the state
   * with lists as well. This may be better to be moved to super class.
  ###
  emitChange: -> @emit 'change', @getLists(), @state


  ###*
   * Sets video state to given state.
   *
   *     - if resulting state is `active` it will use second parameter as
   *       initial participant list.
   *
   * @param {boolean} state
   * @param {array.<string>} participants
  ###
{'fraction_non_alphanumeric': 0.10209881941407958, 'fraction_numerical': 0.0, 'mean_word_length': 3.2180728446288613, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3956328', 'n_tokens_mistral': 2711, 'n_tokens_neox': 2436, 'n_words': 711}