  DestroyDraftTask,
  DatabaseView} = require 'nylas-exports'
 
-module.exports =
-DraftListStore = Reflux.createStore
-  init: ->
+class DraftListStore extends NylasStore
+  constructor: ->
     @listenTo DatabaseStore, @_onDataChanged
     @listenTo AccountStore, @_onAccountChanged
     @listenTo Actions.deleteSelection, @_onDeleteSelection


    @unlisten = @_view.listen => @trigger({})

  _onAccountChanged: ->
    @_createView()

  _onDataChanged: (change) ->
    return unless change.objectClass is Message.name
    containsDraft = _.some(change.objects, (msg) -> msg.draft)
    return unless containsDraft
    @_view.invalidate()

  _onDeleteSelection: ->
    selected = @_view.selection.items()

    for item in selected
      Actions.queueTask(new DestroyDraftTask(draftClientId: item.clientId))

    @_view.selection.clear()

    @listenTo Actions.sendDraftSuccess, => @_view.invalidate()
    @_createView()

  view: =>
    @_view

  _createView: =>
    account = AccountStore.current()

    if @unlisten
