const TRANSLATE_FUNCTION_ARGS = /\(\s*'(?<key>[^']+)'(?:,\s*(?<interpolate>\{(?:(?!\}\))[\s\S])*\}))?\)/gs.source;
function getTranslationValue(getWebappTranslation, key, data) {
    return getWebappTranslation(key, data) || undefined;
}
export const replaceTranslationKeysWithText = (getWebappTranslation, body, regexp, { keyPattern, interpolatePattern, wrapTranslation, escapeHtml, stringify } = {}) => {
    const matches = body.matchAll(new RegExp(regexp, 'g'));
    if (typeof wrapTranslation === 'string') {
        wrapTranslation = [wrapTranslation, wrapTranslation];
    }
    for (const match of matches) {
        const target = match[0];
        let key = match?.groups?.key;
        if (!key && keyPattern) {
            const keyMatch = target.match(new RegExp(keyPattern));
            key = keyMatch?.groups?.key;
        }
        if (!key) {
            throw new Error(`Translation key not found for ${target}`);
        }
        let interpolate = match.groups && match.groups.interpolate;
        if (!interpolate && interpolatePattern) {
            const interpolateMatch = target.match(new RegExp(interpolatePattern));
            interpolate = interpolateMatch?.groups?.interpolate;
        }
        let data;
        if (interpolate) {
            data = {};
            try {
                const interpolateValues = JSON.parse(interpolate);
                for (const [field, value] of Object.entries(interpolateValues)) {
                    data[field] = value;
                }
            }
            catch {
                throw new Error(`Translation interpolations values should be a JSON, ${interpolate}`);
            }
        }
        const translation = getTranslationValue(getWebappTranslation, key, data);
        let replacement = translation;
        if (!replacement) {
            replacement = wrapTranslation ? `${wrapTranslation[0]}${wrapTranslation[1]}` : '';
        }
        else if (wrapTranslation) {
            replacement = `${wrapTranslation[0]}${translation}${wrapTranslation[1]}`;
        }
        else if (escapeHtml) {
            replacement = replacement.replace(/'/g, '&apos;').replace(/"/g, '&quot;').replace(/@/g, '&#64;');
        }
        else if (stringify) {
            replacement = JSON.stringify(replacement);
        }
        body = body.replace(target, replacement);
    }
    return body;
};
export const createJhiTransformTranslateReplacer = (getWebappTranslation, translateOptions) => (body) => replaceTranslationKeysWithText(getWebappTranslation, body, `__jhiTransformTranslate__${TRANSLATE_FUNCTION_ARGS}`, translateOptions);
export const createJhiTransformTranslateStringifyReplacer = getWebappTranslation => (body) => replaceTranslationKeysWithText(getWebappTranslation, body, `__jhiTransformTranslateStringify__${TRANSLATE_FUNCTION_ARGS}`, {
    stringify: true,
});
