//#region src/browser/pipeline/template.ts
/**
* Pipeline template expression engine.
*
* Supports: ${{ args.xxx }}, ${{ data }}, ${{ data | json }}, ${{ error.message }}
* Does NOT execute arbitrary JavaScript — only whitelisted expressions.
*/
const TEMPLATE_RE = /\$\{\{\s*(.+?)\s*\}\}/g;
function resolvePath(obj, path) {
	const parts = path.split(".");
	let current = obj;
	for (const part of parts) {
		if (current === null || current === void 0) return void 0;
		if (typeof current === "object") current = current[part];
		else return;
	}
	return current;
}
function applyFilter(value, filter) {
	switch (filter.trim()) {
		case "json": return JSON.stringify(value, null, 2) ?? "null";
		case "string": return String(value ?? "");
		case "number": return String(Number(value));
		case "boolean": return String(Boolean(value));
		default: return String(value ?? "");
	}
}
function evaluateExpression(expr, ctx) {
	const pipeIdx = expr.indexOf("|");
	let path = expr;
	let filter;
	if (pipeIdx > 0) {
		path = expr.slice(0, pipeIdx).trim();
		filter = expr.slice(pipeIdx + 1).trim();
	}
	let value;
	if (path === "data") value = ctx.data;
	else if (path.startsWith("args.")) value = resolvePath(ctx.args, path.slice(5));
	else if (path.startsWith("error.")) value = resolvePath(ctx.error, path.slice(6));
	else if (path === "error") value = ctx.error?.message ?? "";
	else value = resolvePath(ctx.args, path) ?? `\${{ ${expr} }}`;
	if (filter) return applyFilter(value, filter);
	if (value === void 0 || value === null) return "";
	if (typeof value === "string") return value;
	if (typeof value === "number" || typeof value === "boolean") return String(value);
	return JSON.stringify(value);
}
/**
* Resolve all `${{ ... }}` expressions in a string.
*/
function resolveTemplate(template, ctx) {
	return template.replace(TEMPLATE_RE, (_match, expr) => {
		return evaluateExpression(expr.trim(), ctx);
	});
}
/**
* Deep-resolve templates in an object/array/string value.
*/
function resolveTemplateDeep(value, ctx) {
	if (typeof value === "string") return resolveTemplate(value, ctx);
	if (Array.isArray(value)) return value.map((item) => resolveTemplateDeep(item, ctx));
	if (value && typeof value === "object") {
		const result = {};
		for (const [k, v] of Object.entries(value)) result[resolveTemplate(k, ctx)] = resolveTemplateDeep(v, ctx);
		return result;
	}
	return value;
}
//#endregion
export { resolveTemplate, resolveTemplateDeep };

//# sourceMappingURL=template.js.map