"use strict";

var Bluebird = require("bluebird");
var path = require("path");
var base = require(path.join(__dirname, "..", "pre-commit-base"));
var less = require("less");

function attemptRender(filename, src, resolve, reject, globals) {
	globals = globals || {};
	less.render(src, {
		// Specify search paths for @import directives
		paths : [
			"public/static/less"
		],
		// Specify a filename, for better error messages
		filename : filename,
		modifyVars : globals,
		compress : false
	}, function(e, css) {
		if (e) {
			if ((/^variable @(.+?) is undefined$/).test(e.message)) {
				// ignore undef variable
				globals[(/^variable @(.+?) is undefined$/).exec(e.message)[1]] = "1";
				attemptRender(filename, src, resolve, reject, globals);
				return;
			}
			base.writeError("LESS", filename, e.line, e.message);
			reject();
			return;
		}
		resolve({
			filename : filename,
			src : css
		});
	});
}

module.exports = function(data) {
	return new Bluebird(function(resolve, reject) {
		attemptRender(data.filename, data.src, resolve, reject);
	});
};

@@ -891,16 +891,27 @@
 %09%09%09src :
+ css.css %7C%7C
  css%0A%09%09%7D

Fix less hook for imports