"use strict";
/**
 * i18n Updater
 *
 * Updates the messages/{language}.json files with new module translations.
 */
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.updateI18n = updateI18n;
exports.previewI18nMessages = previewI18nMessages;
const fs = __importStar(require("fs"));
const i18n_generator_1 = require("../transformers/i18n-generator");
/**
 * Update messages/{language}.json with new module translations
 *
 * @param data - Frontend template data
 * @param webBasePath - Base path to web app
 * @param language - Language code (e.g., "en", "es", "fr")
 * @param dryRun - Whether to perform a dry run
 * @returns Update result
 */
function updateI18n(data, webBasePath, language = "en", dryRun = false) {
    const { names, i18nKeys } = data;
    const messagesPath = `${webBasePath}/apps/web/messages/${language}.json`;
    // Check if file exists
    if (!fs.existsSync(messagesPath)) {
        return {
            success: false,
            message: `Messages file not found at ${messagesPath}`,
        };
    }
    const content = fs.readFileSync(messagesPath, "utf-8");
    let messages;
    try {
        messages = JSON.parse(content);
    }
    catch (e) {
        return {
            success: false,
            message: `Failed to parse messages/${language}.json: ${e}`,
        };
    }
    // Build the i18n messages for this module
    const moduleMessages = (0, i18n_generator_1.buildI18nMessages)(i18nKeys);
    const lowercaseModuleName = names.camelCase.toLowerCase();
    // Always ensure types section is updated (even if features already exist)
    let typesUpdated = false;
    if (!messages.types) {
        messages.types = {};
    }
    const typesKey = Object.keys(moduleMessages.types)[0];
    const lowercasePluralKey = names.pluralCamel.toLowerCase();
    if (typesKey && !messages.types[lowercasePluralKey]) {
        messages.types[lowercasePluralKey] = moduleMessages.types[typesKey];
        typesUpdated = true;
    }
    // Check if module already exists in features
    const featuresAlreadyExist = messages.features && messages.features[lowercaseModuleName];
    if (featuresAlreadyExist) {
        // Features exist, but we may have added types
        if (typesUpdated) {
            if (dryRun) {
                return {
                    success: true,
                    message: `[DRY RUN] Module ${names.camelCase} exists, would add types.${lowercasePluralKey}`,
                    alreadyExists: true,
                };
            }
            // Write updated content (types were added)
            const updatedContent = JSON.stringify(messages, null, 2);
            fs.writeFileSync(messagesPath, updatedContent, "utf-8");
            return {
                success: true,
                message: `Module ${names.camelCase} exists, added types.${lowercasePluralKey}`,
                alreadyExists: true,
            };
        }
        return {
            success: true,
            message: `Module ${names.camelCase} already exists in messages/${language}.json`,
            alreadyExists: true,
        };
    }
    // Add to features section (new module)
    if (!messages.features) {
        messages.features = {};
    }
    messages.features[lowercaseModuleName] = moduleMessages.features[i18nKeys.moduleName];
    if (dryRun) {
        return {
            success: true,
            message: `[DRY RUN] Would update messages/${language}.json with ${names.camelCase} translations`,
        };
    }
    // Write updated content with proper formatting
    const updatedContent = JSON.stringify(messages, null, 2);
    fs.writeFileSync(messagesPath, updatedContent, "utf-8");
    return {
        success: true,
        message: `Updated messages/${language}.json with ${names.camelCase} translations`,
    };
}
/**
 * Preview the i18n messages that would be added
 *
 * @param data - Frontend template data
 * @returns Preview of messages
 */
function previewI18nMessages(data) {
    const moduleMessages = (0, i18n_generator_1.buildI18nMessages)(data.i18nKeys);
    return JSON.stringify(moduleMessages, null, 2);
}
//# sourceMappingURL=i18n-updater.js.map