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

export const atLeastOneNumber = (validator: ValidatorFn, controls: string[] = []) =>
	(group: FormGroup,): ValidationErrors | null => {

		if (!controls) {
			controls = Object.keys(group.controls);
		}

		const hasAtLeastOne = group && group.controls && controls
			.some(k => {
				var v = group.controls[k].value;
				return !!(v && !isNaN(v) && parseFloat(v) !== 0 && !validator(group.controls[k]));
			});

		return hasAtLeastOne ? null : {
			atLeastOne: true,
		};
	};
