'use strict';

module.exports = function(grunt) {

    // Project configuration.
    grunt.initConfig({
        // Metadata.
        pkg: grunt.file.readJSON('mentions-kinder.jquery.json'),
        banner: '/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - ' +
            '<%= grunt.template.today("yyyy-mm-dd") %>\n' +
            '<%= pkg.homepage ? "* " + pkg.homepage + "\\n" : "" %>' +
            '* Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.company %>;' +
            ' Licensed <%= _.pluck(pkg.licenses, "type").join(", ") %> */\n',
        // Task configuration.
        clean: {
            files: ['dist', 'tmp', 'test/*.js']
        },
        coffee: {
            dev:{
                options: {
                    bare: true
                },
                expand: true,
                cwd: 'src',
                src: ['**/*.coffee'],
                dest: 'tmp/src',
                ext: '.js'
            },
            test:{
                files:{
                    'tmp/tests.js':'test/**/*.coffee'
                }
            }
        },
        concat: {
            options: {
                banner: '<%= banner %>' +
                    "(function($){\n",
                footer: "\n})(jQuery);",
                stripBanners: true
            },
            dev: {
                src: [
                    'tmp/src/autocompleter.js',
                    'tmp/src/mentions-kinder.js',
                    'tmp/src/jquery-plugin.js',
                    'src/extend-patch.js',
                    'tmp/src/autocompleter/dummy-autocompleter.js',
                    'tmp/src/autocompleter/select2-autocompleter.js'
//                    'tmp/src/autocompleter/typeahead-autocompleter.js'
                ],
                dest: 'tmp/<%= pkg.name %>.js',
                nonull: true
            },
            dist: {
                src: [
                    'tmp/src/autocompleter.js',
                    'tmp/src/mentions-kinder.js',
                    'tmp/src/jquery-plugin.js',
                    'src/extend-patch.js',
                    'tmp/src/autocompleter/dummy-autocompleter.js',
                    'tmp/src/autocompleter/select2-autocompleter.js'
//                    'tmp/src/autocompleter/typeahead-autocompleter.js'
                ],
                dest: 'dist/<%= pkg.name %>.js',
                nonull: true
            }
        },
        uglify: {
            options: {
                banner: '<%= banner %>'
            },
            dist: {
                src: '<%= concat.dist.dest %>',
                dest: 'dist/<%= pkg.name %>.min.js'
            }
        },
        qunit: {
            files: ['test/**/*-test.html']
        },
        watch: {
            all:{
                files:['src/**/*.coffee', 'test/**/*.coffee'],
                tasks:['test']
            }
        },
        connect: {
            server: {
                options: {
                    port: 1338
                }
            }
        }
    });

    // These plugins provide necessary tasks.
    grunt.loadNpmTasks('grunt-contrib-coffee');
    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-clean');
    grunt.loadNpmTasks('grunt-contrib-qunit');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-contrib-connect');

    grunt.registerTask('doc:dist', function(){
        if(grunt.file.exists('doc')){
            var source = grunt.config.get('uglify.dist.dest'),
                destination = "doc/"+source;
            grunt.file.copy(source, destination)
        }
        else {
            var repo = "git@github.com:mixxt/mentions-kinder.js.git",
                options = "--branch gh-pages --single-branch";
            grunt.fail.warn("doc folder not found, run this first:\n\n\t git clone "+ options +" "+ repo +" doc\n\n");
        }
    });

    // Default task.
    grunt.registerTask('precompile', ['coffee:dev', 'coffee:test', 'concat:dev']);
    grunt.registerTask('dist', ['coffee:dev', 'coffee:test', 'concat:dist', 'uglify:dist', 'doc:dist']);
    grunt.registerTask('test', ['precompile', 'qunit']);
    grunt.registerTask('server', ['precompile', 'connect:server', 'watch']);
    grunt.registerTask('default', 'test');

};

@@ -2392,32 +2392,665 @@
    nonull: true%0A
+            %7D,%0A            dist_rangy: %7B%0A                src: %5B%0A                    'tmp/src/autocompleter.js',%0A                    'tmp/src/mentions-kinder.js',%0A                    'tmp/src/jquery-plugin.js',%0A                    'src/extend-patch.js',%0A                    'tmp/src/autocompleter/dummy-autocompleter.js',%0A                    'tmp/src/autocompleter/select2-autocompleter.js',%0A                    'libs/rangy-1.2.3/uncompressed/rangy-core.js'%0A//                    'tmp/src/autocompleter/typeahead-autocompleter.js'%0A                %5D,%0A                dest: 'dist/%3C%25= pkg.name %25%3E.rangy.js',%0A                nonull: true%0A
             %7D%0A  
@@ -3270,24 +3270,177 @@
  %25%3E.min.js'%0A
+            %7D,%0A            dist_rangy: %7B%0A                src: '%3C%25= concat.dist_rangy.dest %25%3E',%0A                dest: 'dist/%3C%25= pkg.name %25%3E.rangy.min.js'%0A
             
@@ -4878,24 +4878,33 @@
 sk('dist', %5B
+%0A        
 'coffee:dev'
@@ -4911,32 +4911,40 @@
 , 'coffee:test',
+%0A       
  'concat:dist', 
@@ -4948,21 +4948,79 @@
 ', '
-uglify:dist',
+concat:dist_rangy',%0A        'uglify:dist', 'uglify:dist_rangy',%0A       
  'do
@@ -5026,16 +5026,21 @@
 oc:dist'
+%0A    
 %5D);%0A    

build version with rangy included