'use strict';

var Enum = require('./Enum.js');
var Utility = require('./Utility.js');
var Tokenizer = require('./Tokenizer.js');

/**
 * @param {string} value
 * @return {object[]}
 */
function compile (value) {
	return Tokenizer.dealloc(parse('', null, null, null, [''], value = Tokenizer.alloc(value), 0, [0], value))
}

/**
 * @param {string} value
 * @param {object} root
 * @param {object?} parent
 * @param {string[]} rule
 * @param {string[]} rules
 * @param {string[]} rulesets
 * @param {number[]} pseudo
 * @param {number[]} points
 * @param {string[]} declarations
 * @return {object}
 */
function parse (value, root, parent, rule, rules, rulesets, pseudo, points, declarations) {
	var index = 0;
	var offset = 0;
	var length = pseudo;
	var atrule = 0;
	var property = 0;
	var previous = 0;
	var variable = 1;
	var scanning = 1;
	var ampersand = 1;
	var character = 0;
	var type = '';
	var props = rules;
	var children = rulesets;
	var reference = rule;
	var characters = type;

	while (scanning)
		switch (previous = character, character = Tokenizer.next()) {
			// (
			case 40:
				if (previous != 108 && Utility.charat(characters, length - 1) == 58) {
					if (Utility.indexof(characters += Utility.replace(Tokenizer.delimit(character), '&', '&\f'), '&\f', Utility.abs(index ? points[index - 1] : 0)) != -1)
						ampersand = -1;
					break
				}
			// " ' [
			case 34: case 39: case 91:
				characters += Tokenizer.delimit(character);
				break
			// \t \n \r \s
			case 9: case 10: case 13: case 32:
				characters += Tokenizer.whitespace(previous);
				break
			// \
			case 92:
				characters += Tokenizer.escaping(Tokenizer.caret() - 1, 7);
				continue
			// /
			case 47:
				switch (Tokenizer.peek()) {
					case 42: case 47:
						Utility.append(comment(Tokenizer.commenter(Tokenizer.next(), Tokenizer.caret()), root, parent, declarations), declarations);
						if ((Tokenizer.token(previous || 1) == 5 || Tokenizer.token(Tokenizer.peek() || 1) == 5) && Utility.strlen(characters) && Utility.substr(characters, -1, void 0) !== ' ') characters += ' ';
						break
					default:
						characters += '/';
				}
				break
			// {
			case 123 * variable:
				points[index++] = Utility.strlen(characters) * ampersand;
			// } ; \0
			case 125 * variable: case 59: case 0:
				switch (character) {
					// \0 }
					case 0: case 125: scanning = 0;
					// ;
					case 59 + offset: if (ampersand == -1) characters = Utility.replace(characters, /\f/g, '');
						if (property > 0 && (Utility.strlen(characters) - length || (variable === 0 && previous === 47)))
							Utility.append(property > 32 ? declaration(characters + ';', rule, parent, length - 1, declarations) : declaration(Utility.replace(characters, ' ', '') + ';', rule, parent, length - 2, declarations), declarations);
						break
					// @ ;
					case 59: characters += ';';
					// { rule/at-rule
					default:
						Utility.append(reference = ruleset(characters, root, parent, index, offset, rules, points, type, props = [], children = [], length, rulesets), rulesets);

						if (character === 123)
							if (offset === 0)
								parse(characters, root, reference, reference, props, rulesets, length, points, children);
							else {
								switch (atrule) {
									// c(ontainer)
									case 99:
										if (Utility.charat(characters, 3) === 110) break
									// l(ayer)
									case 108:
										if (Utility.charat(characters, 2) === 97) break
									default:
										offset = 0;
									// d(ocument) m(edia) s(upports)
									case 100: case 109: case 115:
								}
								if (offset) parse(value, reference, reference, rule && Utility.append(ruleset(value, reference, reference, 0, 0, rules, points, type, rules, props = [], length, children), children), rules, children, length, points, rule ? props : children);
								else parse(characters, reference, reference, reference, [''], children, 0, points, children);
							}
				}

				index = offset = property = 0, variable = ampersand = 1, type = characters = '', length = pseudo;
				break
			// :
			case 58:
				length = 1 + Utility.strlen(characters), property = previous;
			default:
				if (variable < 1)
					if (character == 123)
						--variable;
					else if (character == 125 && variable++ == 0 && Tokenizer.prev() == 125)
						continue

				switch (characters += Utility.from(character), character * variable) {
					// &
					case 38:
						ampersand = offset > 0 ? 1 : (characters += '\f', -1);
						break
					// ,
					case 44:
						points[index++] = (Utility.strlen(characters) - 1) * ampersand, ampersand = 1;
						break
					// @
					case 64:
						// -
						if (Tokenizer.peek() === 45)
							characters += Tokenizer.delimit(Tokenizer.next());

						atrule = Tokenizer.peek(), offset = length = Utility.strlen(type = characters += Tokenizer.identifier(Tokenizer.caret())), character++;
						break
					// -
					case 45:
						if (previous === 45 && Utility.strlen(characters) == 2)
							variable = 0;
				}
		}

	return rulesets
}

/**
 * @param {string} value
 * @param {object} root
 * @param {object?} parent
 * @param {number} index
 * @param {number} offset
 * @param {string[]} rules
 * @param {number[]} points
 * @param {string} type
 * @param {string[]} props
 * @param {string[]} children
 * @param {number} length
 * @param {object[]} siblings
 * @return {object}
 */
function ruleset (value, root, parent, index, offset, rules, points, type, props, children, length, siblings) {
	var post = offset - 1;
	var rule = offset === 0 ? rules : [''];
	var size = Utility.sizeof(rule);

	for (var i = 0, j = 0, k = 0; i < index; ++i)
		for (var x = 0, y = Utility.substr(value, post + 1, post = Utility.abs(j = points[i])), z = value; x < size; ++x)
			if (z = Utility.trim(j > 0 ? rule[x] + ' ' + y : Utility.replace(y, /&\f/g, rule[x])))
				props[k++] = z;

	return Tokenizer.node(value, root, parent, offset === 0 ? Enum.RULESET : type, props, children, length, siblings)
}

/**
 * @param {number} value
 * @param {object} root
 * @param {object?} parent
 * @param {object[]} siblings
 * @return {object}
 */
function comment (value, root, parent, siblings) {
	return Tokenizer.node(value, root, parent, Enum.COMMENT, Utility.from(Tokenizer.char()), Utility.substr(value, 2, -2), 0, siblings)
}

/**
 * @param {string} value
 * @param {object} root
 * @param {object?} parent
 * @param {number} length
 * @param {object[]} siblings
 * @return {object}
 */
function declaration (value, root, parent, length, siblings) {
	return Tokenizer.node(value, root, parent, Enum.DECLARATION, Utility.substr(value, 0, length), Utility.substr(value, length + 1, -1), length, siblings)
}

exports.comment = comment;
exports.compile = compile;
exports.declaration = declaration;
exports.parse = parse;
exports.ruleset = ruleset;
