'use strict';

exports.__esModule = true;
exports.default = preprocess;
function indentError(input, l, p) {
    throw input.error('Mixed tabs and spaces are not allowed', l, p + 1);
}

function preprocess(input, lines) {
    var indentType = void 0;
    var prevNumber = 0;
    var parts = lines.map(function (line) {
        var lastComma = false;
        var comment = false;
        var number = prevNumber + 1;
        var atrule = false;
        var indent = '';
        var tokens = [];
        var colon = false;

        if (line.length > 0) {
            if (line[0][0] === 'space') {
                indent = line[0][1];
                tokens = line.slice(1);
            } else {
                indent = '';
                tokens = line;
            }

            if (!indentType && indent.length) {
                indentType = indent[0] === ' ' ? 'space' : 'tab';
            }
            if (indentType === 'space') {
                if (indent.indexOf('\t') !== -1) {
                    indentError(input, number, indent.indexOf('\t'));
                }
            } else if (indentType === 'tab') {
                if (indent.indexOf(' ') !== -1) {
                    indentError(input, number, indent.indexOf(' '));
                }
            }

            if (tokens.length) {
                for (var i = tokens.length - 1; i >= 0; i--) {
                    var type = tokens[i][0];
                    if (type === ',') {
                        lastComma = true;
                        break;
                    } else if (type === 'space') {
                        continue;
                    } else if (type === 'comment') {
                        continue;
                    } else if (type === 'newline') {
                        continue;
                    } else {
                        break;
                    }
                }
                comment = tokens[0][0] === 'comment';
                atrule = tokens[0][0] === 'at-word';

                var brackets = 0;
                for (var _i = 0; _i < tokens.length - 1; _i++) {
                    var _type = tokens[_i][0];
                    var next = tokens[_i + 1][0];
                    if (_type === '(') {
                        brackets += 1;
                    } else if (_type === ')') {
                        brackets -= 1;
                    } else if (_type === ':' && brackets === 0 && (next === 'space' || next === 'newline')) {
                        colon = true;
                    }
                }
            }

            var last = tokens[tokens.length - 1];
            if (last && last[0] === 'newline') prevNumber = last[2];
        }

        return {
            number: number,
            indent: indent,
            colon: colon,
            tokens: tokens,
            atrule: atrule,
            comment: comment,
            lastComma: lastComma,
            before: ''
        };
    });

    parts = parts.reduceRight(function (all, i) {
        if (!i.tokens.length || i.tokens.every(function (j) {
            return j[0] === 'newline';
        })) {
            var prev = all[0];
            var before = i.indent + i.tokens.map(function (j) {
                return j[1];
            }).join('');
            prev.before = before + prev.before;
        } else {
            all.unshift(i);
        }
        return all;
    }, [{ end: true, before: '' }]);

    parts.forEach(function (part, i) {
        if (i === 0) return;

        var prev = parts[i - 1];
        var last = prev.tokens[prev.tokens.length - 1];
        if (last && last[0] === 'newline') {
            part.before = last[1] + part.before;
            prev.tokens.pop();
        }
    });

    return parts;
}
module.exports = exports['default'];
//# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 9744 characters]