
import { Config } from "../config";
import { Container } from 'aurelia-framework';
import { TranslatedProperty, Language } from './../model/common-model';
import { ValidationRules } from "aurelia-validation";

export class TranslatedPropertyRules {
    public static anyRequired(key: string) {
        return ValidationRules.ensure(key)
            .satisfies((value: TranslatedProperty) =>
                this.languages.reduce((valid: boolean, language: Language) =>
                    valid || (value['lang' + (language.columnIndex)] && (value['lang' + (language.columnIndex)] as string).length > 0),
                    false)
            );
    }

    public static allRequired(key: string) {
        return ValidationRules.ensure(key)
            .satisfies((value: TranslatedProperty) =>
                this.languages.reduce((valid: boolean, language: Language) =>
                    valid && (value['lang' + (language.columnIndex)] && (value['lang' + (language.columnIndex)] as string).length > 0),
                    true)
            )
    }

    private static get languages(): Language[] {
        return Container.instance.get(Config).globalConfig.configuredLanguages || [];
    }
}