import { isMultiReference as e, isReference as t } from "../syntax-tree.js";
import { stream as n } from "../utils/stream.js";
import { getDocument as r, getReferenceNodes as i, streamAst as a, streamReferences as o } from "../utils/ast-utils.js";
import { isCrossReference as s } from "../languages/generated/ast.js";
import { isChildNode as c, toDocumentSegment as l } from "../utils/cst-utils.js";
import { findAssignment as u } from "../utils/grammar-utils.js";
import { UriUtils as d } from "../utils/uri-utils.js";
//#region ../../node_modules/langium/lib/references/references.js
var f = class {
	constructor(e) {
		this.nameProvider = e.references.NameProvider, this.index = e.shared.workspace.IndexManager, this.nodeLocator = e.workspace.AstNodeLocator, this.documents = e.shared.workspace.LangiumDocuments, this.hasMultiReference = a(e.Grammar).some((e) => s(e) && e.isMulti);
	}
	findDeclarations(n) {
		if (n) {
			let r = u(n), a = n.astNode;
			if (r && a) {
				let o = a[r.feature];
				if (t(o) || e(o)) return i(o);
				if (Array.isArray(o)) {
					for (let r of o) if ((t(r) || e(r)) && r.$refNode && r.$refNode.offset <= n.offset && r.$refNode.end >= n.end) return i(r);
				}
			}
			if (a) {
				let e = this.nameProvider.getNameNode(a);
				if (e && (e === n || c(n, e))) return this.getSelfNodes(a);
			}
		}
		return [];
	}
	getSelfNodes(t) {
		if (this.hasMultiReference) {
			let n = this.index.findAllReferences(t, this.nodeLocator.getAstNodePath(t)), r = this.getNodeFromReferenceDescription(n.head());
			if (r) {
				for (let n of o(r)) if (e(n.reference) && n.reference.items.some((e) => e.ref === t)) return n.reference.items.map((e) => e.ref);
			}
			return [t];
		} else return [t];
	}
	getNodeFromReferenceDescription(e) {
		if (!e) return;
		let t = this.documents.getDocument(e.sourceUri);
		if (t) return this.nodeLocator.getAstNode(t.parseResult.value, e.sourcePath);
	}
	findDeclarationNodes(e) {
		let t = this.findDeclarations(e), n = [];
		for (let e of t) {
			let t = this.nameProvider.getNameNode(e) ?? e.$cstNode;
			t && n.push(t);
		}
		return n;
	}
	findReferences(e, t) {
		let r = [];
		t.includeDeclaration && r.push(...this.getSelfReferences(e));
		let i = this.index.findAllReferences(e, this.nodeLocator.getAstNodePath(e));
		return t.documentUri && (i = i.filter((e) => d.equals(e.sourceUri, t.documentUri))), r.push(...i), n(r);
	}
	getSelfReferences(e) {
		let t = this.getSelfNodes(e), n = [];
		for (let e of t) {
			let t = this.nameProvider.getNameNode(e);
			if (t) {
				let i = r(e), a = this.nodeLocator.getAstNodePath(e);
				n.push({
					sourceUri: i.uri,
					sourcePath: a,
					targetUri: i.uri,
					targetPath: a,
					segment: l(t),
					local: !0
				});
			}
		}
		return n;
	}
};
//#endregion
export { f as DefaultReferences };
