import { isAstNode as e, isMultiReference as t, isReference as n } from "../syntax-tree.js";
import { DONE_RESULT as r, StreamImpl as i, TreeStreamImpl as a } from "./stream.js";
import { inRange as o } from "./cst-utils.js";
//#region ../../node_modules/langium/lib/utils/ast-utils.js
function s(t, n = {}) {
	for (let [r, i] of Object.entries(t)) r.startsWith("$") || (Array.isArray(i) ? i.forEach((i, a) => {
		e(i) && (i.$container = t, i.$containerProperty = r, i.$containerIndex = a, n.deep && s(i, n));
	}) : e(i) && (i.$container = t, i.$containerProperty = r, n.deep && s(i, n)));
}
function c(e, t) {
	let n = e;
	for (; n;) {
		if (t(n)) return n;
		n = n.$container;
	}
}
function l(e) {
	let t = u(e).$document;
	if (!t) throw Error("AST node has no document.");
	return t;
}
function u(e) {
	for (; e.$container;) e = e.$container;
	return e;
}
function d(e) {
	return n(e) ? e.ref ? [e.ref] : [] : t(e) ? e.items.map((e) => e.ref) : [];
}
function f(t, n) {
	if (!t) throw Error("Node must be an AstNode.");
	let a = n?.range;
	return new i(() => ({
		keys: Object.keys(t),
		keyIndex: 0,
		arrayIndex: 0
	}), (n) => {
		for (; n.keyIndex < n.keys.length;) {
			let r = n.keys[n.keyIndex];
			if (!r.startsWith("$")) {
				let i = t[r];
				if (e(i)) {
					if (n.keyIndex++, h(i, a)) return {
						done: !1,
						value: i
					};
				} else if (Array.isArray(i)) {
					for (; n.arrayIndex < i.length;) {
						let t = i[n.arrayIndex++];
						if (e(t) && h(t, a)) return {
							done: !1,
							value: t
						};
					}
					n.arrayIndex = 0;
				}
			}
			n.keyIndex++;
		}
		return r;
	});
}
function p(e, t) {
	if (!e) throw Error("Root node must be an AstNode.");
	return new a(e, (e) => f(e, t));
}
function m(e, t) {
	if (!e) throw Error("Root node must be an AstNode.");
	return t?.range && !h(e, t.range) ? new a(e, () => []) : new a(e, (e) => f(e, t), { includeRoot: !0 });
}
function h(e, t) {
	if (!t) return !0;
	let n = e.$cstNode?.range;
	return n ? o(n, t) : !1;
}
function g(e) {
	return new i(() => ({
		keys: Object.keys(e),
		keyIndex: 0,
		arrayIndex: 0
	}), (i) => {
		for (; i.keyIndex < i.keys.length;) {
			let r = i.keys[i.keyIndex];
			if (!r.startsWith("$")) {
				let a = e[r];
				if (n(a) || t(a)) return i.keyIndex++, {
					done: !1,
					value: {
						reference: a,
						container: e,
						property: r
					}
				};
				if (Array.isArray(a)) {
					for (; i.arrayIndex < a.length;) {
						let o = i.arrayIndex++, s = a[o];
						if (n(s) || t(a)) return {
							done: !1,
							value: {
								reference: s,
								container: e,
								property: r,
								index: o
							}
						};
					}
					i.arrayIndex = 0;
				}
			}
			i.keyIndex++;
		}
		return r;
	});
}
function _(e, t) {
	let n = e.getTypeMetaData(t.$type), r = t;
	for (let e of Object.values(n.properties)) e.defaultValue !== void 0 && r[e.name] === void 0 && (r[e.name] = v(e.defaultValue));
}
function v(e) {
	return Array.isArray(e) ? [...e.map(v)] : e;
}
//#endregion
export { _ as assignMandatoryProperties, u as findRootNode, c as getContainerOfType, l as getDocument, d as getReferenceNodes, s as linkContentToContainer, p as streamAllContents, m as streamAst, f as streamContents, g as streamReferences };
