//#region src/utils/template-interpolation.ts
/**
* Resolves a nested path from an object using dot notation.
* Example: getValue({ user: { profile: { name: 'John' } } }, 'user.profile.name') => 'John'
*
* @param obj - The object to traverse
* @param path - Dot-separated path (e.g., 'user.profile.name')
* @returns The value at the path, or undefined if not found
*/
function getValue(obj, path) {
	if (!obj || typeof obj !== "object") return;
	const keys = path.split(".");
	let current = obj;
	for (const key of keys) {
		if (current === null || current === void 0 || typeof current !== "object") return;
		current = current[key];
	}
	return current;
}
/**
* Converts a value to string for template interpolation.
* Handles primitives, null, undefined gracefully.
*
* @param value - The value to convert
* @returns String representation or empty string if undefined/null
*/
function valueToString(value) {
	if (value === null || value === void 0) return "";
	if (typeof value === "string") return value;
	if (typeof value === "number" || typeof value === "boolean") return String(value);
	try {
		return JSON.stringify(value);
	} catch {
		return "";
	}
}
/**
* Interpolates a message template with placeholders from a payload object.
* Supports {{path.to.value}} placeholder syntax with dot notation for nested paths.
* Missing values are replaced with empty strings.
*
* @param template - Message template with {{placeholder}} syntax
* @param payload - Object containing values to interpolate
* @returns Interpolated message with all placeholders resolved
*
* @example
* const template = "User {{user.name}} from {{user.profile.location}} submitted: {{message}}";
* const payload = {
*   user: { name: "Alice", profile: { location: "NYC" } },
*   message: "Hello World"
* };
* interpolateTemplate(template, payload);
* // => "User Alice from NYC submitted: Hello World"
*/
function interpolateTemplate(template, payload) {
	return template.replace(/\{\{([^}]+)\}\}/g, (_match, path) => {
		return valueToString(getValue(payload, path.trim()));
	});
}

//#endregion
export { interpolateTemplate };