'use strict';
var fs = require('fs');
var path = require('path');
var css = require('css');
var globby = require('globby');
var parse = require('parse-import');
var urlRegex = require('url-regex');

/**
 * Get options
 *
 * @param {Array} rules
 * @param {Object} opts
 * @api private
 */

function getOptions(rules, opts) {
	var dir;
	var obj = {
		source: opts.source,
		transform: opts.transform || function (val) {
			return val;
		}
	};

	obj.path = opts.path || [];
	obj.path = Array.isArray(obj.path) ? obj.path : [obj.path];

	if (rules.length && rules[0].position && rules[0].position.source) {
		dir = path.dirname(rules[0].position.source);
	}

	if (dir && obj.path.indexOf(dir) === -1) {
		obj.path.unshift(dir);
	}

	if (!obj.path.length) {
		obj.path.push(process.cwd());
	}

	return obj;
}

/**
 * Create error
 *
 * @param {String} file
 * @param {String} src
 * @param {Array} paths
 * @api private
 */

function createError(file, src, paths) {
	var err = ['Failed to find ' + file];

	if (src) {
		err.push('from ' + src);
	}

	err.push([
		'in [',
		'    ' + paths.join(',\n    '),
		']'
	].join('\n'));

	return err.join(' ');
}

/**
 * Check if a file exists
 *
 * @param {String} file
 * @param {String} src
 * @param {Object} opts
 * @api private
 */

function exists(file, src, opts) {
	var files = opts.path.map(function (dir) {
		return path.join(dir, file);
	});

	files = globby.sync(files);

	if (!files.length) {
		throw new Error(createError(file, src, opts.path));
	}

	return files[0];
}

/**
 * Read the contents of a file
 *
 * @param {String} file
 * @param {Object} opts
 * @api private
 */

function read(file, opts) {
	var encoding = opts.encoding || 'utf8';
	var data = opts.transform(fs.readFileSync(file, encoding));
	return css.parse(data, {source: file}).stylesheet;
}

/**
 * Run
 *
 * @param {Object} style
 * @param {Object} opts
 * @api private
 */

function run(style, opts) {
	opts = getOptions(style.rules, opts || {});

	var rules = style.rules || [];
	var ret = [];

	rules.forEach(function (rule) {
		if (rule.type !== 'import') {
			ret.push(rule);
			return;
		}

		var importRule = '@import ' + rule.import + ';';
		var data = parse(importRule)[0];
		var pos = rule.position ? rule.position.source : null;

		if (urlRegex({ exact: true }).test(data.path)) {
			ret.push(rule);
			return;
		}

		opts.source = exists(data.path, pos, opts);

		if (opts.path.indexOf(path.dirname(opts.source)) === -1) {
			opts.path.unshift(path.dirname(opts.source));
		}

		var content = read(opts.source, opts);
		run(content, opts);

		if (!data.condition || !data.condition.length) {
			ret = ret.concat(content.rules);
			return;
		}

		ret.push({
			media: data.condition,
			rules: content.rules,
			type: 'media'
		});
	});

	style.rules = ret;
}

/**
 * Module exports
 */

module.exports = function (opts) {
	return function (style) {
		run(style, opts);
	};
};

@@ -1139,24 +1139,603 @@
 in(' ');%0A%7D%0A%0A
+/**%0A * Create bad import rule error%0A *%0A * @param %7BString%7D rule%0A * @api private%0A */%0A%0Afunction createImportError(rule) %7B%0A%09var url = rule.import ? rule.import.replace(/%5Cr?%5Cn/g, ' ') : '%3Cno url%3E';%0A%09var err = %5B'Bad import url: @import ' + url%5D;%0A%0A%09if (rule.position) %7B%0A%09%09err.push('  starting at line ' + rule.position.start.line + ' column ' + rule.position.start.column);%0A%09%09err.push('    ending at line ' + rule.position.end.line + ' column ' + rule.position.end.column);%0A%0A%09%09if (rule.position.source) %7B%0A%09%09%09err.push('  in ' + rule.position.source);%0A%09%09%7D%0A%09%7D%0A%0A%09return err.join('%5Cn');%0A %7D%0A%0A
 /**%0A * Check
@@ -2836,16 +2836,77 @@
  null;%0A%0A
+%09%09if (!data) %7B%0A%09%09%09throw Error(createImportError(rule));%0A%09%09%7D%0A%0A
 %09%09if (ur

create meaningful exception for: Cannot read property 'path' of undefined