import { AbstractControl, ValidationErrors, ValidatorFn } from '@angular/forms';

export function minMaxValidator(minKey: string, maxKey: string, errorKey: string): ValidatorFn {
    return (formGroup: AbstractControl): ValidationErrors | null => {
        const minControl = formGroup.get(minKey);
        const maxControl = formGroup.get(maxKey);
        if (minControl && maxControl && minControl.value > maxControl.value) {
            return {
                [errorKey]: {
                    message: `${minKey} (${minControl.value}) must be less than or equal to ${maxKey} (${maxControl.value})`
                }
            };
        }
        return null;
    };
}