 async          = require 'async'
 { Module }     = require 'jraphical'
 { difference } = require 'underscore'
+_ = require 'lodash'
 
 
 module.exports = class JGroup extends Module


    memberData = {}
    @fetchRolesByAccount member, (err, roles = []) ->
      memberData.roles = roles  unless err
      member.fetchEmail (err, email) ->
        memberData.email = email  unless err
        memberData.username = member.data.profile.nickname
        callback { memberData }


  @create = (client, groupData, owner, callback) ->


    @on 'MemberAdded', (member) ->
      @constructor.emit 'MemberAdded', { group: this, member }

      return  if @slug is 'guests'

      @prepareNewlyAddedMember member, ({err, memberData }) =>

        return @sendNotification 'GroupJoined', {}  if err

        member = _.extend {}, ObjectRef(member).data
        member = _.extend member, memberData

        @sendNotification 'GroupJoined',
          actionType : 'groupJoined'
          actorType  : 'member'
          subject    : ObjectRef(this).data
          member     : member

    @on 'MemberRemoved', (member, requester) ->

      requester ?= member
      @constructor.emit 'MemberRemoved', { group: this, member, requester }

      return  if @slug is 'guests'

      username = member.data.profile.nickname
      member = _.extend {}, ObjectRef(member).data
      member = _.extend member, { username }
      @sendNotification 'GroupLeft',
        actionType : 'groupLeft'
        actorType  : 'member'
        subject    : ObjectRef(this).data
        member     : member

  @render        :
    loggedIn     :
