var Common;!function(e){!function(t){"use strict";var r=e.Promises.isCancelablePromise,s=e.Promises.CancelablePromise;class i{constructor(e=t.FormatServiceHelper.DefaultServices){this._options=null;this._services=e;this._sourceText=null;this._sourceInfo=null;this._options=new t.FormatterOptions;this._canBePrettyPrinted=!0;this._prettyPrintPromise=null}get canFormat(){return this._canBePrettyPrinted}get sourceText(){return this._sourceText}get options(){return this._options}updateOptions(e){this._options.updateAllOptions(e)}setDocument(e,s,i,o,n){F12.Tools.Utility.Assert.hasValue(e,"source does not have a value.");return new Promise((s,o)=>{this._sourceText=new t.UnmappedTextMapping(e);this._sourceInfo={name:"",mimeType:i,sourceType:2};this._canBePrettyPrinted=e.length>0&&!/^[\s;]+$/.test(e);if(this._prettyPrintPromise){r(this._prettyPrintPromise)&&this._prettyPrintPromise.cancel();this._prettyPrintPromise=null}n&&this._canBePrettyPrinted&&this.formatSource(this._options.forceChunking);s(this._sourceInfo)})}getFormattedText(e){F12.Tools.Utility.Assert.isTrue(!!this.sourceText,"Must call setDocument first");if(2===((e=e||this._options).prettyPrint&&this._canBePrettyPrinted?2:0)){this._prettyPrintPromise||this.formatSource(e.forceChunking);F12.Tools.Utility.Assert.hasValue(this._prettyPrintPromise,"No pretty print promise.");return this._prettyPrintPromise}return Promise.resolve(this._sourceText)}formatSource(r){let i=null;F12.Tools.Utility.Assert.isTrue(this._canBePrettyPrinted,"Source cannot be pretty printed.");if(this._prettyPrintPromise)return;let o=()=>{if(i){i.terminate();return!0}i=null;return!1};this._prettyPrintPromise=new s((s,n,a)=>{window.setImmediate(()=>{var n=t.FormatServiceHelper.getWorkerServices(new e.Uri("formatWorker.js"),this._services),a=null;(i=new Worker(n.path)).onerror=(r=>{this._canBePrettyPrinted=!1;a=e.ToolWindowHelpers.loadString("FormatterInternalError");o();s(new t.UnmappedTextMapping(this._sourceText.text,a))});var l=[];i.onmessage=(r=>{r.error&&F12.Tools.Utility.Assert.failDebugOnly(r.errorMessage);if(void 0===r.data.generatedSourceSpanIndex){var o=r.data;this._canBePrettyPrinted=o.canBePrettyPrinted;var n=null;if(this._canBePrettyPrinted){var c=o.generatedSourceSpanData,u=[];for(P=0;P<c.length;P++){if(l[P]){F12.Tools.Utility.Assert.isNull(c[P].mappings);c[P].mappings=l[P]}var p=new t.SourceSpan(c[P]);u.push(p)}if(o.error){a=e.ToolWindowHelpers.loadString(u.length>1?"FormatterEmbededJSInternalError":"FormatterInternalError");if(o.errorMessage.length>0){a+="\n";a+=o.errorMessage}}n=new t.FormattedTextMapping(2,o.formattedText,this._sourceText,u,a)}i.terminate();i=null;s(n||this.sourceText)}else{var m=r.data;l[m.generatedSourceSpanIndex]||(l[m.generatedSourceSpanIndex]=[]);for(var h=l[m.generatedSourceSpanIndex],P=0;P<m.mappings.length;P++)h.push(m.mappings[P])}});var c=new t.WorkerSendMessage;c.source=this._sourceText.text;c.mimeType=this._sourceInfo.mimeType;c.commonUrl=n.services.commonRoot;c.loader=n.services.editorRoot;c.forceResponseChunking=r;i.postMessage(c)})},o)}}t.ScriptFormatter=i}(e.FormatService||(e.FormatService={}))}(Common||(Common={}));
