import path from 'node:path';
import fs from 'node:fs';
import hbs from 'handlebars';
hbs.registerHelper('json', (object) => {
    return `<pre>${JSON.stringify(object, null, 2)}</pre>`;
});
export class OIDCTemplateEngine {
    templates;
    logger;
    layoutHbs;
    loginHbs;
    consentHbs;
    constructor(templates, logger) {
        this.templates = templates;
        this.logger = logger;
    }
    async load(target) {
        const template = this.templates?.[target];
        if (template) {
            this.logger?.info(`OIDC: Loading template ${template}`);
            const layout = await new Promise((resolve, reject) => {
                fs.readFile(path.resolve(template), (err, data) => err ? reject(err) : resolve(data.toString()));
            });
            return hbs.compile(layout);
        }
        else {
            const msg = `OIDC: 'odic.template.${target}' not configured!`;
            this.logger?.warn(msg);
            throw new Error(msg);
        }
    }
    async layout(context) {
        this.layoutHbs ??= await this.load('layout');
        return this.layoutHbs(context);
    }
    async login(context) {
        this.loginHbs ??= await this.load('login');
        return this.loginHbs(context);
    }
    async consent(context) {
        this.consentHbs ??= await this.load('consent');
        return this.consentHbs(context);
    }
}
//# sourceMappingURL=templates.js.map