var path = require('path');

var ASSET_FOLDER = require('../../constants/pluginAssetsFolder');
var Promise = require('../../utils/promise');
var fs = require('../../utils/fs');

/**
    Copy all assets from plugins.
    Assets are files stored in "_assets"
    nd resources declared in the plugin itself.

    @param {Output}
    @return {Promise}
*/
function copyPluginAssets(output) {
    var book = output.getBook();

    // Don't copy plugins assets for language book
    // It'll be resolved to the parent folder
    if (book.isLanguageBook()) {
        return Promise(output);
    }

    var plugins = output.getPlugins()

        // We reverse the order of plugins to copy
        // so that first plugins can replace assets from other plugins.
        .reverse();

    return Promise.forEach(plugins, function(plugin) {
        return copyAssets(output, plugin)
        .then(function() {
            return copyResources(output, plugin);
        });
    })
    .thenResolve(output);
}

/**
    Copy assets from a plugin

    @param {Plugin}
    @return {Promise}
*/
function copyAssets(output, plugin) {
    var logger = output.getLogger();
    var pluginRoot = plugin.getPath();
    var options = output.getOptions();

    var outputRoot = options.get('root');
    var assetOutputFolder = path.join(outputRoot, 'gitbook');
    var prefix = options.get('prefix');

    var assetFolder = path.join(pluginRoot, ASSET_FOLDER, prefix);

    if (!fs.existsSync(assetFolder)) {
        return Promise();
    }

    logger.debug.ln('copy assets from theme', assetFolder);
    return fs.copyDir(
        assetFolder,
        assetOutputFolder,
        {
            deleteFirst: false,
            overwrite: true,
            confirm: true
        }
    );
}

/**
    Copy resources from a plugin

    @param {Plugin}
    @return {Promise}
*/
function copyResources(output, plugin) {
    var logger = output.getLogger();

    var options = output.getOptions();
    var prefix = options.get('prefix');
    var outputRoot = options.get('root');

    var pluginRoot = plugin.getPath();
    var resources = plugin.getResources(prefix);

    var assetsFolder = resources.get('assets');
    var assetOutputFolder = path.join(outputRoot, 'gitbook', plugin.getNpmID());

    if (!assetsFolder) {
        return Promise();
    }

    // Resolve assets folder
    assetsFolder = path.resolve(pluginRoot, assetsFolder);
    if (!fs.existsSync(assetsFolder)) {
        logger.warn.ln('assets folder for plugin "' + plugin.getName() + '" doesn\'t exist');
        return Promise();
    }

    logger.debug.ln('copy resources from plugin', assetsFolder);

    return fs.copyDir(
        assetsFolder,
        assetOutputFolder,
        {
            deleteFirst: false,
            overwrite: true,
            confirm: true
        }
    );
}

module.exports = copyPluginAssets;

@@ -1925,32 +1925,35 @@
     var options 
+   
 = output.getOpti
@@ -1959,38 +1959,42 @@
 ions();%0A    var 
-prefix
+outputRoot
  = options.get('
@@ -1997,26 +1997,25 @@
 et('
-prefix
+root
 ');%0A
+%0A
     var 
 outp
@@ -2014,39 +2014,74 @@
 var 
-outputRoot = options.get('root'
+state = output.getState();%0A    var resources = state.getResources(
 );%0A%0A
@@ -2091,32 +2091,37 @@
  var pluginRoot 
+     
 = plugin.getPath
@@ -2124,33 +2124,39 @@
 Path();%0A    var 
-r
+pluginR
 esources = plugi
@@ -2154,34 +2154,38 @@
 s = 
-plugin.getResources(prefix
+resources.get(plugin.getName()
 );%0A%0A
@@ -2207,17 +2207,23 @@
 older = 
-r
+pluginR
 esources

Update copyPluginAssets to use prepared resources