import{Mb as t,jb as e}from"./shared-0-C4kdVF7Y.js";import{dd as i,ld as s,ud as r}from"./shared-3-D-Ux4aGn.js";import{n,r as a,t as o}from"./tree-sitter-CihJMrpN.js";var l=class{constructor(t){this.maxSize=t,this.cache=new Map}get(t){const e=this.cache.get(t);return void 0!==e&&(this.cache.delete(t),this.cache.set(t,e)),e}set(t,e){if(this.cache.delete(t),this.cache.set(t,e),this.cache.size>this.maxSize){const t=this.cache.keys().next().value;void 0!==t&&this.cache.delete(t)}}clear(){this.cache.clear()}},h=new class{constructor(){this.initPromise=null,this.parser=null,this.languagePromises=new Map,this.queryPromises=new Map,this.initFailureLogged=!1,this.highlightCache=new l(100)}async initialize(){return this.initPromise||(this.initPromise=(async()=>{try{this.languagePromises.clear(),this.queryPromises.clear(),this.highlightCache.clear(),await n.init({locateFile:t=>"/tree-sitter/wasm/web-tree-sitter.wasm"});try{this.parser=new n}catch(i){t.error(e.USER_CONTENT_RENDERER,"[TreeSitter] Failed to create Parser instance:",i instanceof Error?i.message:String(i)),this.parser=null}}catch(s){t.error(e.USER_CONTENT_RENDERER,"[TreeSitter] Failed to initialize parser WASM:",s instanceof Error?s.message:String(s)),this.parser=null}})()),this.initPromise}async loadLanguage(s){if("undefined"==typeof window||!this.initPromise)return null;if(this.languagePromises.has(s))return this.languagePromises.get(s)??null;const r=i[s];if(!r)return this.languagePromises.set(s,Promise.resolve(null)),null;const n=(async()=>{try{const t=await fetch(r);if(!t.ok)throw new Error(`Failed to fetch WASM: ${t.status} ${t.statusText}`);const e=new Uint8Array(await t.arrayBuffer());return await o.load(e)}catch(i){return t.error(e.USER_CONTENT_RENDERER,`[TreeSitter] Failed to load WASM for ${s}:`,i instanceof Error?i.message:String(i)),null}})();return this.languagePromises.set(s,n),n}async loadQuery(i,s){if("undefined"==typeof window||!this.initPromise)return null;if(this.queryPromises.has(i))return this.queryPromises.get(i)??null;const n=r[i];if(!n)return this.queryPromises.set(i,Promise.resolve(null)),null;const o=(async()=>{try{let o="";if("typescript"===i||"tsx"===i){const t=r.javascript;if(t){const e=await fetch(t);e.ok&&(o=await e.text(),o+="\n\n")}}if("cpp"===i){const t=r.c;if(t){const e=await fetch(t);e.ok&&(o=await e.text(),o+="\n\n")}}const l=await fetch(n);return l.ok?(o+=await l.text(),new a(s,o)):(t.error(e.USER_CONTENT_RENDERER,`[TreeSitter] Failed to fetch query file for ${i}: ${l.status} ${l.statusText}`),null)}catch(o){return t.error(e.USER_CONTENT_RENDERER,`[TreeSitter] Query loading error for ${i}:`,o instanceof Error?o.message:String(o)),null}})();return this.queryPromises.set(i,o),o}async highlight(i,r){if("undefined"==typeof window)return null;if(this.initPromise?await this.initPromise:await this.initialize(),!this.parser)return this.initFailureLogged||(t.warn(e.USER_CONTENT_RENDERER,"[TreeSitter] Parser failed to initialize, falling back to plain text for all syntax highlighting"),this.initFailureLogged=!0),null;const n=s[r]||r,a=`${n}\0${i}`,o=this.highlightCache.get(a);if(o)return o;const l=await this.loadLanguage(n);if(!l)return t.warn(e.USER_CONTENT_RENDERER,"Failed to load language:",n,"- falling back to plain text"),this.plainTextTokens(i);this.parser.setLanguage(l);const h=this.parser.parse(i);if(!h)return t.warn(e.USER_CONTENT_RENDERER,"Failed to parse code - falling back to plain text"),this.plainTextTokens(i);const c=await this.loadQuery(n,l);if(!c)return t.warn(e.USER_CONTENT_RENDERER,"Failed to load query for:",n,"- falling back to plain text"),h.delete(),this.plainTextTokens(i);const u=this.queryToTokens(i,h,c);return h.delete(),this.highlightCache.set(a,u),u}queryToTokens(t,e,i){const s=[];let r=0,n=i.captures(e.rootNode);const a=new Set;n=n.filter(t=>{const e=t.node.id;return!a.has(e)&&(a.add(e),!0)}),n.sort((t,e)=>t.node.startIndex-e.node.startIndex);const o=e=>t.substring(0,e).split("\n").length-1;for(const l of n){const e=l.node,i=this.normalizeCaptureName(l.name);if(e.endIndex<=r)continue;if(e.startIndex>r){const i=t.substring(r,e.startIndex);this.addGapTokens(s,i,r,o),r=e.startIndex}else if(e.startIndex<r)continue;const n=e.text,a=e.startPosition.row;if(a!==e.endPosition.row&&n.includes("\n")){const t=n.split("\n");for(let e=0;e<t.length;e++)t[e]&&s.push({type:i,content:t[e],style:{},row:a+e})}else s.push({type:i,content:n,style:{},row:a});r=e.endIndex}if(r<t.length){const e=t.substring(r);this.addGapTokens(s,e,r,o)}return s}plainTextTokens(t){const e=[],i=t.split("\n");for(let s=0;s<i.length;s++)i[s]&&e.push({type:"text",content:i[s],style:{},row:s});return e}normalizeCaptureName(t){return t.replace(/\./g,"-")}addGapTokens(t,e,i,s){const r=s(i);if(r===s(i+e.length))e&&t.push({type:"text",content:e,style:{},row:r});else{const i=e.split("\n");for(let e=0;e<i.length;e++)i[e]&&t.push({type:"text",content:i[e],style:{},row:r+e})}}};export{h as treeSitterHighlighter};