// Generated by CoffeeScript 2.5.1
(function() {
  var AND, DQUOT, MUST, MUST_NOT, NOT, OR, QLF, SEQ, SHOULD, SQUOT, TERM, builder, escapeDQ, escapeSQ, isTerm, pretty;

  ({isTerm, TERM, MUST, MUST_NOT, SHOULD, QLF, SEQ, OR, AND, NOT, DQUOT, SQUOT} = require("./ast-helper"));

  escapeDQ = function(s) {
    return s.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
  };

  escapeSQ = function(s) {
    return s.replace(/\\/g, '\\\\').replace(/'/g, '\\\'');
  };

  builder = function(indent = "") {
    var hCount, output, stack, vertical;
    stack = [];
    vertical = false;
    output = "";
    hCount = 0;
    return {
      pushV: function(head) {
        if (hCount > 0) {
          if (vertical) {
            output += `\n${indent}`;
          } else {
            output += ", ";
          }
        }
        output += `[${head}`;
        stack.push({indent, vertical});
        indent += "  ";
        vertical = true;
        output += `\n${indent}`;
        hCount = 0;
        return this;
      },
      pushH: function(head) {
        if (hCount > 0) {
          if (vertical) {
            output += `\n${indent}`;
          } else {
            output += ", ";
          }
        }
        output += `[${head}`;
        stack.push({indent, vertical});
        vertical = false;
        hCount++;
        return this;
      },
      pop: function() {
        output += "]";
        return ({vertical, indent} = stack.pop());
      },
      atom: function(s) {
        if (hCount > 0) {
          output += ", ";
        }
        return output += s;
      },
      dquot: function(s) {
        if (hCount > 0) {
          output += ", \"";
        }
        output += s.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
        return output += "\"";
      },
      squot: function(s) {
        if (hCount > 0) {
          output += ", '";
        }
        output += s.replace(/\\/g, '\\\\').replace(/'/g, '\\\'');
        return output += "'";
      },
      output: function() {
        return output;
      }
    };
  };

  pretty = function(t, b = builder()) {
    var arg, args, head, i, j, len, len1;
    if (!isTerm(t)) {
      return;
    }
    [head, ...args] = t;
    switch (head) {
      case SEQ:
      case AND:
      case OR:
        b.pushV(head);
        for (i = 0, len = args.length; i < len; i++) {
          arg = args[i];
          pretty(arg, b);
        }
        return b.pop();
      case NOT:
      case MUST:
      case MUST_NOT:
      case SHOULD:
        b.pushH(head);
        for (j = 0, len1 = args.length; j < len1; j++) {
          arg = args[j];
          pretty(arg, b);
        }
        return b.pop();
      case QLF:
        b.pushH(head);
        b.squot(args[0]);
        pretty(args[1], b);
        return b.pop();
      case DQUOT:
      case SQUOT:
      case TERM:
        b.pushH(head);
        b.squot(args[0]);
        return b.pop();
      default:
        throw new Error("ich hab was vergessen");
    }
  };

  module.exports = function(t, indent = "") {
    var b;
    b = builder(indent);
    pretty(t, b);
    return b.output();
  };

}).call(this);
