var PromiseConstructor = typeof Promise === 'undefined' ? require('promise') : Promise;

module.exports = function(environment, ParseTree, ImportManager) {
    var render = function (input, options, callback) {
        var parse = require('./parse')(environment, ParseTree, ImportManager);

        if (typeof(options) === 'function') {
            callback = options;
            options = {};
        }

        if (!callback) {
            var self = this;
            return new PromiseConstructor(function (resolve, reject) {
                render.call(self, input, options, function(err, output) {
                    if (err) {
                        reject(err);
                    } else {
                        resolve(output);
                    }
                });
            });
        } else {
            parse(input, options, function(err, root, imports, options) {
                if (err) { return callback(err); }

                var result;
                try {
                    var parseTree = new ParseTree(root, imports);
                    result = parseTree.toCSS(options);
                }
                catch (err) { return callback(err); }

                callback(null, result);
            });
        }
    };

    return render;
};

var PromiseConstructor = typeof Promise === 'undefined' ? require('promise') : Promise;

module.exports = function(environment, ParseTree, ImportManager) {
    var render = function (input, options, callback) {
        if (typeof(options) === 'function') {
            callback = options;
            options = {};
        }

        if (!callback) {
            var self = this;
            return new PromiseConstructor(function (resolve, reject) {
                render.call(self, input, options, function(err, output) {
                    if (err) {
                        reject(err);
                    } else {
                        resolve(output);
                    }
                });
            });
        } else {
            this.parse(input, options, function(err, root, imports, options) {
                if (err) { return callback(err); }

                var result;
                try {
                    var parseTree = new ParseTree(root, imports);
                    result = parseTree.toCSS(options);
                }
                catch (err) { return callback(err); }

                callback(null, result);
            });
        }
    };

    return render;
};

Fix parse refactor - this passed to the plugin manager must be less
Fix parse refactor - this passed to the plugin manager must be less
