export class FormattedContentBuilder{constructor(t){this.indentString=t,this.lastOriginalPosition=0,this.formattedContent=[],this.formattedContentLength=0,this.lastFormattedPosition=0,this.nestingLevel=0,this.newLines=0,this.enforceSpaceBetweenWords=!0,this.softSpace=!1,this.hardSpaces=0,this.cachedIndents=new Map,this.mapping={original:[0],formatted:[0]}}setEnforceSpaceBetweenWords(t){const e=this.enforceSpaceBetweenWords;return this.enforceSpaceBetweenWords=t,e}addToken(t,e){const n=this.formattedContent[this.formattedContent.length-1];this.enforceSpaceBetweenWords&&n&&/\w/.test(n[n.length-1])&&/\w/.test(t)&&this.addSoftSpace(),this.appendFormatting(),this.addMappingIfNeeded(e),this.addText(t)}addSoftSpace(){this.hardSpaces||(this.softSpace=!0)}addHardSpace(){this.softSpace=!1,++this.hardSpaces}addNewLine(t){this.formattedContentLength&&(t?++this.newLines:this.newLines=this.newLines||1)}increaseNestingLevel(){this.nestingLevel+=1}decreaseNestingLevel(){this.nestingLevel>0&&(this.nestingLevel-=1)}content(){return this.formattedContent.join("")+(this.newLines?"\n":"")}appendFormatting(){if(this.newLines){for(let t=0;t<this.newLines;++t)this.addText("\n");this.addText(this.indent())}else this.softSpace&&this.addText(" ");if(this.hardSpaces)for(let t=0;t<this.hardSpaces;++t)this.addText(" ");this.newLines=0,this.softSpace=!1,this.hardSpaces=0}indent(){const t=this.cachedIndents.get(this.nestingLevel);if(t)return t;let e="";for(let n=0;n<this.nestingLevel;++n)e+=this.indentString;return this.nestingLevel<=20&&this.cachedIndents.set(this.nestingLevel,e),e}addText(t){this.formattedContent.push(t),this.formattedContentLength+=t.length}addMappingIfNeeded(t){t-this.lastOriginalPosition!==this.formattedContentLength-this.lastFormattedPosition&&(this.mapping.original.push(t),this.lastOriginalPosition=t,this.mapping.formatted.push(this.formattedContentLength),this.lastFormattedPosition=this.formattedContentLength)}}