class Router
	@routes: {base: ''}
	@base: ''
	@webFolder: ''
	@devPrefix: ''

	@setBase: (base) ->
		isAbsolute = /(http|https):\/\//.test(base)
		throw new Error('The base url must be absolute.') unless isAbsolute
		@setWebFolder base.replace /\/app_dev.php/, ''  unless @webFolder

		isDev = (base.match(/\/app_dev.php/) or []).length isnt 0
		@devPrefix = if isDev then '/app_dev.php' else ''

		@base = base.replace /\/$/, ''

	@setWebFolder: (@webFolder) ->

	@load: (config) ->
		for name, route of config
			@routes[name] = route

	@url: (name, params) -> 
		@webFolder.replace(/\/$/, '') + @path arguments...

	@path: (name, params) ->
		throw new Error("Route #{name} not in configuration.") if typeof @routes[name] is 'undefined'
		route = @routes[name]

		if params
			for name, value of params
				route = route.replace("{#{name}}", value)

		return @devPrefix + route

	@asset: (path) ->
		@webFolder + path

	@redirect: ->
		if arguments.length is 1 and /(http|https):\/\//.test arguments[0]
			return window.location.href = arguments[0]
		@redirect @url arguments...

module.exports = Router