import { Container } from 'aurelia-framework';
import { IncompleteMlField } from './incomplete-multilingual-field/incomplete-multilingual-field';
import { UtilsMlField } from './utils-ml-field';
import { Config, CustomLogger } from '../exports';
import { I18N } from 'aurelia-i18n';
import { Entity } from 'breeze-client';
import { Language } from './../model/common-model';

export class UtilsMlFieldHelper {
  public static async BeforeSave(entity: Entity, mlProps: Array<IncompleteMlField> = [], languages: Array<Language> = null) {
    if (languages == null) {
      languages = Container.instance.get(Config).globalConfig.configuredLanguages;
    }
    let areTranslationsFilled = await UtilsMlField.checkMultilingualField(entity, mlProps, languages);

    let isValid: boolean = true;
    if (!areTranslationsFilled) {
      let logger = Container.instance.get(CustomLogger);
      let i18n = Container.instance.get(I18N);
      let key = 'incompletemlfield.youMustFillEmptyTr';
      let msg = i18n.i18next.exists(key) ? i18n.tr(key) : i18n.tr(key, { ns: 'common' })
      logger.LogError(msg, null, null, true);
      isValid = false;
    }
    return isValid;
  }
}
