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

//# sourceMappingURL=languageDetectionSimpleWorker.js.map
