module.exports = function() {
  var parseVariables,
      gonzales = require('gonzales-pe'),
      gonzo = require('gonzales-ast'),
      _ = require('lodash');

  function astToSrc(ast, syntax) {
    return gonzales.astToSrc({
      ast: ast,
      syntax: syntax
    });
  }

  // Parse SASS variables to object
  parseVariables = function(string, syntax) {

    syntax = syntax || 'scss'

    var out = {},
      ast = gonzales.srcToAST({
        src: string,
        syntax: syntax
      });

    gonzo.traverse(ast, [{
      test: function(name, nodes) {
        return name === 'declaration' && nodes[1][0] === 'variable';
      },
      process: function(nodes) {
        var varName = nodes[1][1][1].filter(function(element) {
            return element != 'ident'
          })[0],
          varVal = astToSrc(nodes[4], syntax);

        out[varName] = varVal;
      }
    }, {
      test: function(name, nodes) {
        return name === 'atrules';
      },
      process: function(nodes) {
        var varName = nodes[1][1][1],
          varVal = '';

        /* Grabs all the listed values
         * Fix then https://github.com/tonyganch/gonzales-pe/issues/17 is fixed */
        nodes.forEach(function(element) {
          if (
            element == 'atrules' ||
            element[0] == 'atkeyword'
          ) {
            return
          };
          if (
            element[0] == 'operator' &&
            element[1] == ':'
          ) {
            return;
          }
          varVal += astToSrc(element, 'less');
        });
        varVal = varVal.trim();
        out[varName] = varVal;
      }
    }]);

    return out;
  }

  // Modifies string so that variables passed in object are updated
  setVariables = function(string, variables) {
    var sorted = [], lines = [],
      ast, result, changeVariable;

    changeVariable = function(ast, key, value) {
      return gonzo.traverse(ast, [
        {
          test: function(name, nodes) {
            return name === 'declaration' && nodes[1][0] === 'variable' && nodes[1][1].indexOf(key) !== -1;
          },
          process: function(node) {
            node[4] = [
            'value',
             value
            ]
            return node;
          }
        }
      ]);
    }

    ast = gonzales.srcToAST({
      src: string,
      syntax: 'scss'
    });

    _.forEach(variables, function(value, key){
      ast = changeVariable(ast, key, value);
    });

    result = gonzales.astToSrc({
      ast: ast,
      syntax: 'scss'
    });
    return result;
  }

  return {
    parseVariables: parseVariables,
    setVariables: setVariables
  }
}

@@ -1746,16 +1746,24 @@
 (string,
+ syntax,
  variabl
@@ -1764,24 +1764,24 @@
 ariables) %7B%0A
-
     var sort
@@ -2235,32 +2235,1064 @@
 de;%0A          %7D%0A
+        %7D,%0A        %7B%0A          test: function(name, nodes) %7B%0A            if (name === 'atrules') %7B%0A              console.log(nodes);%0A            %7D%0A            return name === 'atrules';%0A          %7D,%0A          process: function(node) %7B%0A            var varName = node%5B1%5D%5B1%5D%5B1%5D;%0A%0A            /* Processed the variables declared as atrules%0A            * Fix then https://github.com/tonyganch/gonzales-pe/issues/17 is fixed */%0A            node.forEach(function(element) %7B%0A              if (%0A                element == 'atrules' %7C%7C%0A                element%5B0%5D == 'atkeyword'%0A              ) %7B%0A                return%0A              %7D;%0A              if (%0A                element%5B0%5D == 'operator' &&%0A                element%5B1%5D == ':'%0A              ) %7B%0A                return;%0A              %7D%0A              if (%0A                element%5B0%5D == 's'%0A              ) %7B%0A                return;%0A              %7D%0A              if (varName == key) %7B%0A                element = %5Bvalue%5D;%0A              %7D%0A            %7D);%0A            return node;%0A          %7D%0A
         %7D%0A      
@@ -3302,16 +3302,17 @@
 %0A    %7D%0A%0A
+%0A
     ast 
@@ -3358,38 +3358,38 @@
 ,%0A      syntax: 
-'scss'
+syntax
 %0A    %7D);%0A%0A    _.
@@ -3546,22 +3546,22 @@
 syntax: 
-'scss'
+syntax
 %0A    %7D);

Change variables in LESS syntax