# Author: VTEX

request = require 'request'

class ResolveTags

  constructor: (options) ->
    @options = options

  ########################
  # Handler              #
  ########################

  handler: (req, res, next) =>
    @options.logger?.debug('ResolveTags middleware')
    return next() if @options.utils.isAdmin(req, @options) is false
    return next() if req.tags?
    return next(new Error('req.tags or options.grunt are required')) if not @options.grunt? and not req.tags?

    if @options.grunt?
      return next(new Error('no grunt.config("tags") found')) if not @options.grunt.config("tags")?
      req.tags = @options.grunt.config("tags")
      return next()

module.exports = ResolveTags
