/**
 * Handlebars helpers for the inference template engine.
 *
 * Pure functions — no engine state, no side effects. Registered once at
 * module load time via `registerCoreHelpers()`. Callable from any rule
 * template as `{{helper arg1 arg2}}` or as a sub-expression
 * `{{#if (helper a b)}}`.
 *
 * Phase 1 of the Handlebars rewrite: this file lands the infrastructure
 * but doesn't yet replace the old string-substitution engine. See
 * docs/plans/2026-04-17-HANDLEBARS-TEMPLATE-ENGINE-REWRITE.md.
 */
import Handlebars from 'handlebars';
/**
 * Deep equality check — used by `{{#if (eq name "value")}}` conditionals.
 * Strict JS equality is enough for the comparisons rule templates need
 * (strings against literals, numbers against literals).
 */
export function eq(a, b) {
    return a === b;
}
/**
 * camelCase / snake_case → Title Case.
 *   sortOrder → 'Sort Order'
 *   created_at → 'Created at'
 *   authorId → 'Author'   (drop trailing Id, common in relationship
 *                          names; matches runtime/views/core
 *                          humanizeFieldName so both paths agree).
 */
export function humanize(name) {
    const s = String(name ?? '');
    return s
        .replace(/Id$/, '')
        .replace(/([A-Z])/g, ' $1')
        .replace(/_/g, ' ')
        .replace(/^./, c => c.toUpperCase())
        .trim();
}
/**
 * Minimal English pluralizer. Tracks `deriveBasePath` in
 * @specverse/types/spec-rules so generated REST paths match — same
 * rule the runtime test-harness and frontend api-client use.
 *   Category → categories
 *   Box      → boxes
 *   Post     → posts
 */
export function pluralize(name) {
    const s = String(name ?? '');
    if (/[^aeiou]y$/i.test(s))
        return s.slice(0, -1) + 'ies';
    if (/(s|x|z|ch|sh)$/i.test(s))
        return s + 'es';
    return s + 's';
}
/** String.toLowerCase() as a helper — templates can't call methods directly. */
export function lowerCase(s) {
    return String(s ?? '').toLowerCase();
}
/** String.toUpperCase() as a helper. */
export function upperCase(s) {
    return String(s ?? '').toUpperCase();
}
/**
 * Boolean OR across any number of arguments — used by rule templates that
 * need to branch on one of several attribute-name matches, e.g.
 *   `{{#if (or (eq name "name") (eq name "title") (eq name "status"))}}`.
 * Handlebars always appends an options object as the final argument to
 * helper calls; strip it before reducing so it's not counted as truthy.
 */
export function or(...args) {
    return args.slice(0, -1).some(v => !!v);
}
/**
 * Register the core helpers on the global Handlebars singleton. Idempotent
 * — re-registering the same name just overwrites. Called once at
 * rule-engine module load time; tests can also call it if they need
 * a fresh registration in an isolated context.
 */
export function registerCoreHelpers() {
    Handlebars.registerHelper('eq', eq);
    Handlebars.registerHelper('humanize', humanize);
    Handlebars.registerHelper('pluralize', pluralize);
    Handlebars.registerHelper('lowerCase', lowerCase);
    Handlebars.registerHelper('upperCase', upperCase);
    Handlebars.registerHelper('or', or);
}
//# sourceMappingURL=template-helpers.js.map