define ["./dom", "./console", "./messages", "./ajax", "underscore", "./bootstrap"],
(dom, console, messages, ajax, _, {glyph}) ->
severityToClass =
info: "alert-info"
success: "alert-success"
warn: "alert-warning"
error: "alert-danger"
getURL = (container) -> container.attr "data-dismiss-url"
removeAlert = (container, alert) ->
alert.remove()
if container.find(".alert").length is 0
container.update null
dismissAll = (container) ->
alerts = container.find "[data-alert-id]"
if alerts.length is 0
container.update null
return
ajax (getURL container),
success: -> container.update null
dismissOne = (container, button) ->
alert = button.parent()
id = alert.attr "data-alert-id"
unless id
removeAlert container, alert
return
ajax (getURL container),
data: { id }
success: -> removeAlert container, alert
setupUI = (outer) ->
outer.update """
<div data-container-type="inner"></div>
"""
if (outer.attr "data-show-dismiss-all") is "true"
outer.append """
<div class="pull-right">
<button class="btn btn-xs btn-default" data-action="dismiss-all">
#{glyph "remove"}
#{messages "core-dismiss-label"}
</button>
</div>
"""
outer.on "click", "[data-action=dismiss-all]", ->
dismissAll outer
return false
outer.on "click", "button.close", ->
dismissOne outer, this
return false
findInnerContainer = ->
outer = dom.body.findFirst "[data-container-type=alerts]"
unless outer
console.error "Unable to locate alert container element to present an alert."
return null