import JDLValidation from '../../models/jdl-validation.js';
import { validations } from '../../jhipster/index.js';
const { Validations: { PATTERN }, } = validations;
export default { convertValidations };
export function convertValidations(validations, constantValueGetter) {
    if (!validations) {
        throw new Error('Validations have to be passed so as to be converted.');
    }
    return validations.reduce((jdlValidations, parsedValidation) => {
        if (parsedValidation) {
            jdlValidations = [...jdlValidations, convertValidation(parsedValidation, constantValueGetter)];
        }
        return jdlValidations;
    }, []);
}
function convertValidation(validation, constantValueGetter) {
    let { value } = validation;
    if (validation.constant) {
        value = constantValueGetter.call(undefined, value);
    }
    if (validation.key === PATTERN) {
        value = formatThePatternValidationValue(value);
    }
    return new JDLValidation({
        name: validation.key,
        value,
    });
}
function formatThePatternValidationValue(value) {
    if (!value.includes("'")) {
        return value;
    }
    const chunks = value.split("'").map(chunk => {
        if (!chunk.endsWith('\\')) {
            return `${chunk}\\`;
        }
        return chunk;
    });
    return chunks.join("\\'");
}
