/*!--------------------------------------------------------
 * Copyright (C) Microsoft Corporation. All rights reserved.
 *--------------------------------------------------------*/(function(){var g=["vs/workbench/services/languageDetection/browser/languageDetectionSimpleWorker","require","exports","vs/base/common/stopwatch","vs/editor/common/services/editorSimpleWorker"],v=function(u){for(var a=[],d=0,h=u.length;d<h;d++)a[d]=g[u[d]];return a};define(g[0],v([1,2,3,4]),function(u,a,d,h){"use strict";Object.defineProperty(a,"__esModule",{value:!0}),a.LanguageDetectionSimpleWorker=a.create=void 0;function m(x){return new r(x,null)}a.create=m;class r extends h.EditorSimpleWorker{constructor(){super(...arguments);this._regexpLoadFailed=!1,this._loadFailed=!1}async detectLanguage(t,n,e){const i=[],o=[],s=new d.StopWatch(!0),f=this.getTextForDetection(t);if(!f)return;const p=async()=>{for await(const c of this.detectLanguagesImpl(f))i.push(c.languageId),o.push(c.confidence);if(s.stop(),i.length)return this._host.fhr("sendTelemetryEvent",[i,o,s.elapsed()]),i[0]},w=async()=>{if(n){const c=await this.runRegexpModel(f,n);if(c)return c}};if(e){const c=await w();if(c)return c;const l=await p();if(l)return l}else{const c=await p();if(c)return c;const l=await w();if(l)return l}}getTextForDetection(t){const n=this._getModel(t);if(!n)return;const e=n.positionAt(1e4);return n.getValueInRange({startColumn:1,startLineNumber:1,endColumn:e.column,endLineNumber:e.lineNumber})}async getRegexpModel(){if(this._regexpLoadFailed)return;if(this._regexpModel)return this._regexpModel;const t=await this._host.fhr("getRegexpModelUri",[]);try{return this._regexpModel=await new Promise((n,e)=>{u([t],n,e)}),this._regexpModel}catch{this._regexpLoadFailed=!0;return}}async runRegexpModel(t,n){const e=await this.getRegexpModel();return e?e.detect(t,n):void 0}async getModelOperations(){if(this._modelOperations)return this._modelOperations;const t=await this._host.fhr("getIndexJsUri",[]),{ModelOperations:n}=await new Promise((e,i)=>{u([t],e,i)});return this._modelOperations=new n({modelJsonLoaderFunc:async()=>{const e=await fetch(await this._host.fhr("getModelJsonUri",[]));try{return await e.json()}catch{const o="Failed to parse model JSON.";throw new Error(o)}},weightsLoaderFunc:async()=>await(await fetch(await this._host.fhr("getWeightsUri",[]))).arrayBuffer()}),this._modelOperations}adjustLanguageConfidence(t){switch(t.languageId){case"javascript":case"html":case"json":case"typescript":case"css":case"python":case"xml":case"php":t.confidence+=r.positiveConfidenceCorrectionBucket1;break;case"cpp":case"shellscript":case"java":case"csharp":case"c":t.confidence+=r.positiveConfidenceCorrectionBucket2;break;case"bat":case"ini":case"makefile":case"sql":case"csv":case"toml":t.confidence-=r.negativeConfidenceCorrection;break;default:break}return t}async*detectLanguagesImpl(t){if(this._loadFailed)return;let n;try{n=await this.getModelOperations()}catch(s){console.log(s),this._loadFailed=!0;return}let e;try{e=await n.runModel(t)}catch(s){console.warn(s)}if(!e||e.length===0||e[0].confidence<r.expectedRelativeConfidence)return;const i=this.adjustLanguageConfidence(e[0]);if(i.confidence<r.expectedRelativeConfidence)return;const o=[i];for(let s of e){if(s===i)continue;if(s=this.adjustLanguageConfidence(s),o[o.length-1].confidence-s.confidence>=r.expectedRelativeConfidence){for(;o.length;)yield o.shift();if(s.confidence>r.expectedRelativeConfidence){o.push(s);continue}return}else{if(s.confidence>r.expectedRelativeConfidence){o.push(s);continue}return}}}}a.LanguageDetectionSimpleWorker=r,r.expectedRelativeConfidence=.2,r.positiveConfidenceCorrectionBucket1=.05,r.positiveConfidenceCorrectionBucket2=.025,r.negativeConfidenceCorrection=.5})}).call(this);

//# sourceMappingURL=languageDetectionSimpleWorker.js.map
