//#region src/presentation/server/CodemationTsyringeParamInfoReader.ts
var CodemationTsyringeParamInfoReader = class {
	static injectionTokenMetadataKey = "injectionTokens";
	static designParamTypesMetadataKey = "design:paramtypes";
	static read(target) {
		const designParamTypes = this.readDesignParamTypes(target);
		const injectionTokens = this.readInjectionTokens(target);
		Object.keys(injectionTokens).forEach((key) => {
			designParamTypes[Number(key)] = injectionTokens[key];
		});
		return designParamTypes;
	}
	static readDesignParamTypes(target) {
		const reflected = Reflect.getMetadata?.(this.designParamTypesMetadataKey, target);
		return Array.isArray(reflected) ? [...reflected] : [];
	}
	static readInjectionTokens(target) {
		const reflected = Reflect.getOwnMetadata?.(this.injectionTokenMetadataKey, target);
		if (!reflected || typeof reflected !== "object") return {};
		return reflected;
	}
};

//#endregion
//#region src/presentation/server/CodemationTsyringeTypeInfoRegistrar.ts
var CodemationTsyringeTypeInfoRegistrar = class {
	visitedTokens = /* @__PURE__ */ new Set();
	visitedConfigObjects = /* @__PURE__ */ new Set();
	constructor(container) {
		this.container = container;
	}
	registerWorkflowDefinitions(workflows) {
		for (const workflow of workflows) for (const node of workflow.nodes) {
			this.registerTypeToken(node.type);
			this.registerConfigTokens(node.config);
		}
	}
	registerTypeToken(token) {
		if (typeof token !== "function" || this.visitedTokens.has(token)) return;
		this.visitedTokens.add(token);
		const paramInfo = CodemationTsyringeParamInfoReader.read(token);
		for (const dependency of paramInfo) this.registerDependency(dependency);
		this.registerFactoryProvider(token, paramInfo);
	}
	registerDependency(dependency) {
		const token = this.resolveDependencyToken(dependency);
		if (typeof token !== "function") return;
		if (!this.container.isRegistered(token, true)) return;
		this.registerTypeToken(token);
	}
	registerConfigTokens(value) {
		if (Array.isArray(value)) {
			value.forEach((entry) => this.registerConfigTokens(entry));
			return;
		}
		if (!value || typeof value !== "object") return;
		if (this.visitedConfigObjects.has(value)) return;
		this.visitedConfigObjects.add(value);
		if ("type" in value && typeof value.type === "function") this.registerTypeToken(value.type);
		Object.values(value).forEach((entry) => this.registerConfigTokens(entry));
	}
	registerFactoryProvider(token, paramInfo) {
		if (this.container.isRegistered(token, true)) return;
		const classToken = token;
		const constructorToken = token;
		this.container.register(classToken, { useFactory: (dependencyContainer) => {
			return new constructorToken(...paramInfo.map((dependency) => this.resolveFactoryDependency(dependencyContainer, dependency)));
		} });
	}
	resolveDependencyToken(dependency) {
		if (this.isInjectionDescriptor(dependency)) return dependency.token;
		return dependency;
	}
	resolveFactoryDependency(dependencyContainer, dependency) {
		const token = this.resolveDependencyToken(dependency);
		if (typeof token === "function") {
			if (dependencyContainer.isRegistered(token, true)) try {
				return dependencyContainer.resolve(token);
			} catch (error) {
				if (!this.isMissingTypeInfoError(error)) throw error;
			}
			this.registerTypeToken(token);
			return new token(...CodemationTsyringeParamInfoReader.read(token).map((entry) => this.resolveFactoryDependency(dependencyContainer, entry)));
		}
		return dependencyContainer.resolve(token);
	}
	isInjectionDescriptor(value) {
		return value !== null && typeof value === "object" && "token" in value;
	}
	isMissingTypeInfoError(error) {
		return error instanceof Error && error.message.includes("TypeInfo not known for");
	}
};

//#endregion
export { CodemationTsyringeParamInfoReader as n, CodemationTsyringeTypeInfoRegistrar as t };
//# sourceMappingURL=CodemationTsyringeTypeInfoRegistrar-Bj6FJYFz.js.map