'use strict';

const P = require('bluebird');
const transforms = require('./transforms');

/**
 * Iterates through the steps of a processing script and performs the indicated transforms.
 * Transforms may be indicated as either a plain string indicating the name of a transform function
 * attached to the transforms object, or a key-value object, with the key representing the name
 * of a transform function attached to the transform object, and the value representing values to
 * pass in (represented as either an array or an object, depending on the structure of the
 * transform function).
 * @param {!Document} doc page Document object
 * @param {!Object} script processing script
 * @return {!Promise} promise resolving to the transformed doc
 */
module.exports = function(doc, script) {

    /**
     * Process array `arr` in chunks of size `size` per tick using function `fn`.
     * @param {!Array} arr array
     * @param {!Function} fn processing function
     */
    function _processChunked(arr, fn) {
        return P.each(arr, chunk => new P((res, rej) => {
            return setImmediate(() => chunk.forEach(i => fn(i, res, rej)));
        }));
    }

    return _processChunked(script, (step, res, rej) => {
        if (typeof step === 'string') {
            transforms[step](doc);
        } else {
            const transform = Object.keys(step)[0];
            if (Array.isArray(step[transform])) {
                transforms[transform](doc, step[transform].join());
            } else {
                Object.keys(step[transform]).forEach((k) => {
                    const v = step[transform][k];
                    transforms[transform](doc, k, v);
                });
            }
        }
        res();
    }).then(() => doc);
};

@@ -681,16 +681,130 @@
  script%0A
+ * @param %7B?Object%7D options optional object for additional input data that will be passed on to%0A * scalar scripts%0A
  * @retu
@@ -899,16 +899,25 @@
 , script
+, options
 ) %7B%0A%0A   
@@ -1416,16 +1416,25 @@
 tep%5D(doc
+, options
 );%0A     

Add options object to simple processing scripts