 TinFoilCollection = require './tin-foil-collection'
 TinFoilMap = require './tin-foil-map'
 
-keywords = ['extend', 'mixin', 'set', 'get', 'prop', 'hasDefinition', 'add', 'compile', '_mapDefinitionAliases', '_mapDefinitionAlias',
-            '_mapNestedAliases', '_mapTinFoilCollectionAliases', '_mapTinFoilMapAliases']
+keywords = ['extend', 'mixin', 'set', 'get', 'prop', 'hasDefinition', 'add', 'compile', '_mapDefinitionAliases', '_mapNormalAlias',
+            '_mapNestedAliases', '_mapTinFoilCollectionAlias', '_mapTinFoilMapAlias']
 
 class TinFoil
 

    aliases = definition.aliases

    if type?.isTinFoil
      @_mapTinFoilAliases type, prefix

    else if type == TinFoilCollection
      @_mapTinFoilCollectionAliases definition, prefix

    else if type == TinFoilMap
      @_mapTinFoilMapAliases definition, prefix

    else
      @_mapDefinitionAlias name, "#{prefix}#{alias}" for alias in aliases

  @_mapDefinitionAlias: (name, alias) ->
    # TODO: Check alias name against keywords
    # TODO: Should we allow overriding aliases?
    @[alias] = (val) ->
      definition = @definition name
      throw Error "Definition [#{name}] not found" unless definition
      @define name,
        type: definition.type
        defaultValue: val
        aliases: definition.aliases.slice(0)
        false
      this

    this

  @_mapTinFoilAliases: (type, prefix = '') ->
    for own name, definition of type.definitions
      @_mapDefinitionAliases name, "#{prefix}#{name}"

    this

  @_mapTinFoilCollectionAliases: (definition, prefix = '') ->
    collection = definition.defaultValue

    for alias in definition.aliases
      @["#{prefix}#{alias}"] = (val) =>
        collection.add val
        this

    this

  @_mapTinFoilMapAliases: (definition, prefix = '') ->
    map = definition.defaultValue

    for alias in definition.aliases
      @["#{prefix}#{alias}"] = (key, value) =>
        map.add key, value
        this

    this


    # Special types
    if type.isTinFoil
      type = type.extend()

    if type is TinFoilCollection or type is TinFoilMap
      defaultValue = new type
