'use strict';

function compose(middlewareArray) {
    if (!middlewareArray.length) {
        return function (_req, _res, next) { next(); };
    }

    let head = middlewareArray[0];
    let tail = middlewareArray.slice(1);

    return function (req, res, next) {
        head(req, res, function (err) {
            if (err) return next(err);
            compose(tail)(req, res, next);
        });
    };
}

module.exports = compose;