// @bun
import{ec as F}from"./chunk-b0bkjk0p.js";import{Bc as N}from"./chunk-pdvve1gm.js";import{existsSync as H,readFileSync as P,unwatchFile as T}from"fs";var k;((J)=>{J[J.Error=1]="Error";J[J.Warning=2]="Warning";J[J.Information=3]="Information";J[J.Hint=4]="Hint"})(k||={});var L;((O)=>{O[O.File=1]="File";O[O.Module=2]="Module";O[O.Namespace=3]="Namespace";O[O.Package=4]="Package";O[O.Class=5]="Class";O[O.Method=6]="Method";O[O.Property=7]="Property";O[O.Field=8]="Field";O[O.Constructor=9]="Constructor";O[O.Enum=10]="Enum";O[O.Interface=11]="Interface";O[O.Function=12]="Function";O[O.Variable=13]="Variable";O[O.Constant=14]="Constant";O[O.String=15]="String";O[O.Number=16]="Number";O[O.Boolean=17]="Boolean";O[O.Array=18]="Array";O[O.Object=19]="Object";O[O.Key=20]="Key";O[O.Null=21]="Null";O[O.EnumMember=22]="EnumMember";O[O.Struct=23]="Struct";O[O.Event=24]="Event";O[O.Operator=25]="Operator";O[O.TypeParameter=26]="TypeParameter"})(L||={});var M;((Q)=>{Q[Q.Text=1]="Text";Q[Q.Method=2]="Method";Q[Q.Function=3]="Function";Q[Q.Constructor=4]="Constructor";Q[Q.Field=5]="Field";Q[Q.Variable=6]="Variable";Q[Q.Class=7]="Class";Q[Q.Interface=8]="Interface";Q[Q.Module=9]="Module";Q[Q.Property=10]="Property";Q[Q.Unit=11]="Unit";Q[Q.Value=12]="Value";Q[Q.Enum=13]="Enum";Q[Q.Keyword=14]="Keyword";Q[Q.Snippet=15]="Snippet";Q[Q.Color=16]="Color";Q[Q.File=17]="File";Q[Q.Reference=18]="Reference";Q[Q.Folder=19]="Folder";Q[Q.EnumMember=20]="EnumMember";Q[Q.Constant=21]="Constant";Q[Q.Struct=22]="Struct";Q[Q.Event=23]="Event";Q[Q.Operator=24]="Operator";Q[Q.TypeParameter=25]="TypeParameter"})(M||={});var v;((A)=>{A[A.Text=1]="Text";A[A.Read=2]="Read";A[A.Write=3]="Write"})(v||={});class E{documents=new Map;rootUri=null;config={};messageId=0;watchers=new Map;initialize(j){return this.rootUri=j.rootUri,{capabilities:{textDocumentSync:1,hoverProvider:!0,completionProvider:{triggerCharacters:[".",":","<",'"',"'","/"],resolveProvider:!0},definitionProvider:!0,referencesProvider:!0,renameProvider:{prepareProvider:!0},documentSymbolProvider:!0,workspaceSymbolProvider:!0,codeActionProvider:{codeActionKinds:["quickfix","refactor","source"]},signatureHelpProvider:{triggerCharacters:["(",","],retriggerCharacters:[","]},documentHighlightProvider:!0,documentFormattingProvider:!0,diagnosticProvider:{interFileDependencies:!0,workspaceDiagnostics:!0}}}}didOpen(j){let{uri:z,text:G,version:A}=j.textDocument;this.updateDocument(z,G,A)}didChange(j){let{uri:z,version:G}=j.textDocument,A=j.contentChanges[0]?.text||"";this.updateDocument(z,A,G)}didClose(j){let z=j.textDocument.uri;if(this.documents.delete(z),this.watchers.get(z))T(this.uriToPath(z)),this.watchers.delete(z)}updateDocument(j,z,G){let A=this.uriToPath(j),J=F(z,A,!0);this.documents.set(j,{content:z,version:G,declarations:J})}getDiagnostics(j){let z=this.documents.get(j);if(!z)return[];let G=[];for(let A of z.declarations){if(A.isExported&&A.kind==="variable"&&!A.typeAnnotation)G.push({range:this.getDeclarationRange(A,z.content),severity:2,code:"missing-type",source:"dtsx",message:`Exported variable '${A.name}' should have an explicit type annotation for better declaration generation`});if(A.typeAnnotation==="any"||A.returnType==="any")G.push({range:this.getDeclarationRange(A,z.content),severity:3,code:"any-type",source:"dtsx",message:"Consider using a more specific type instead of 'any'"});if(A.kind==="function"&&A.isExported&&!A.returnType)G.push({range:this.getDeclarationRange(A,z.content),severity:3,code:"missing-return-type",source:"dtsx",message:`Function '${A.name}' should have an explicit return type for better declaration generation`})}return G}hover(j){let z=this.documents.get(j.textDocument.uri);if(!z)return null;let{line:G,character:A}=j.position,R=z.content.split(`
`)[G]||"",U=R.slice(0,A).match(/[\w$]+$/),V=R.slice(A).match(/^[\w$]+/);if(!U&&!V)return null;let X=(U?.[0]||"")+(V?.[0]||""),Y=z.declarations.find((W)=>W.name===X);if(!Y)return null;let B=`\`\`\`typescript
${this.buildSignature(Y)}
\`\`\``;if(Y.leadingComments&&Y.leadingComments.length>0){let q=Y.leadingComments.join(`
`).match(/\*\s*([^@*][^*]*)/);if(q)B+=`

${q[1].trim()}`}return{contents:{kind:"markdown",value:B}}}completion(j){let z=this.documents.get(j.textDocument.uri);if(!z)return[];let G=[];for(let A of z.declarations){if(!A.isExported)continue;let J=6;switch(A.kind){case"function":J=3;break;case"class":J=7;break;case"interface":J=8;break;case"type":J=8;break;case"enum":J=13;break}G.push({label:A.name,kind:J,detail:this.buildSignature(A)})}return G}definition(j){let z=this.documents.get(j.textDocument.uri);if(!z)return null;let{line:G,character:A}=j.position,R=z.content.split(`
`)[G]||"",U=R.slice(0,A).match(/[\w$]+$/),V=R.slice(A).match(/^[\w$]+/);if(!U&&!V)return null;let X=(U?.[0]||"")+(V?.[0]||""),Y=z.declarations.find(($)=>$.name===X);if(!Y)return null;return{uri:j.textDocument.uri,range:this.getDeclarationRange(Y,z.content)}}references(j){let z=[],G=this.getWordAtPosition(j.textDocument.uri,j.position);if(!G)return z;for(let[A,J]of this.documents){let R=J.content.split(`
`);for(let U=0;U<R.length;U++){let V=R[U],X=0;while(X<V.length){let Y=V.indexOf(G,X);if(Y===-1)break;let $=Y>0?V[Y-1]:" ",B=Y+G.length<V.length?V[Y+G.length]:" ";if(!/[\w$]/.test($)&&!/[\w$]/.test(B))z.push({uri:A,range:{start:{line:U,character:Y},end:{line:U,character:Y+G.length}}});X=Y+1}}}return z}prepareRename(j){let z=this.getWordAtPosition(j.textDocument.uri,j.position);if(!z)return null;let G=this.documents.get(j.textDocument.uri);if(!G)return null;let A=G.declarations.find((R)=>R.name===z);if(!A)return null;return{range:this.getDeclarationRange(A,G.content),placeholder:z}}rename(j){if(!this.getWordAtPosition(j.textDocument.uri,j.position))return null;let G=this.references({textDocument:j.textDocument,position:j.position,context:{includeDeclaration:!0}});if(G.length===0)return null;let A={};for(let J of G){if(!A[J.uri])A[J.uri]=[];A[J.uri].push({range:J.range,newText:j.newName})}return{changes:A}}documentSymbols(j){let z=this.documents.get(j.textDocument.uri);if(!z)return[];let G=[];for(let A of z.declarations){let J=this.getDeclarationRange(A,z.content),R=this.declarationKindToSymbolKind(A.kind),U={name:A.name,detail:this.buildSignature(A),kind:R,range:J,selectionRange:J};if(A.members&&A.members.length>0)U.children=A.members.map((V)=>({name:V.name,kind:this.declarationKindToSymbolKind(V.kind),range:this.getDeclarationRange(V,z.content),selectionRange:this.getDeclarationRange(V,z.content)}));G.push(U)}return G}workspaceSymbols(j){let z=[],G=j.query.toLowerCase();for(let[A,J]of this.documents)for(let R of J.declarations){if(!R.isExported)continue;if(G&&!R.name.toLowerCase().includes(G))continue;z.push({name:R.name,kind:this.declarationKindToSymbolKind(R.kind),location:{uri:A,range:this.getDeclarationRange(R,J.content)}})}return z}codeActions(j){let z=[];if(!this.documents.get(j.textDocument.uri))return z;for(let J of j.context.diagnostics){if(J.code==="missing-type"){let R=this.getWordAtRange(j.textDocument.uri,J.range);if(R)z.push({title:`Add type annotation to '${R}'`,kind:"quickfix",diagnostics:[J],edit:{changes:{[j.textDocument.uri]:[{range:{start:J.range.end,end:J.range.end},newText:": unknown"}]}}})}if(J.code==="any-type")z.push({title:"Replace 'any' with 'unknown'",kind:"quickfix",diagnostics:[J],isPreferred:!0,edit:{changes:{[j.textDocument.uri]:[{range:J.range,newText:"unknown"}]}}});if(J.code==="missing-return-type"){let R=this.getWordAtRange(j.textDocument.uri,J.range);if(R)z.push({title:`Add return type to '${R}'`,kind:"quickfix",diagnostics:[J]})}}let A=this.getDeclarationAtPosition(j.textDocument.uri,j.range.start);if(A){if(A.kind==="function")z.push({title:"Extract function signature to type",kind:"refactor.extract"});if(A.kind==="interface"||A.kind==="type")z.push({title:"Generate runtime validator",kind:"source"})}return z}signatureHelp(j){let z=this.documents.get(j.textDocument.uri);if(!z)return null;let{line:G,character:A}=j.position,R=z.content.split(`
`)[G]||"",U=0,V=-1,X=0;for(let Z=A-1;Z>=0;Z--){let _=R[Z];if(_===")")U++;else if(_==="("){if(U===0){V=Z;break}U--}else if(_===","&&U===0)X++}if(V===-1)return null;let $=R.slice(0,V).trimEnd().match(/[\w$]+$/);if(!$)return null;let B=$[0],W=z.declarations.find((Z)=>Z.name===B&&Z.kind==="function");if(!W||!W.parameters)return null;let q=W.parameters.map((Z)=>{let _="";if(Z.rest)_+="...";if(_+=Z.name,Z.optional)_+="?";if(Z.type)_+=`: ${Z.type}`;return _});return{signatures:[{label:`${B}(${q.join(", ")}): ${W.returnType||"void"}`,parameters:W.parameters.map((Z)=>({label:Z.name,documentation:Z.type?`Type: ${Z.type}`:void 0}))}],activeSignature:0,activeParameter:Math.min(X,W.parameters.length-1)}}documentHighlight(j){let z=[],G=this.getWordAtPosition(j.textDocument.uri,j.position);if(!G)return z;let A=this.documents.get(j.textDocument.uri);if(!A)return z;let J=A.content.split(`
`);for(let R=0;R<J.length;R++){let U=J[R],V=0;while(V<U.length){let X=U.indexOf(G,V);if(X===-1)break;let Y=X>0?U[X-1]:" ",$=X+G.length<U.length?U[X+G.length]:" ";if(!/[\w$]/.test(Y)&&!/[\w$]/.test($)){let B=/^\s*[=:]/.test(U.slice(X+G.length))||/^(const|let|var|function|class|interface|type|enum)\s+$/.test(U.slice(0,X));z.push({range:{start:{line:R,character:X},end:{line:R,character:X+G.length}},kind:B?3:2})}V=X+1}}return z}async formatting(j){let z=this.documents.get(j.textDocument.uri);if(!z)return[];let G=this.generateDts(j.textDocument.uri);if(!G)return[];let A=z.content.split(`
`);return[{range:{start:{line:0,character:0},end:{line:A.length-1,character:A[A.length-1].length}},newText:G}]}getWordAtPosition(j,z){let G=this.documents.get(j);if(!G)return null;let J=G.content.split(`
`)[z.line]||"",R=J.slice(0,z.character).match(/[\w$]+$/),U=J.slice(z.character).match(/^[\w$]+/);if(!R&&!U)return null;return(R?.[0]||"")+(U?.[0]||"")}getWordAtRange(j,z){let G=this.documents.get(j);if(!G)return null;let A=G.content.split(`
`);if(z.start.line===z.end.line)return A[z.start.line]?.slice(z.start.character,z.end.character)||null;return null}getDeclarationAtPosition(j,z){let G=this.documents.get(j);if(!G)return null;for(let A of G.declarations){let J=this.getDeclarationRange(A,G.content);if(this.positionInRange(z,J))return A}return null}positionInRange(j,z){if(j.line<z.start.line||j.line>z.end.line)return!1;if(j.line===z.start.line&&j.character<z.start.character)return!1;if(j.line===z.end.line&&j.character>z.end.character)return!1;return!0}declarationKindToSymbolKind(j){switch(j){case"function":return 12;case"class":return 5;case"interface":return 11;case"type":return 11;case"enum":return 10;case"variable":return 13;case"const":return 14;case"property":return 7;case"method":return 6;case"module":return 2;case"namespace":return 3;default:return 13}}generateDts(j){let z=this.documents.get(j);if(!z)return null;let G={filePath:this.uriToPath(j),sourceCode:z.content,declarations:z.declarations};return N(z.declarations,G,!0,this.config.importOrder)}buildSignature(j){switch(j.kind){case"function":{let z=j.parameters?.map((J)=>{let R="";if(J.rest)R+="...";if(R+=J.name,J.optional)R+="?";if(J.type)R+=`: ${J.type}`;return R}).join(", ")||"",G=j.generics||"",A=j.returnType?`: ${j.returnType}`:"";return`function ${j.name}${G}(${z})${A}`}case"variable":return`const ${j.name}: ${j.typeAnnotation||"unknown"}`;case"interface":return`interface ${j.name}${j.generics||""}${j.extends?` extends ${j.extends}`:""}`;case"type":return`type ${j.name}${j.generics||""} = ${j.typeAnnotation||"unknown"}`;case"class":return`class ${j.name}${j.generics||""}${j.extends?` extends ${j.extends}`:""}`;case"enum":return`enum ${j.name}`;default:return j.name}}getDeclarationRange(j,z){if(j.start!==void 0&&j.end!==void 0)return{start:this.offsetToPosition(j.start,z),end:this.offsetToPosition(j.end,z)};let G=z.split(`
`);for(let A=0;A<G.length;A++){let J=G[A].indexOf(j.name);if(J!==-1)return{start:{line:A,character:J},end:{line:A,character:J+j.name.length}}}return{start:{line:0,character:0},end:{line:0,character:0}}}offsetToPosition(j,z){let G=0,A=0;for(let J=0;J<j&&J<z.length;J++)if(z[J]===`
`)G++,A=0;else A++;return{line:G,character:A}}uriToPath(j){if(j.startsWith("file://"))return decodeURIComponent(j.slice(7));return j}pathToUri(j){return`file://${encodeURIComponent(j).replace(/%2F/g,"/")}`}handleMessage(j){if(j.method==="initialize")return{jsonrpc:"2.0",id:j.id,result:this.initialize(j.params)};if(j.method==="initialized")return null;if(j.method==="shutdown")return{jsonrpc:"2.0",id:j.id,result:null};if(j.method==="exit")process.exit(0);let z=j.params;if(j.method==="textDocument/didOpen")return this.didOpen(z),null;if(j.method==="textDocument/didChange")return this.didChange(z),null;if(j.method==="textDocument/didClose")return this.didClose(z),null;if(j.method==="textDocument/hover")return{jsonrpc:"2.0",id:j.id,result:this.hover(z)};if(j.method==="textDocument/completion")return{jsonrpc:"2.0",id:j.id,result:this.completion(z)};if(j.method==="textDocument/definition")return{jsonrpc:"2.0",id:j.id,result:this.definition(z)};if(j.method==="textDocument/references")return{jsonrpc:"2.0",id:j.id,result:this.references(z)};if(j.method==="textDocument/prepareRename")return{jsonrpc:"2.0",id:j.id,result:this.prepareRename(z)};if(j.method==="textDocument/rename")return{jsonrpc:"2.0",id:j.id,result:this.rename(z)};if(j.method==="textDocument/documentSymbol")return{jsonrpc:"2.0",id:j.id,result:this.documentSymbols(z)};if(j.method==="workspace/symbol")return{jsonrpc:"2.0",id:j.id,result:this.workspaceSymbols(z)};if(j.method==="textDocument/codeAction")return{jsonrpc:"2.0",id:j.id,result:this.codeActions(z)};if(j.method==="textDocument/signatureHelp")return{jsonrpc:"2.0",id:j.id,result:this.signatureHelp(z)};if(j.method==="textDocument/documentHighlight")return{jsonrpc:"2.0",id:j.id,result:this.documentHighlight(z)};if(j.method==="textDocument/formatting")return{jsonrpc:"2.0",id:j.id,result:this.formatting(z)};if(j.method==="textDocument/diagnostic"){let G=z?.textDocument?.uri;return{jsonrpc:"2.0",id:j.id,result:{kind:"full",items:G?this.getDiagnostics(G):[]}}}if(j.id!==void 0)return{jsonrpc:"2.0",id:j.id,error:{code:-32601,message:`Method not found: ${j.method}`}};return null}}function C(){let j=new E,z="";process.stdin.setEncoding("utf8"),process.stdin.on("data",(G)=>{z+=G;while(!0){let A=z.indexOf(`\r
\r
`);if(A===-1)break;let R=z.slice(0,A).match(/Content-Length:\s*(\d+)/i);if(!R){z=z.slice(A+4);continue}let U=Number.parseInt(R[1],10),V=A+4,X=V+U;if(z.length<X)break;let Y=z.slice(V,X);z=z.slice(X);try{let $=JSON.parse(Y),B=j.handleMessage($);if(B){let W=JSON.stringify(B),q=Buffer.byteLength(W,"utf8");process.stdout.write(`Content-Length: ${q}\r
\r
${W}`)}}catch($){console.error("Error parsing LSP message:",$)}}})}function f(j,z){return(G)=>{if(!H(G))return;let A=P(G,"utf-8"),J=`file://${G}`;j.didOpen({textDocument:{uri:J,languageId:"typescript",version:1,text:A}});let R=j.getDiagnostics(J);z(J,R)}}
export{k as s,L as t,M as u,v,E as w,C as x,f as y};
