'use strict';

exports.__esModule = true;

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var defaultRaw = {
    colon: ': ',
    indent: '  ',
    commentLeft: ' ',
    commentRight: ' '
};

var Stringifier = function () {
    function Stringifier(builder) {
        _classCallCheck(this, Stringifier);

        this.builder = builder;
    }

    Stringifier.prototype.stringify = function stringify(node, semicolon) {
        this[node.type](node, semicolon);
    };

    Stringifier.prototype.root = function root(node) {
        this.body(node);
        if (node.raws.after) this.builder(node.raws.after);
    };

    Stringifier.prototype.comment = function comment(node) {
        var left = defaultRaw.commentLeft;
        var right = defaultRaw.commentRight;
        if (this.has(node.raws.left)) left = node.raws.left;

        if (node.raws.inline) {
            if (this.has(node.raws.inlineRight)) {
                right = node.raws.inlineRight;
            } else {
                right = '';
            }
            this.builder('//' + left + node.text + right, node);
        } else {
            if (this.has(node.raws.right)) right = node.raws.right;
            this.builder('/*' + left + node.text + right + '*/', node);
        }
    };

    Stringifier.prototype.decl = function decl(node) {
        var between = node.raws.between || defaultRaw.colon;
        var string = node.prop + between + this.rawValue(node, 'value');

        if (node.important) {
            string += node.raws.important || ' !important';
        }

        this.builder(string, node);
    };

    Stringifier.prototype.rule = function rule(node) {
        this.block(node, this.rawValue(node, 'selector'));
    };

    Stringifier.prototype.atrule = function atrule(node) {
        var name = '@' + node.name;
        var params = node.params ? this.rawValue(node, 'params') : '';

        if (this.has(node.raws.afterName)) {
            name += node.raws.afterName;
        } else if (params) {
            name += ' ';
        }

        this.block(node, name + params);
    };

    Stringifier.prototype.body = function body(node) {
        var indent = node.root().raws.indent || defaultRaw.indent;

        for (var i = 0; i < node.nodes.length; i++) {
            var child = node.nodes[i];
            var before = child.raws.before.replace(/[^\n]*$/, '') + this.indent(node, indent);
            if (child.type === 'comment' && child.raws.before.indexOf('\n') === -1) {
                before = child.raws.before;
            }
            if (before) this.builder(before);
            this.stringify(child);
        }
    };

    Stringifier.prototype.block = function block(node, start) {
        var between = node.raws.sssBetween || '';
        this.builder(start + between, node, 'start');
        if (this.has(node.nodes)) this.body(node);
    };

    Stringifier.prototype.indent = function indent(node, step) {
        var result = '';
        while (node.parent) {
            result += step;
            node = node.parent;
        }
        return result;
    };

    Stringifier.prototype.has = function has(value) {
        return typeof value !== 'undefined';
    };

    Stringifier.prototype.rawValue = function rawValue(node, prop) {
        var value = node[prop];
        var raw = node.raws[prop];
        if (raw && raw.value === value) {
            return raw.sss || raw.raw;
        } else {
            return value;
        }
    };

    return Stringifier;
}();

exports.default = Stringifier;
module.exports = exports['default'];
//# sourceMappingURL=data:application/json;base64,[encoded data, 9048 characters]