 hardcodedTriggers = require '../support/hardcoded_triggers'
 
 class TriggerRepository
-  LEGACY_KEY         = 'trigger'
   KEY                = 'triggers'
   HARDCODED_TRIGGERS = hardcodedTriggers
 

  constructor: (@brain) ->

  find: (trigger) ->
    @swapLegacy(trigger)
    @brainTrigger(trigger) || @hardcodedTrigger(trigger)

  save: (name, phrase, author = 'Liona') ->
    trigger = name: name, phrase: phrase, author: author
    triggers = @brainTriggers()
    triggers.push trigger

    @brain.set KEY, triggers
    trigger

  remove: (trigger) ->
    triggerObj = @find(trigger) # Cleans up any legacy triggers
    triggers = @brainTriggers()
    index = triggers.indexOf triggerObj
    triggers.splice(triggerObj, 1) unless index is -1
    @brain.set KEY, triggers

  all: ->
    all = (@hardcodedTrigger(trigger) for trigger, phrase of HARDCODED_TRIGGERS)

  brainTrigger: (trigger) ->
    @brainTriggers().filter((t) -> t.name is trigger)?[0]

  hardcodedTrigger: (trigger) ->
    phrase = HARDCODED_TRIGGERS[trigger]
    return unless phrase?
    name: trigger, phrase: phrase, author: 'Liona'

module.exports = TriggerRepository
