<?php

declare(strict_types=1);

namespace Australcode\Rewards\Frontend;

defined( 'ABSPATH' ) || exit;

/**
 * Tiny token replacer for merchant-editable storefront messages.
 *
 * Each customer-facing surface (product earn message, cart notice, redeem form,
 * order email) renders a template — the merchant's override if set, otherwise the
 * plugin's translated default — where `{token}` placeholders are swapped for live
 * values. Lets a store write its own wording (and its own language, which also
 * sidesteps incomplete translations) instead of hard-coded copy.
 *
 * Pure and side-effect free: it does NOT escape. The caller passes already-escaped
 * value fragments and escapes the final string for its context (`wp_kses_post` for
 * HTML surfaces, `esc_html` for email). Unknown tokens are left untouched.
 */
final class MessageTemplate {

	/**
	 * Replaces every `{token}` in $template with its value, in a single pass.
	 *
	 * @param string                $template     Template with `{token}` placeholders.
	 * @param array<string,string>  $replacements Map of `{token}` => replacement value.
	 */
	public static function render( string $template, array $replacements ): string {
		return strtr( $template, $replacements );
	}
}
