var E=Object.defineProperty;var g=(s,t)=>{for(var r in t)E(s,r,{get:t[r],enumerable:!0})};var C={};g(C,{FormatterWorkerPool:()=>u,formatterWorkerPool:()=>h});import*as T from"./../../core/platform/platform.js";var c,u=class s{taskQueue;workerTasks;entrypointURL;constructor(t){this.taskQueue=[],this.workerTasks=new Map,this.entrypointURL=t??import.meta.resolve("../../entrypoints/formatter_worker/formatter_worker-entrypoint.js")}static instance(t){return(!c||t?.forceNew)&&(c=new s(t?.entrypointURL)),c}dispose(){for(let t of this.taskQueue)console.error("rejecting task"),t.errorCallback(new Event("Worker terminated"));for(let[t,r]of this.workerTasks.entries())r?.errorCallback(new Event("Worker terminated")),t.terminate(!0)}static removeInstance(){c?.dispose(),c=void 0}createWorker(){let t=T.HostRuntime.HOST_RUNTIME.createWorker(this.entrypointURL);return t.onmessage=this.onWorkerMessage.bind(this,t),t.onerror=this.onWorkerError.bind(this,t),t}processNextTask(){let t=Math.max(2,navigator.hardwareConcurrency-1);if(!this.taskQueue.length)return;let r=[...this.workerTasks.keys()].find(n=>!this.workerTasks.get(n));if(!r&&this.workerTasks.size<t&&(r=this.createWorker()),!r)return;let e=this.taskQueue.shift();e&&(this.workerTasks.set(r,e),r.postMessage({method:e.method,params:e.params}))}onWorkerMessage(t,r){let e=this.workerTasks.get(t);if(e){if(e.isChunked&&r.data&&!r.data.isLastChunk){e.callback(r.data);return}this.workerTasks.set(t,null),this.processNextTask(),e.callback(r.data?r.data:null)}}onWorkerError(t,r){console.error(r);let e=this.workerTasks.get(t);t.terminate(),this.workerTasks.delete(t);let n=this.createWorker();this.workerTasks.set(n,null),this.processNextTask(),e&&e.errorCallback(r)}runChunkedTask(t,r,e){let n=new l(t,r,o,()=>o(null),!0);this.taskQueue.push(n),this.processNextTask();function o(i){if(!i){e(!0,null);return}let k="isLastChunk"in i&&!!i.isLastChunk,m="chunk"in i&&i.chunk;e(k,m)}}runTask(t,r){return new Promise((e,n)=>{let o=new l(t,r,e,n,!1);this.taskQueue.push(o),this.processNextTask()})}format(t,r,e){let n={mimeType:t,content:r,indentString:e};return this.runTask("format",n)}javaScriptSubstitute(t,r){return r.size===0?Promise.resolve(t):this.runTask("javaScriptSubstitute",{content:t,mapping:r}).then(e=>e||"")}javaScriptScopeTree(t,r="script"){return this.runTask("javaScriptScopeTree",{content:t,sourceType:r}).then(e=>e||null)}parseCSS(t,r){this.runChunkedTask("parseCSS",{content:t},e);function e(n,o){r(n,o||[])}}},l=class{method;params;callback;errorCallback;isChunked;constructor(t,r,e,n,o){this.method=t,this.params=r,this.callback=e,this.errorCallback=n,this.isChunked=o}};function h(){return u.instance()}var L={};g(L,{format:()=>x,formatScriptContent:()=>S});import*as p from"./../../core/common/common.js";import*as a from"./../../core/platform/platform.js";function w(s,t,r){return(t?s[t-1]+1:0)+r}function P(s,t){let r=a.ArrayUtilities.upperBound(s,t-1,a.ArrayUtilities.DEFAULT_COMPARATOR),e;return r?e=t-s[r-1]-1:e=t,[r,e]}async function x(s,t,r,e=p.Settings.Settings.instance().moduleSetting("text-editor-indent").get()){return s.isDocumentOrScriptOrStyleSheet()?await S(t,r,e):{formattedContent:r,formattedMapping:new f}}async function S(s,t,r=p.Settings.Settings.instance().moduleSetting("text-editor-indent").get()){let e=t.replace(/\r\n?|[\n\u2028\u2029]/g,`
`).replace(/^\uFEFF/,""),n=h(),o={content:e,mapping:{original:[],formatted:[]}};try{o=await n.format(s,e,r)}catch{}let i=a.StringUtilities.findLineEndingIndexes(e),k=a.StringUtilities.findLineEndingIndexes(o.content),m=new d(i,k,o.mapping);return{formattedContent:o.content,formattedMapping:m}}var f=class{originalToFormatted(t,r=0){return[t,r]}formattedToOriginal(t,r=0){return[t,r]}},d=class{originalLineEndings;formattedLineEndings;mapping;constructor(t,r,e){this.originalLineEndings=t,this.formattedLineEndings=r,this.mapping=e}originalToFormatted(t,r){let e=w(this.originalLineEndings,t,r||0),n=this.convertPosition(this.mapping.original,this.mapping.formatted,e);return P(this.formattedLineEndings,n)}formattedToOriginal(t,r){let e=w(this.formattedLineEndings,t,r||0),n=this.convertPosition(this.mapping.formatted,this.mapping.original,e);return P(this.originalLineEndings,n)}convertPosition(t,r,e){let n=a.ArrayUtilities.upperBound(t,e,a.ArrayUtilities.DEFAULT_COMPARATOR)-1,o=r[n]+e-t[n];return n<r.length-1&&o>r[n+1]&&(o=r[n+1]),o}};export{C as FormatterWorkerPool,L as ScriptFormatter};
//# sourceMappingURL=formatter.js.map
