import*as Common from"../common/common.js";const MAX_WORKERS=Math.min(2,navigator.hardwareConcurrency-1);let formatterWorkerPoolInstance;export class FormatterWorkerPool{constructor(){this._taskQueue=[],this._workerTasks=new Map}static instance(){return formatterWorkerPoolInstance||(formatterWorkerPoolInstance=new FormatterWorkerPool),formatterWorkerPoolInstance}_createWorker(){const t=Common.Worker.WorkerWrapper.fromURL(new URL("../formatter_worker/formatter_worker-entrypoint.js",import.meta.url));return t.onmessage=this._onWorkerMessage.bind(this,t),t.onerror=this._onWorkerError.bind(this,t),t}_processNextTask(){if(!this._taskQueue.length)return;let t=[...this._workerTasks.keys()].find((t=>!this._workerTasks.get(t)));if(!t&&this._workerTasks.size<MAX_WORKERS&&(t=this._createWorker()),!t)return;const e=this._taskQueue.shift();e&&(this._workerTasks.set(t,e),t.postMessage({method:e.method,params:e.params}))}_onWorkerMessage(t,e){const r=this._workerTasks.get(t);r&&(r.isChunked&&e.data&&!e.data.isLastChunk?r.callback(e.data):(this._workerTasks.set(t,null),this._processNextTask(),r.callback(e.data?e.data:null)))}_onWorkerError(t,e){console.error(e);const r=this._workerTasks.get(t);t.terminate(),this._workerTasks.delete(t);const s=this._createWorker();this._workerTasks.set(s,null),this._processNextTask(),r&&r.callback(null)}_runChunkedTask(t,e,r){const s=new Task(t,e,(function(t){if(!t)return void r(!0,null);const e="isLastChunk"in t&&Boolean(t.isLastChunk),s="chunk"in t&&t.chunk;r(e,s)}),!0);this._taskQueue.push(s),this._processNextTask()}_runTask(t,e){return new Promise((r=>{const s=new Task(t,e,r,!1);this._taskQueue.push(s),this._processNextTask()}))}format(t,e,r){const s={mimeType:t,content:e,indentString:r};return this._runTask("format",s)}javaScriptIdentifiers(t){return this._runTask("javaScriptIdentifiers",{content:t}).then((t=>t||[]))}evaluatableJavaScriptSubstring(t){return this._runTask("evaluatableJavaScriptSubstring",{content:t}).then((t=>t||""))}parseCSS(t,e){this._runChunkedTask("parseCSS",{content:t},(function(t,r){e(t,r||[])}))}javaScriptOutline(t,e){this._runChunkedTask("javaScriptOutline",{content:t},(function(t,r){e(t,r||[])}))}outlineForMimetype(t,e,r){switch(e){case"text/html":case"text/javascript":return this.javaScriptOutline(t,(function(t,e){r(t,e.map((t=>({line:t.line,column:t.column,title:t.name,subtitle:t.arguments}))))})),!0;case"text/css":return this.parseCSS(t,(function(t,e){r(t,e.map((t=>{const e="selectorText"in t?t.selectorText:t.atRule;return{line:t.lineNumber,subtitle:void 0,column:t.columnNumber,title:e}})))})),!0}return!1}findLastExpression(t){return this._runTask("findLastExpression",{content:t})}findLastFunctionCall(t){return this._runTask("findLastFunctionCall",{content:t})}argumentsList(t){return this._runTask("argumentsList",{content:t})}}class Task{constructor(t,e,r,s){this.method=t,this.params=e,this.callback=r,this.isChunked=s}}export class FormatResult{constructor(){this.content,this.mapping}}class JSOutlineItem{constructor(){this.name,this.arguments,this.line,this.column}}class CSSProperty{constructor(){this.name,this.nameRange,this.value,this.valueRange,this.range,this.disabled}}class SCSSProperty{constructor(){this.range,this.name,this.value,this.disabled}}class SCSSRule{constructor(){this.selectors,this.properties,this.styleRange}}export function formatterWorkerPool(){return FormatterWorkerPool.instance()}export let OutlineItem;export let FormatMapping;export let CSSStyleRule;export let CSSAtRule;export let CSSRule;export let TextRange;