'use strict';

var _commonjsHelpers = require('../../../_virtual/_commonjsHelpers.js');
var XMLStreamWriter = require('../../../_virtual/XMLStreamWriter.js');
var NodeType = require('./NodeType.js');
var XMLWriterBase = require('./XMLWriterBase.js');
var WriterState = require('./WriterState.js');

var hasRequiredXMLStreamWriter;

function requireXMLStreamWriter () {
	if (hasRequiredXMLStreamWriter) return XMLStreamWriter.__module.exports;
	hasRequiredXMLStreamWriter = 1;
	// Generated by CoffeeScript 1.12.7
	(function() {
	  var NodeType$1, WriterState$1, XMLWriterBase$1,
	    extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
	    hasProp = {}.hasOwnProperty;

	  NodeType$1 = NodeType.__require();

	  XMLWriterBase$1 = XMLWriterBase.__require();

	  WriterState$1 = WriterState.__require();

	  XMLStreamWriter.__module.exports = (function(superClass) {
	    extend(XMLStreamWriter, superClass);

	    function XMLStreamWriter(stream, options) {
	      this.stream = stream;
	      XMLStreamWriter.__super__.constructor.call(this, options);
	    }

	    XMLStreamWriter.prototype.endline = function(node, options, level) {
	      if (node.isLastRootNode && options.state === WriterState$1.CloseTag) {
	        return '';
	      } else {
	        return XMLStreamWriter.__super__.endline.call(this, node, options, level);
	      }
	    };

	    XMLStreamWriter.prototype.document = function(doc, options) {
	      var child, i, j, k, len, len1, ref, ref1, results;
	      ref = doc.children;
	      for (i = j = 0, len = ref.length; j < len; i = ++j) {
	        child = ref[i];
	        child.isLastRootNode = i === doc.children.length - 1;
	      }
	      options = this.filterOptions(options);
	      ref1 = doc.children;
	      results = [];
	      for (k = 0, len1 = ref1.length; k < len1; k++) {
	        child = ref1[k];
	        results.push(this.writeChildNode(child, options, 0));
	      }
	      return results;
	    };

	    XMLStreamWriter.prototype.attribute = function(att, options, level) {
	      return this.stream.write(XMLStreamWriter.__super__.attribute.call(this, att, options, level));
	    };

	    XMLStreamWriter.prototype.cdata = function(node, options, level) {
	      return this.stream.write(XMLStreamWriter.__super__.cdata.call(this, node, options, level));
	    };

	    XMLStreamWriter.prototype.comment = function(node, options, level) {
	      return this.stream.write(XMLStreamWriter.__super__.comment.call(this, node, options, level));
	    };

	    XMLStreamWriter.prototype.declaration = function(node, options, level) {
	      return this.stream.write(XMLStreamWriter.__super__.declaration.call(this, node, options, level));
	    };

	    XMLStreamWriter.prototype.docType = function(node, options, level) {
	      var child, j, len, ref;
	      level || (level = 0);
	      this.openNode(node, options, level);
	      options.state = WriterState$1.OpenTag;
	      this.stream.write(this.indent(node, options, level));
	      this.stream.write('<!DOCTYPE ' + node.root().name);
	      if (node.pubID && node.sysID) {
	        this.stream.write(' PUBLIC "' + node.pubID + '" "' + node.sysID + '"');
	      } else if (node.sysID) {
	        this.stream.write(' SYSTEM "' + node.sysID + '"');
	      }
	      if (node.children.length > 0) {
	        this.stream.write(' [');
	        this.stream.write(this.endline(node, options, level));
	        options.state = WriterState$1.InsideTag;
	        ref = node.children;
	        for (j = 0, len = ref.length; j < len; j++) {
	          child = ref[j];
	          this.writeChildNode(child, options, level + 1);
	        }
	        options.state = WriterState$1.CloseTag;
	        this.stream.write(']');
	      }
	      options.state = WriterState$1.CloseTag;
	      this.stream.write(options.spaceBeforeSlash + '>');
	      this.stream.write(this.endline(node, options, level));
	      options.state = WriterState$1.None;
	      return this.closeNode(node, options, level);
	    };

	    XMLStreamWriter.prototype.element = function(node, options, level) {
	      var att, child, childNodeCount, firstChildNode, j, len, name, ref, ref1;
	      level || (level = 0);
	      this.openNode(node, options, level);
	      options.state = WriterState$1.OpenTag;
	      this.stream.write(this.indent(node, options, level) + '<' + node.name);
	      ref = node.attribs;
	      for (name in ref) {
	        if (!hasProp.call(ref, name)) continue;
	        att = ref[name];
	        this.attribute(att, options, level);
	      }
	      childNodeCount = node.children.length;
	      firstChildNode = childNodeCount === 0 ? null : node.children[0];
	      if (childNodeCount === 0 || node.children.every(function(e) {
	        return (e.type === NodeType$1.Text || e.type === NodeType$1.Raw) && e.value === '';
	      })) {
	        if (options.allowEmpty) {
	          this.stream.write('>');
	          options.state = WriterState$1.CloseTag;
	          this.stream.write('</' + node.name + '>');
	        } else {
	          options.state = WriterState$1.CloseTag;
	          this.stream.write(options.spaceBeforeSlash + '/>');
	        }
	      } else if (options.pretty && childNodeCount === 1 && (firstChildNode.type === NodeType$1.Text || firstChildNode.type === NodeType$1.Raw) && (firstChildNode.value != null)) {
	        this.stream.write('>');
	        options.state = WriterState$1.InsideTag;
	        options.suppressPrettyCount++;
	        this.writeChildNode(firstChildNode, options, level + 1);
	        options.suppressPrettyCount--;
	        options.state = WriterState$1.CloseTag;
	        this.stream.write('</' + node.name + '>');
	      } else {
	        this.stream.write('>' + this.endline(node, options, level));
	        options.state = WriterState$1.InsideTag;
	        ref1 = node.children;
	        for (j = 0, len = ref1.length; j < len; j++) {
	          child = ref1[j];
	          this.writeChildNode(child, options, level + 1);
	        }
	        options.state = WriterState$1.CloseTag;
	        this.stream.write(this.indent(node, options, level) + '</' + node.name + '>');
	      }
	      this.stream.write(this.endline(node, options, level));
	      options.state = WriterState$1.None;
	      return this.closeNode(node, options, level);
	    };

	    XMLStreamWriter.prototype.processingInstruction = function(node, options, level) {
	      return this.stream.write(XMLStreamWriter.__super__.processingInstruction.call(this, node, options, level));
	    };

	    XMLStreamWriter.prototype.raw = function(node, options, level) {
	      return this.stream.write(XMLStreamWriter.__super__.raw.call(this, node, options, level));
	    };

	    XMLStreamWriter.prototype.text = function(node, options, level) {
	      return this.stream.write(XMLStreamWriter.__super__.text.call(this, node, options, level));
	    };

	    XMLStreamWriter.prototype.dtdAttList = function(node, options, level) {
	      return this.stream.write(XMLStreamWriter.__super__.dtdAttList.call(this, node, options, level));
	    };

	    XMLStreamWriter.prototype.dtdElement = function(node, options, level) {
	      return this.stream.write(XMLStreamWriter.__super__.dtdElement.call(this, node, options, level));
	    };

	    XMLStreamWriter.prototype.dtdEntity = function(node, options, level) {
	      return this.stream.write(XMLStreamWriter.__super__.dtdEntity.call(this, node, options, level));
	    };

	    XMLStreamWriter.prototype.dtdNotation = function(node, options, level) {
	      return this.stream.write(XMLStreamWriter.__super__.dtdNotation.call(this, node, options, level));
	    };

	    return XMLStreamWriter;

	  })(XMLWriterBase$1);

	}).call(_commonjsHelpers.commonjsGlobal);
	return XMLStreamWriter.__module.exports;
}

exports.__require = requireXMLStreamWriter;
