import { getDocument } from "../utils/ast-utils.js";
import { isJSDoc, parseJSDoc } from "./jsdoc.js";
var JSDocDocumentationProvider = class {
	constructor(e) {
		this.indexManager = e.shared.workspace.IndexManager, this.commentProvider = e.documentation.CommentProvider;
	}
	getDocumentation(e) {
		let r = this.commentProvider.getComment(e);
		if (r && isJSDoc(r)) return parseJSDoc(r).toMarkdown({
			renderLink: (t, n) => this.documentationLinkRenderer(e, t, n),
			renderTag: (t) => this.documentationTagRenderer(e, t)
		});
	}
	documentationLinkRenderer(e, t, n) {
		let r = this.findNameInLocalSymbols(e, t) ?? this.findNameInGlobalScope(e, t);
		if (r && r.nameSegment) {
			let e = r.nameSegment.range.start.line + 1, t = r.nameSegment.range.start.character + 1;
			return `[${n}](${r.documentUri.with({ fragment: `L${e},${t}` }).toString()})`;
		} else return;
	}
	documentationTagRenderer(e, t) {}
	findNameInLocalSymbols(t, n) {
		let r = getDocument(t).localSymbols;
		if (!r) return;
		let i = t;
		do {
			let e = r.getStream(i).find((e) => e.name === n);
			if (e) return e;
			i = i.$container;
		} while (i);
	}
	findNameInGlobalScope(e, t) {
		return this.indexManager.allElements().find((e) => e.name === t);
	}
};
export { JSDocDocumentationProvider };
