"use strict";
/**
 * TemplateRenderer — minimalist {{VAR}} substitution.
 *
 * Story 5.1 — OpenLife v1.2 Royal Stack.
 *
 * Zero external dependencies. Replaces `{{VAR}}` tokens with literal
 * string values from a flat object. Unknown variables are left in place
 * (so missing vars are visible in output rather than silently empty).
 *
 * NOT a templating engine — no conditionals, loops, or HTML escaping.
 * If those become needed in v1.3, swap to Handlebars at this same
 * surface.
 *
 * Use case: SquadCreator / SkillCreator render `dist-templates/{squad,
 * skill}-template/` markdown files with operator-supplied values.
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.renderTemplate = renderTemplate;
exports.renderTemplateFile = renderTemplateFile;
exports.listTemplateVars = listTemplateVars;
const fs = __importStar(require("fs"));
/**
 * Render a template string by substituting `{{VAR}}` tokens. Unknown
 * tokens are returned verbatim (intentional — surfaces missing vars).
 */
function renderTemplate(template, vars) {
    return template.replace(/\{\{([A-Z_][A-Z0-9_]*)\}\}/g, (match, name) => {
        return Object.prototype.hasOwnProperty.call(vars, name) ? vars[name] : match;
    });
}
/**
 * Render a template file from disk. Throws if the source file does not
 * exist (caller is expected to verify dist-templates layout up-front).
 */
function renderTemplateFile(filePath, vars) {
    if (!fs.existsSync(filePath)) {
        throw new Error(`template not found: ${filePath}`);
    }
    return renderTemplate(fs.readFileSync(filePath, 'utf-8'), vars);
}
/**
 * Inspect a template for required variables — returns the sorted list of
 * unique `{{VAR}}` tokens. Useful for tooling that wants to surface
 * "missing required variables" before render.
 */
function listTemplateVars(template) {
    const found = new Set();
    template.replace(/\{\{([A-Z_][A-Z0-9_]*)\}\}/g, (_, name) => {
        found.add(name);
        return '';
    });
    return Array.from(found).sort();
}
