// Copyright 2015, University of Colorado Boulder

/**
 * This (asynchronous) grunt task builds and minifies an individual wrapper.
 * Note this file was created to support building the phet-io-wrapper-sonification repo, and should be worked on before
 * using on other wrappers.
 * @author Michael Kauzmann (PhET Interactive Simulations)
 */

// modules
var ChipperConstants = require( '../../../chipper/js/common/ChipperConstants' );
var copyDirectory = require( '../../../chipper/js/grunt/phet-io/copyDirectory' );
var createDependenciesJSON = require( '../../../chipper/js/grunt/createDependenciesJSON' );

// build in node apis
var fs = require( 'fs' );
var assert = require( 'assert' );

// Don't copy these files/folders into the built wrapper
var WRAPPER_BLACKLIST = [ '.git', 'README.md', '.gitignore', 'node_modules', 'build' ];


/**
 * @param grunt - the grunt instance
 * @param {Object} buildConfig - see getBuildConfig.js
 */
module.exports = function( grunt, buildConfig ) {
  'use strict';

  // Tell grunt to wait because this task is asynchronous.
  // Returns a handle to a function that must be called when the task has completed.
  var done = grunt.task.current.async();

  buildConfig.phetLibs.forEach( function( repo ) {

    //  We only need the common folder from this repo
    if ( repo === 'phet-io-wrappers' ) {
      repo = 'phet-io-wrappers/common';
    }

    // Special case for the sherpa repo. Look for packageJSON.wrapper.sherpaDependencies for individual files to copy on build.
    if ( repo === 'sherpa' ) {

      var pathToLib = '/sherpa/lib/';
      fs.mkdirSync( ChipperConstants.BUILD_DIR + '/' + repo );
      fs.mkdirSync( ChipperConstants.BUILD_DIR + pathToLib );
      grunt.log.debug( 'created sherpa lib file: ' + ChipperConstants.BUILD_DIR + pathToLib );

      assert( buildConfig.wrapperSherpaDependencies,
        'Must declare individual files of sherpaDependencies in wrapper\'s package.json to have "sherpa" repo as a dependency' );
      buildConfig.wrapperSherpaDependencies.forEach( function( file ) {
        grunt.file.copy( '..' + pathToLib + file, ChipperConstants.BUILD_DIR + pathToLib + file );
      } );
    }

    // otherwise copy the whole directory over, except the black list from above
    else {
      copyDirectory( grunt, '../' + repo, ChipperConstants.BUILD_DIR + '/' + repo + '/', null, {
        blacklist: WRAPPER_BLACKLIST, // List of files to not copy

        // TODO: We want to minify the built wrapper, but currently it is causing an error,
        // see https://github.com/phetsims/phet-io-wrapper-sonification/issues/19#event-1153696188
        minifyJS: true,
        licenseToPrepend: '// Copyright 2002-2017, University of Colorado Boulder\n' +
                          '// This PhET-iO file requires a license\n' +
                          '// USE WITHOUT A LICENSE AGREEMENT IS STRICTLY PROHIBITED.\n' +
                          '// For licensing, please contact phethelp@colorado.edu\n\n'
      } );
    }
  } );


  // Since this is an asynchronous task, each step in the task uses a callback to advance to the next step.
  // The final step in the task calls 'done', to tell grunt that the task has completed.
  createDependenciesJSON( grunt, buildConfig, function( dependenciesJSON ) {
    done();
  } );
};

@@ -655,42 +655,8 @@
 ' );
-%0Avar assert = require( 'assert' );
 %0A%0A//
@@ -1503,24 +1503,412 @@
 herpa' ) %7B%0A%0A
+      // If there are no sherpaDependencies mentioned in the package.json, then still just copy the whole repo.%0A      if ( !buildConfig.wrapperSherpaDependencies ) %7B%0A        grunt.log.debug( 'To decrease the size of the sherpa dependency, use wrapper.sheraDependencies in package.json ' +%0A                         'to specify sherpa libraries from sherpa/lib.' );%0A      %7D%0A%0A      else %7B%0A  
       var pa
@@ -1933,16 +1933,18 @@
 /lib/';%0A
+  
       fs
@@ -2000,24 +2000,26 @@
 po );%0A      
+  
 fs.mkdirSync
@@ -2064,24 +2064,26 @@
 ib );%0A      
+ 
+ 
 grunt.log.de
@@ -2166,189 +2166,8 @@
 %0A%0A  
-    assert( buildConfig.wrapperSherpaDependencies,%0A        'Must declare individual files of sherpaDependencies in wrapper%5C's package.json to have %22sherpa%22 repo as a dependency' );%0A
     
@@ -2230,24 +2230,26 @@
 n( file ) %7B%0A
+  
         grun
@@ -2337,28 +2337,101 @@
 le );%0A      
+  
 %7D );
+%0A%0A        return; // short circuit before the whole copy below.%0A      %7D
 %0A    %7D%0A%0A    
@@ -2507,29 +2507,16 @@
 m above%0A
-    else %7B%0A  
     copy
@@ -2608,18 +2608,16 @@
 %7B%0A      
-  
 blacklis
@@ -2674,18 +2674,16 @@
 %0A%0A      
- 
- 
 // TODO:
@@ -2758,18 +2758,16 @@
  error,%0A
-  
       //
@@ -2861,18 +2861,16 @@
 8%0A      
-  
 minifyJS
@@ -2883,18 +2883,16 @@
 ,%0A      
- 
- 
 licenseT
@@ -2982,26 +2982,24 @@
             
-  
 '// This PhE
@@ -3028,26 +3028,24 @@
 icense%5Cn' +%0A
-  
             
@@ -3141,26 +3141,24 @@
             
-  
 '// For lice
@@ -3214,21 +3214,13 @@
     
-  
 %7D );%0A
-    %7D%0A
   %7D 
@@ -3480,26 +3480,8 @@
 ion(
- dependenciesJSON 
 ) %7B%0A

copy all of sherpa by default, https://github.com/phetsims/phet-io-wrapper-sonification/issues/26