// lib/process/template.js — mustache-lite token substitution.
//
// Resolves `{{ a.b.c }}` against a scope object. Missing paths render as the
// empty string so partially-populated intake payloads don't blow up template
// rendering. Used by stages/human-approval and stages/package.

export function applyTemplate(template, scope) {
  if (!template || typeof template !== 'string') return template;
  return template.replace(/{{\s*([^}]+?)\s*}}/g, (_, expr) => {
    const parts = String(expr).split('.');
    let cur = scope;
    for (const part of parts) {
      if (cur == null) return '';
      cur = cur[part];
    }
    return cur == null ? '' : String(cur);
  });
}
