var SourceMapGenerator=require("./source-map-generator").SourceMapGenerator,util=require("./util"),REGEX_NEWLINE=/(\r?\n)/,NEWLINE_CODE=10,isSourceNode="$$$isSourceNode$$$";function SourceNode(e,n,o,r,t){this.children=[],this.sourceContents={},this.line=null==e?null:e,this.column=null==n?null:n,this.source=null==o?null:o,this.name=null==t?null:t,this[isSourceNode]=!0,null!=r&&this.add(r)}SourceNode.fromStringWithSourceMap=function(e,n,o){var r=new SourceNode,t=e.split(REGEX_NEWLINE),i=0,u=function(){return e()+(e()||"");function e(){return i<t.length?t[i++]:void 0}},l=1,c=0,s=null;return n.eachMapping(function(e){if(null!==s){if(!(l<e.generatedLine)){var n=(o=t[i]||"").substr(0,e.generatedColumn-c);return t[i]=o.substr(e.generatedColumn-c),c=e.generatedColumn,a(s,n),void(s=e)}a(s,u()),l++,c=0}for(;l<e.generatedLine;)r.add(u()),l++;if(c<e.generatedColumn){var o=t[i]||"";r.add(o.substr(0,e.generatedColumn)),t[i]=o.substr(e.generatedColumn),c=e.generatedColumn}s=e},this),i<t.length&&(s&&a(s,u()),r.add(t.splice(i).join(""))),n.sources.forEach(function(e){var t=n.sourceContentFor(e);null!=t&&(null!=o&&(e=util.join(o,e)),r.setSourceContent(e,t))}),r;function a(e,n){if(null===e||void 0===e.source)r.add(n);else{var t=o?util.join(o,e.source):e.source;r.add(new SourceNode(e.originalLine,e.originalColumn,t,n,e.name))}}},SourceNode.prototype.add=function(e){if(Array.isArray(e))e.forEach(function(e){this.add(e)},this);else{if(!e[isSourceNode]&&"string"!=typeof e)throw new TypeError("Expected a SourceNode, string, or an array of SourceNodes and strings. Got "+e);e&&this.children.push(e)}return this},SourceNode.prototype.prepend=function(e){if(Array.isArray(e))for(var n=e.length-1;n>=0;n--)this.prepend(e[n]);else{if(!e[isSourceNode]&&"string"!=typeof e)throw new TypeError("Expected a SourceNode, string, or an array of SourceNodes and strings. Got "+e);this.children.unshift(e)}return this},SourceNode.prototype.walk=function(e){for(var n,o=0,r=this.children.length;o<r;o++)(n=this.children[o])[isSourceNode]?n.walk(e):""!==n&&e(n,{source:this.source,line:this.line,column:this.column,name:this.name})},SourceNode.prototype.join=function(e){var n,o,r=this.children.length;if(r>0){for(n=[],o=0;o<r-1;o++)n.push(this.children[o]),n.push(e);n.push(this.children[o]),this.children=n}return this},SourceNode.prototype.replaceRight=function(e,n){var o=this.children[this.children.length-1];return o[isSourceNode]?o.replaceRight(e,n):"string"==typeof o?this.children[this.children.length-1]=o.replace(e,n):this.children.push("".replace(e,n)),this},SourceNode.prototype.setSourceContent=function(e,n){this.sourceContents[util.toSetString(e)]=n},SourceNode.prototype.walkSourceContents=function(e){for(var n=0,o=this.children.length;n<o;n++)this.children[n][isSourceNode]&&this.children[n].walkSourceContents(e);var r=Object.keys(this.sourceContents);for(n=0,o=r.length;n<o;n++)e(util.fromSetString(r[n]),this.sourceContents[r[n]])},SourceNode.prototype.toString=function(){var e="";return this.walk(function(n){e+=n}),e},SourceNode.prototype.toStringWithSourceMap=function(e){var n={code:"",line:1,column:0},o=new SourceMapGenerator(e),r=!1,t=null,i=null,u=null,l=null;return this.walk(function(e,c){n.code+=e,null!==c.source&&null!==c.line&&null!==c.column?(t===c.source&&i===c.line&&u===c.column&&l===c.name||o.addMapping({source:c.source,original:{line:c.line,column:c.column},generated:{line:n.line,column:n.column},name:c.name}),t=c.source,i=c.line,u=c.column,l=c.name,r=!0):r&&(o.addMapping({generated:{line:n.line,column:n.column}}),t=null,r=!1);for(var s=0,a=e.length;s<a;s++)e.charCodeAt(s)===NEWLINE_CODE?(n.line++,n.column=0,s+1===a?(t=null,r=!1):r&&o.addMapping({source:c.source,original:{line:c.line,column:c.column},generated:{line:n.line,column:n.column},name:c.name})):n.column++}),this.walkSourceContents(function(e,n){o.setSourceContent(e,n)}),{code:n.code,map:o}},exports.SourceNode=SourceNode;