 React = require 'react'
 EventEmitter2 = require 'eventemitter2'
 
-api = require '../api'
+api  = require '../api'
 
 ERROR_MAP = {
   invalid_enrollment_code: 'The enrollment code is invalid'


  register: (inviteCode) ->
    @errors = []
    api.channel.emit("course.#{@ecosystem_book_uuid}.send.registration", data: {
      book_cnx_id: @ecosystem_book_uuid, enrollment_code: inviteCode
    })



module.exports = Course


class Course

  constructor: (@user, attributes) ->
    @channel = new EventEmitter2
    @set(attributes)
    _.bindAll(@, '_onRegistered', '_onConfirmed')


  isPending: ->
    @status is "pending"

  description: ->
    if @isIncomplete() # still fetching
      ""
    else if @isPending() # we originated from a join request
      "#{@to.course.name} #{@to.period.name} period"
    else
      "#{@name} #{_.first(@periods).name} period"

  isIncomplete: -> not (@name or @to)

  set: (attributes) ->
    _.extend(@, attributes)
