import*as Platform from"../platform/platform.js";import{TextCursor}from"./TextCursor.js";import{SourceRange,TextRange}from"./TextRange.js";export class Text{constructor(t){this._value=t}lineEndings(){return this._lineEndings||(this._lineEndings=Platform.StringUtilities.findLineEndingIndexes(this._value)),this._lineEndings}value(){return this._value}lineCount(){return this.lineEndings().length}offsetFromPosition(t,e){return(t?this.lineEndings()[t-1]+1:0)+e}positionFromOffset(t){const e=this.lineEndings(),n=Platform.ArrayUtilities.lowerBound(e,t,Platform.ArrayUtilities.DEFAULT_COMPARATOR);return{lineNumber:n,columnNumber:t-(n&&e[n-1]+1)}}lineAt(t){const e=this.lineEndings(),n=t>0?e[t-1]+1:0,r=e[t];let i=this._value.substring(n,r);return i.length>0&&"\r"===i.charAt(i.length-1)&&(i=i.substring(0,i.length-1)),i}toSourceRange(t){const e=this.offsetFromPosition(t.startLine,t.startColumn),n=this.offsetFromPosition(t.endLine,t.endColumn);return new SourceRange(e,n-e)}toTextRange(t){const e=new TextCursor(this.lineEndings()),n=TextRange.createFromLocation(0,0);return e.resetTo(t.offset),n.startLine=e.lineNumber(),n.startColumn=e.columnNumber(),e.advance(t.offset+t.length),n.endLine=e.lineNumber(),n.endColumn=e.columnNumber(),n}replaceRange(t,e){const n=this.toSourceRange(t);return this._value.substring(0,n.offset)+e+this._value.substring(n.offset+n.length)}extract(t){const e=this.toSourceRange(t);return this._value.substr(e.offset,e.length)}}export let Position;