import { elideItalian } from "@accessibit/types";

/**
 * Substitute `{{key}}` placeholders in `template` with values from `vars`.
 * Missing keys collapse to an empty string.
 *
 * Single source of truth for `{{var}}` interpolation across the dashboard and
 * this package — email templates, dialogs, and dictionary copy. Importing this
 * helper keeps placeholder semantics (regex, missing-key behavior, stringify
 * rules) consistent everywhere.
 */
export function fill(
  template: string,
  vars: Record<string, string | number>,
): string {
  return template.replace(/\{\{(\w+)\}\}/g, (_, key: string) =>
    vars[key] !== undefined ? String(vars[key]) : "",
  );
}

/**
 * `fill`, in the language the sentence is in: Italian is elided ("dall'8
 * mar"), the others are left as written (Spanish says "del 8", "al 11").
 */
export function fillFor(
  lang: string,
  template: string,
  vars: Record<string, string | number>,
): string {
  const text = fill(template, vars);
  return lang === "it" ? elideItalian(text) : text;
}
