/*
 * grunt-css-amd
 * http://gruntjs.com/
 *
 * Copyright (c) 2012 Thiago Felix, contributors
 * Licensed under the MIT license.
 * https://github.com/thiagofelix/grunt-css-amd/blob/master/LICENSE-MIT
 */

module.exports = function(grunt) {
  "use strict";

  grunt.registerMultiTask("cssamd", "Compile all css files into javascript amd module files", function() {

    var helpers = require('grunt-lib-contrib').init(grunt);
    var options = helpers.options(this);

    grunt.verbose.writeflags(options, "Options");
    
    // TODO: ditch this when grunt v0.4 is released
    this.files = this.files || helpers.normalizeMultiTaskFiles(this.data, this.target);

    var srcFiles;
    var sourceCss;
    var sourceModuled;
    var fileName; 
    var outputFilename;
    var moduleName;
    
    this.files.forEach(function(files) {      

      srcFiles = grunt.file.expandFiles(files.src);

      srcFiles.forEach(function(srcFile) {        
        grunt.log.writeln('Processing "' + srcFile + ".");

        fileName = srcFile.substr(srcFile.lastIndexOf('/')+1);

        try{                      
          moduleName = options.moduleNamePrefix || files.dest + fileName;   
          sourceCss = grunt.file.read(srcFile);
          sourceModuled = modulyCSS(moduleName, sourceCss);          
        } catch(e) {
          grunt.log.error(e);
          grunt.fail.warn('Failed to compile css '+srcFile+'.'); 
        }

        try{          
          outputFilename = files.dest + fileName + '.js';
          grunt.file.write(outputFilename, sourceModuled);
          grunt.log.writeln('File "' + outputFilename + '" created.');
        } catch(e) {
          grunt.log.error(e);
          grunt.fail.warn('Failed to save transformed css '+outputFilename+'.'); 
        }
      });
    });    
  });
  
  var modulyCSS = function(moduleName, source) {
    try {
      
      var output  = "";

      output += "define('"+moduleName+"',[], function(){\n";
      output += "\tvar style = document.createElement('style');\n";
      output += "\tstyle.appendChild(\n";
      output += "\t\tdocument.createTextNode('"+source.replace(/[\r\n]/g,"")+"')\n";
      output += "\t);\n";
      output += "\treturn style;\n";
      output += "});\n";
      return output;
    } catch (e) {
      grunt.log.error(e);
      grunt.fail.warn('CSS modularization failed.');
    }
  };

};
/*
 * grunt-css-amd
 * http://gruntjs.com/
 *
 * Copyright (c) 2012 Thiago Felix, contributors
 * Licensed under the MIT license.
 * https://github.com/thiagofelix/grunt-css-amd/blob/master/LICENSE-MIT
 */

"use strict";

module.exports = function(grunt) {

  grunt.registerMultiTask("cssamd", "Compile all css files into javascript amd module files", function() {
    grunt.log.writeln('Yeah, we are rocking! ', this.files.length);

    if (this.files.length < 1) {
      grunt.verbose.warn('Destination not written because no source files were provided.');
    }

    this.files.forEach(function(f) {
      var src = f.src.filter(function(filepath) {
        // Warn on and remove invalid source files (if nonull was set).
        if (!grunt.file.exists(filepath)) {
          grunt.log.warn('Source file "' + filepath + '" not found.');
          return false;
        } else {
          return true;
        }
      });

      if (src.length === 0) {
        grunt.log.warn('Destination (' + f.dest + ') not written because src files were empty.');
        return;
      }

      var css = grunt.file.read(src);

      var output = CSS2Module(css);

      // Write the destination file.
      grunt.file.write(f.dest, output);            

      // Print a success message.
      grunt.log.writeln('File "' + f.dest + '" created.');

    });

  });

  var CSS2Module = function(source) {
    try {
      
      var output  = "";

      output += "define([], function(){\n";
      output += "\tvar style = document.createElement('style');\n";
      output += "\tstyle.appendChild(\n";
      output += "\t\tdocument.createTextNode('"+source.replace(/[\r\n]/g,"")+"')\n";
      output += "\t);\n";
      output += "\treturn style;\n";
      output += "});\n";
      return output;
    } catch (e) {
      grunt.log.error(e);
      grunt.fail.warn('CSS modularization failed.');
    }
  };
};

Update cssamd.js