"use strict";
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;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TemplateHelpers = void 0;
const handlebars_1 = __importDefault(require("handlebars"));
const fs = __importStar(require("fs-extra"));
class TemplateHelpers {
    constructor() {
        this.handlebars = handlebars_1.default.create();
        this.registerHelpers();
    }
    registerHelpers() {
        // String transformation helpers
        this.handlebars.registerHelper('lowercase', (str) => {
            if (!str)
                return '';
            return str.toLowerCase();
        });
        this.handlebars.registerHelper('uppercase', (str) => {
            if (!str)
                return '';
            return str.toUpperCase();
        });
        this.handlebars.registerHelper('capitalize', (str) => {
            if (!str)
                return '';
            return str.charAt(0).toUpperCase() + str.slice(1);
        });
        this.handlebars.registerHelper('camelCase', (str) => {
            if (!str)
                return '';
            return str.replace(/-([a-z])/g, (g) => (g[1] ? g[1].toUpperCase() : ''));
        });
        this.handlebars.registerHelper('pascalCase', (str) => {
            if (!str)
                return '';
            const camel = str.replace(/-([a-z])/g, (g) => (g[1] ? g[1].toUpperCase() : ''));
            return camel.charAt(0).toUpperCase() + camel.slice(1);
        });
        // Type mapping helpers for Zod
        this.handlebars.registerHelper('zodType', (aslType) => {
            const typeMap = {
                string: 'string()',
                text: 'string()',
                number: 'number()',
                boolean: 'boolean()',
                date: 'date()',
                json: 'any()',
                ref: 'string()',
                email: 'string().email()',
            };
            return typeMap[aslType] || 'string()';
        });
        // Type mapping helpers for TypeScript
        this.handlebars.registerHelper('tsType', (aslType) => {
            const typeMap = {
                string: 'string',
                text: 'string',
                number: 'number',
                boolean: 'boolean',
                date: 'Date',
                json: 'any',
                ref: 'string',
                email: 'string',
            };
            return typeMap[aslType] || 'string';
        });
        // Comparison helpers
        this.handlebars.registerHelper('eq', (a, b) => {
            return a === b;
        });
        this.handlebars.registerHelper('neq', (a, b) => {
            return a !== b;
        });
        this.handlebars.registerHelper('gt', (a, b) => {
            return a > b;
        });
        this.handlebars.registerHelper('lt', (a, b) => {
            return a < b;
        });
        // Logical helpers
        this.handlebars.registerHelper('and', (...args) => {
            // Remove the Handlebars options object (last argument)
            const values = args.slice(0, -1);
            return values.every(Boolean);
        });
        this.handlebars.registerHelper('or', (...args) => {
            // Remove the Handlebars options object (last argument)
            const values = args.slice(0, -1);
            return values.some(Boolean);
        });
        this.handlebars.registerHelper('not', (value) => {
            return !value;
        });
        // Array/iteration helpers
        this.handlebars.registerHelper('join', (arr, separator = ', ') => {
            if (!Array.isArray(arr))
                return '';
            return arr.join(separator);
        });
        this.handlebars.registerHelper('length', (arr) => {
            if (!Array.isArray(arr))
                return 0;
            return arr.length;
        });
        // Index helpers for iterations
        this.handlebars.registerHelper('isFirst', function (index) {
            return index === 0;
        });
        this.handlebars.registerHelper('isLast', function (index, array) {
            return index === array.length - 1;
        });
        // JSON helpers
        this.handlebars.registerHelper('json', (context) => {
            return JSON.stringify(context, null, 2);
        });
        // Conditional helpers for field types
        this.handlebars.registerHelper('isTextField', (type) => {
            return type === 'string' || type === 'text' || type === 'email';
        });
        this.handlebars.registerHelper('isNumberField', (type) => {
            return type === 'number';
        });
        this.handlebars.registerHelper('isBooleanField', (type) => {
            return type === 'boolean';
        });
        this.handlebars.registerHelper('isDateField', (type) => {
            return type === 'date';
        });
        // Pluralization helper
        this.handlebars.registerHelper('pluralize', (str) => {
            if (!str)
                return '';
            // Simple pluralization logic
            if (str.endsWith('y')) {
                return str.slice(0, -1) + 'ies';
            }
            if (str.endsWith('s') || str.endsWith('sh') || str.endsWith('ch')) {
                return str + 'es';
            }
            return str + 's';
        });
        // Singularize helper
        this.handlebars.registerHelper('singularize', (str) => {
            if (!str)
                return '';
            // Simple singularization logic
            if (str.endsWith('ies')) {
                return str.slice(0, -3) + 'y';
            }
            if (str.endsWith('es')) {
                return str.slice(0, -2);
            }
            if (str.endsWith('s')) {
                return str.slice(0, -1);
            }
            return str;
        });
    }
    async render(templatePath, data) {
        const templateContent = await fs.readFile(templatePath, 'utf-8');
        const template = this.handlebars.compile(templateContent);
        return template(data);
    }
    async renderFromString(templateString, data) {
        const template = this.handlebars.compile(templateString);
        return template(data);
    }
    // Helper method to get file extension from template path
    getOutputExtension(templatePath) {
        // Extract extension from .tsx.hbs or .ts.hbs or .css.hbs
        const match = templatePath.match(/\.([^.]+)\.hbs$/);
        return match ? `.${match[1]}` : '';
    }
}
exports.TemplateHelpers = TemplateHelpers;
//# sourceMappingURL=TemplateHelpers.js.map