import { stream } from "../utils/stream.js";
import { getDocument } from "../utils/ast-utils.js";
import { MultiMapScope, StreamScope } from "./scope.js";
import { WorkspaceCache } from "../utils/caching.js";
var DefaultScopeProvider = class {
	constructor(e) {
		this.reflection = e.shared.AstReflection, this.nameProvider = e.references.NameProvider, this.descriptions = e.workspace.AstNodeDescriptionProvider, this.indexManager = e.shared.workspace.IndexManager, this.globalScopeCache = new WorkspaceCache(e.shared);
	}
	getScope(e) {
		let n = [], r = this.reflection.getReferenceType(e), i = getDocument(e.container).localSymbols;
		if (i) {
			let t = e.container;
			do
				i.has(t) && n.push(i.getStream(t).filter((e) => this.reflection.isSubtype(e.type, r))), t = t.$container;
			while (t);
		}
		let a = this.getGlobalScope(r, e);
		for (let e = n.length - 1; e >= 0; e--) a = this.createScope(n[e], a);
		return a;
	}
	createScope(t, n, i) {
		return new StreamScope(stream(t), n, i);
	}
	createScopeForNodes(t, n, i) {
		return new StreamScope(stream(t).map((e) => {
			let t = this.nameProvider.getName(e);
			if (t) return this.descriptions.createDescription(e, t);
		}).nonNullable(), n, i);
	}
	getGlobalScope(e, t) {
		return this.globalScopeCache.get(e, () => new MultiMapScope(this.indexManager.allElements(e)));
	}
};
export { DefaultScopeProvider };
