import { getResolvedRef } from "@scalar/workspace-store/helpers/get-resolved-ref";
import { isObject } from "@scalar/helpers/object/is-object";
import { unpackProxyObject } from "@scalar/workspace-store/helpers/unpack-proxy";
//#region src/v2/blocks/operation-code-sample/helpers/get-resolved-ref-deep.ts
/**
* Recursively resolves all $ref objects in a data structure to their actual values.
* Traverses through objects, arrays, and nested structures to find and resolve
* any $ref references at any depth level.
*
* Handles circular references gracefully by detecting them and returning '[circular]'
* to prevent infinite loops.
*/
var getResolvedRefDeep = (node) => {
	const visited = /* @__PURE__ */ new WeakSet();
	const cachedResults = /* @__PURE__ */ new WeakMap();
	const resolveNode = (current) => {
		if (!isObject(current) && !Array.isArray(current)) return current;
		const rawValue = unpackProxyObject(current, { depth: 1 });
		if (cachedResults.has(rawValue)) return cachedResults.get(rawValue);
		if (visited.has(rawValue)) return "[circular]";
		visited.add(rawValue);
		if ("$ref" in current) {
			const result = resolveNode(getResolvedRef(current));
			cachedResults.set(rawValue, result);
			return result;
		}
		if (Array.isArray(current)) {
			const result = current.map(resolveNode);
			cachedResults.set(rawValue, result);
			return result;
		}
		const result = Object.fromEntries(Object.entries(current).map(([key, value]) => [key, resolveNode(value)]));
		cachedResults.set(rawValue, result);
		return result;
	};
	return resolveNode(node);
};
//#endregion
export { getResolvedRefDeep };

//# sourceMappingURL=get-resolved-ref-deep.js.map