"use strict";
/**
 * i18n Generator
 *
 * Generates internationalization keys and messages for a module.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateI18nKeys = generateI18nKeys;
exports.buildI18nMessages = buildI18nMessages;
exports.getFieldLabelKey = getFieldLabelKey;
exports.getFieldPlaceholderKey = getFieldPlaceholderKey;
exports.getFieldErrorKey = getFieldErrorKey;
exports.getTypeKey = getTypeKey;
exports.generateTranslationCall = generateTranslationCall;
exports.mergeI18nMessages = mergeI18nMessages;
exports.sortObjectKeys = sortObjectKeys;
const name_transformer_1 = require("./name-transformer");
/**
 * Generate complete i18n key set for a module
 *
 * @param names - Name transforms
 * @param fields - Array of frontend fields
 * @param relationships - Array of frontend relationships
 * @returns i18n key set
 */
function generateI18nKeys(names, fields, relationships) {
    const lowerModuleName = names.camelCase.toLowerCase();
    // Generate field keys
    const fieldKeys = {};
    fields.forEach((field) => {
        fieldKeys[field.name] = {
            label: (0, name_transformer_1.toTitleCase)(field.name),
            placeholder: `Enter ${(0, name_transformer_1.toTitleCase)(field.name).toLowerCase()}`,
            error: `${(0, name_transformer_1.toTitleCase)(field.name)} is required`,
        };
    });
    // Generate relationship keys
    const relationshipKeys = {};
    relationships.forEach((rel) => {
        const effectiveName = rel.variant || rel.name;
        const effectiveKey = (0, name_transformer_1.toCamelCase)(effectiveName).toLowerCase();
        relationshipKeys[effectiveKey] = {
            label: (0, name_transformer_1.toTitleCase)(effectiveName),
            placeholder: `Select ${(0, name_transformer_1.toTitleCase)(effectiveName).toLowerCase()}`,
            error: `${(0, name_transformer_1.toTitleCase)(effectiveName)} is required`,
            list: (0, name_transformer_1.pluralize)((0, name_transformer_1.toTitleCase)(rel.name)),
        };
        // Add fields for relationship edge properties
        if (rel.fields && rel.fields.length > 0) {
            relationshipKeys[effectiveKey].fields = {};
            rel.fields.forEach((field) => {
                relationshipKeys[effectiveKey].fields[field.name] = {
                    label: (0, name_transformer_1.toTitleCase)(field.name),
                    placeholder: `Enter ${(0, name_transformer_1.toTitleCase)(field.name).toLowerCase()}`,
                    error: `${(0, name_transformer_1.toTitleCase)(field.name)} is required`,
                };
            });
        }
    });
    // Generate type keys
    const singularTitle = names.titleCase;
    const pluralTitle = names.pluralTitleCase;
    return {
        moduleName: lowerModuleName,
        fields: fieldKeys,
        relationships: relationshipKeys,
        type: {
            singular: singularTitle,
            plural: pluralTitle,
            icuPlural: `{count, plural, =1 {${singularTitle}} other {${pluralTitle}}}`,
        },
    };
}
/**
 * Build the i18n message object to merge into en.json
 *
 * @param i18nKeys - i18n key set
 * @returns Object structure for en.json
 */
function buildI18nMessages(i18nKeys) {
    // Use proper pluralization and lowercase for types key
    const pluralLowercaseKey = (0, name_transformer_1.pluralize)(i18nKeys.moduleName).toLowerCase();
    return {
        features: {
            [i18nKeys.moduleName]: {
                fields: i18nKeys.fields,
                relationships: i18nKeys.relationships,
            },
        },
        types: {
            [pluralLowercaseKey]: i18nKeys.type.icuPlural,
        },
    };
}
/**
 * Get the translation key path for a field label
 *
 * @param moduleName - Module name (lowercase)
 * @param fieldName - Field name
 * @returns Translation key path
 */
function getFieldLabelKey(moduleName, fieldName) {
    return `features.${moduleName}.fields.${fieldName}.label`;
}
/**
 * Get the translation key path for a field placeholder
 *
 * @param moduleName - Module name (lowercase)
 * @param fieldName - Field name
 * @returns Translation key path
 */
function getFieldPlaceholderKey(moduleName, fieldName) {
    return `features.${moduleName}.fields.${fieldName}.placeholder`;
}
/**
 * Get the translation key path for a field error
 *
 * @param moduleName - Module name (lowercase)
 * @param fieldName - Field name
 * @returns Translation key path
 */
function getFieldErrorKey(moduleName, fieldName) {
    return `features.${moduleName}.fields.${fieldName}.error`;
}
/**
 * Get the translation key path for type pluralization
 *
 * @param pluralKebab - Plural kebab-case name
 * @returns Translation key path
 */
function getTypeKey(pluralKebab) {
    return `types.${pluralKebab}`;
}
/**
 * Generate translation call for use in templates
 *
 * @param keyPath - Full translation key path
 * @param params - Optional parameters for ICU messages
 * @returns Translation function call as string
 */
function generateTranslationCall(keyPath, params) {
    if (params) {
        const paramStr = JSON.stringify(params);
        return `t(\`${keyPath}\`, ${paramStr})`;
    }
    return `t(\`${keyPath}\`)`;
}
/**
 * Merge i18n messages into existing messages object
 * This performs a deep merge, preserving existing keys
 *
 * @param existing - Existing messages object
 * @param newMessages - New messages to merge
 * @returns Merged messages object
 */
function mergeI18nMessages(existing, newMessages) {
    const result = { ...existing };
    function deepMerge(target, source) {
        for (const key of Object.keys(source)) {
            if (source[key] &&
                typeof source[key] === "object" &&
                !Array.isArray(source[key])) {
                if (!target[key]) {
                    target[key] = {};
                }
                deepMerge(target[key], source[key]);
            }
            else {
                target[key] = source[key];
            }
        }
    }
    deepMerge(result, newMessages);
    return result;
}
/**
 * Sort object keys alphabetically (for consistent output)
 *
 * @param obj - Object to sort
 * @returns New object with sorted keys
 */
function sortObjectKeys(obj) {
    const sorted = {};
    const keys = Object.keys(obj).sort();
    for (const key of keys) {
        if (obj[key] && typeof obj[key] === "object" && !Array.isArray(obj[key])) {
            sorted[key] = sortObjectKeys(obj[key]);
        }
        else {
            sorted[key] = obj[key];
        }
    }
    return sorted;
}
//# sourceMappingURL=i18n-generator.js.map