"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTranslationByText = exports.getTranslationByKey = exports.applyI18nPattern = exports.convertToPascalCase = exports.convertToCamelCase = exports.extractI18nKey = void 0;
exports.generateI18nKey = generateI18nKey;
const UITranslationButton_types_1 = require("./UITranslationButton.types");
/**
 * Method extracts i18n binding and returns key of i18n entry.
 *
 * @param value Binding value.
 * @param patterns Check if method should resolve syntax annotation based i18n binding.
 * @param prefixes Allowed prefixes for single bracket pattern.
 * @returns {string | undefined} I18n entry key or undefined if input does not matches i18n binding pattern.
 */
const extractI18nKey = (value, patterns, prefixes) => {
    let key;
    for (const pattern of patterns) {
        if (pattern === UITranslationButton_types_1.TranslationTextPattern.SingleBracketBinding) {
            for (const prefix of prefixes) {
                const i18nMatch = value.toString().match(`^{${prefix}>([^\\{}:]+)}$`);
                if (i18nMatch?.[1]) {
                    key = i18nMatch?.[1];
                }
            }
        }
        else if (pattern === UITranslationButton_types_1.TranslationTextPattern.DoubleBracketReplace && value.match(`^{{[^\\{}:]+}}$`)) {
            key = value.toString().substring(2, value.length - 2);
        }
    }
    return key;
};
exports.extractI18nKey = extractI18nKey;
/**
 * Convert to camel case.
 * It gets input like 'product details info' and convert it to 'productDetailsInfo'.
 *
 * @param input Value to convert.
 * @param maxWord Maximal cxount of words to convert.
 * @returns Canverted value.
 */
const convertToCamelCase = (input = '', maxWord = 4) => {
    let output = '';
    const parts = input
        .replace(/[^a-zA-Z0-9 ]/g, '')
        .trim()
        .split(' ');
    const len = parts.length >= maxWord ? maxWord : parts.length;
    for (let i = 0; len > i; i++) {
        const part = parts[i];
        if (i === 0) {
            output += part.toLowerCase();
        }
        else {
            const initial = part.charAt(0).toUpperCase();
            const rest = part.substring(1).toLowerCase();
            output += `${initial}${rest}`;
        }
    }
    return output;
};
exports.convertToCamelCase = convertToCamelCase;
/**
 * Convert to pascal case.
 * It gets input like 'product details info' and convert it to 'ProductDetailsInfo'.
 *
 * @param input Value to convert.
 * @param maxWord Maximal cxount of words to convert.
 * @returns Canverted value.
 */
const convertToPascalCase = (input, maxWord = 4) => {
    let output = '';
    const parts = input
        .replace(/[^a-zA-Z0-9 ]/g, '')
        .trim()
        .split(' ');
    const len = parts.length >= maxWord ? maxWord : parts.length;
    for (let i = 0; len > i; i++) {
        const part = parts[i];
        const initial = part.charAt(0).toUpperCase();
        const rest = part.substring(1).toLowerCase();
        output += `${initial}${rest}`;
    }
    return output;
};
exports.convertToPascalCase = convertToPascalCase;
/**
 * Get unique key.
 * If the key is not unique, it increment key by one and recheck.
 *
 * @param key new key and it is incremented.
 * @param i18nData I18n entries.
 * @param originalKey original key without any index increment.
 * @param counter counter for increment.
 * @returns Generated i18n key.
 */
const getI18nUniqueKey = (key, i18nData, originalKey = key, counter = 1) => {
    const uniqueKey = key;
    if (i18nData[key] !== undefined) {
        key = `${originalKey}${counter}`;
        counter++;
        return getI18nUniqueKey(key, i18nData, originalKey, counter);
    }
    return uniqueKey;
};
/**
 * Generates a unique i18n key for the text.
 * It considers currently active bundle and translation key generator.
 *
 * @param text
 * @param translationKeyGenerator
 * @param bundle
 * @returns Generated i18n key.
 */
function generateI18nKey(text, translationKeyGenerator, bundle) {
    let key = translationKeyGenerator === UITranslationButton_types_1.TranslationKeyGenerator.CamelCase
        ? (0, exports.convertToCamelCase)(text)
        : (0, exports.convertToPascalCase)(text);
    if (key === '') {
        key = translationKeyGenerator === UITranslationButton_types_1.TranslationKeyGenerator.CamelCase ? 'key' : 'Key';
    }
    return getI18nUniqueKey(key, bundle);
}
/**
 * Method to apply passed pattern for passed key.
 *
 * @param key I18n key.
 * @param pattern I18n pattern.
 * @param prefix Prefix for single bracket pattern.
 * @returns Generated i18n value for passed pattern.
 */
const applyI18nPattern = (key, pattern, prefix) => {
    return pattern === UITranslationButton_types_1.TranslationTextPattern.DoubleBracketReplace ? `{{${key}}}` : `{${prefix}>${key}}`;
};
exports.applyI18nPattern = applyI18nPattern;
/**
 * Method finds first existing i18n key searching by key.
 *
 * @param {I18nBundle} bundle Search for value.
 * @param {string} key Search for key.
 * @returns {TranslationEntry | undefined} Key if value is found.
 */
const getTranslationByKey = (bundle, key) => {
    const entries = bundle[key];
    if (entries?.length > 0) {
        return entries[0];
    }
    return undefined;
};
exports.getTranslationByKey = getTranslationByKey;
/**
 * Method finds existing i18n key searching by value.
 *
 * @param {I18nBundle} bundle Search for value.
 * @param {string} value Search for value.
 * @returns {TranslationEntry | undefined} Key if value is found.
 */
const getTranslationByText = (bundle, value) => {
    for (const key in bundle) {
        const entries = bundle[key];
        if (entries.length) {
            const first = entries[0];
            if (first.value.value === value) {
                return first;
            }
        }
    }
    return undefined;
};
exports.getTranslationByText = getTranslationByText;
//# sourceMappingURL=UITranslationUtils.js.map