import { passthrough } from '@yeoman/transform';
import { Minimatch } from 'minimatch';
import { createJhiTransformTranslateReplacer, createJhiTransformTranslateStringifyReplacer } from '../../languages/support/index.mjs';
const PLACEHOLDER_REGEX = /(?:placeholder|title)=['|"](\{\{\s?['|"]([a-zA-Z0-9.\-_]+)['|"]\s?\|\s?translate\s?\}\})['|"]/.source;
const JHI_TRANSLATE_REGEX = /(\n?\s*[a-z][a-zA-Z]*Translate="[a-zA-Z0-9 +{}'_!?.]+")/.source;
const TRANSLATE_VALUES_REGEX = /(\n?\s*\[translateValues\]="\{(?:(?!\}").)*?\}")/.source;
const TRANSLATE_REGEX = [JHI_TRANSLATE_REGEX, TRANSLATE_VALUES_REGEX].join('|');
function getTranslationValue(getWebappTranslation, key, data) {
    return getWebappTranslation(key, data) || undefined;
}
function replaceTranslationKeysWithText(getWebappTranslation, content, regexSource, { keyIndex = 1, replacementIndex = 1, escape } = {}) {
    const regex = new RegExp(regexSource, 'g');
    const allMatches = content.matchAll(regex);
    for (const match of allMatches) {
        const key = match[keyIndex];
        const target = match[replacementIndex];
        let translation = getTranslationValue(getWebappTranslation, key);
        if (escape) {
            translation = escape(translation, match);
        }
        content = content.replace(target, translation);
    }
    return content;
}
function replaceJSTranslation(getWebappTranslation, content, jsKey) {
    return replaceTranslationKeysWithText(getWebappTranslation, content, `${jsKey}\\s?:\\s?['|"]([a-zA-Z0-9.\\-_]+\\.[a-zA-Z0-9.\\-_]+)['|"]`, {
        escape: (translation, match) => translation.replaceAll(match[0].slice(-1), `\\${match[0].slice(-1)}`),
    });
}
function replacePageTitles(getWebappTranslation, content) {
    return replaceJSTranslation(getWebappTranslation, content, 'title');
}
function replacePlaceholders(getWebappTranslation, content) {
    return replaceTranslationKeysWithText(getWebappTranslation, content, PLACEHOLDER_REGEX, { keyIndex: 2 });
}
function replaceErrorMessage(getWebappTranslation, content) {
    return replaceJSTranslation(getWebappTranslation, content, 'errorMessage');
}
export const createTranslationReplacer = (getWebappTranslation, enableTranslation) => {
    const htmlJhiTranslateReplacer = createJhiTransformTranslateReplacer(getWebappTranslation);
    const htmlJhiTranslateStringifyReplacer = createJhiTransformTranslateStringifyReplacer(getWebappTranslation);
    return function replaceAngularTranslations(content, filePath) {
        if (/\.html$/.test(filePath)) {
            if (!enableTranslation) {
                content = content.replace(new RegExp(TRANSLATE_REGEX, 'g'), '');
                content = replacePlaceholders(getWebappTranslation, content);
            }
        }
        if (/(:?\.html|component\.ts)$/.test(filePath)) {
            content = htmlJhiTranslateReplacer(content);
            content = htmlJhiTranslateStringifyReplacer(content);
        }
        if (!enableTranslation) {
            if (/(:?route|module)\.ts$/.test(filePath)) {
                content = replacePageTitles(getWebappTranslation, content);
            }
            if (/error\.route\.ts$/.test(filePath)) {
                content = replaceErrorMessage(getWebappTranslation, content);
            }
        }
        return content;
    };
};
const minimatch = new Minimatch('**/*{.html,.component.ts,.route.ts,.module.ts}');
export const isTranslatedAngularFile = file => minimatch.match(file.path);
const translateAngularFilesTransform = (getWebappTranslation, enableTranslation) => {
    const translate = createTranslationReplacer(getWebappTranslation, enableTranslation);
    return passthrough(file => {
        file.contents = Buffer.from(translate(file.contents.toString(), file.path));
    });
};
export default translateAngularFilesTransform;
