import ApplicationUtils from "../../ApplicationUtils";
import { getI18n } from "../../i18n";
export class RecordValidationProcessor {
  constructor(dataUnit, validationSource, validator) {
    this._dataUnit = dataUnit;
    this._validationSource = validationSource;
    this._validator = validator;
    this._i18n = getI18n();
  }
  validate(isSaving = true) {
    return new Promise((accept, reject) => {
      const records = this._dataUnit.getModifiedRecords();
      for (let i = 0; i < records.length; i++) {
        const record = records[i];
        const invalidResults = [];
        let validationResult = this.validateRequired(record);
        if (validationResult && !validationResult.isValid) {
          invalidResults.push(validationResult);
        }
        if (this._validator) {
          validationResult = this._validator.validateRecord(record);
          if (validationResult && !validationResult.isValid) {
            invalidResults.push(validationResult);
          }
        }
        if (invalidResults.length > 0) {
          if (!isSaving && invalidResults[0].invalidFields.length > 0) {
            this._validationSource.markAsInvalid(invalidResults[0].invalidFields[0], record.__record__id__);
            reject();
            break;
          }
          this.processValidationResult(invalidResults, record.__record__id__);
          reject();
          break;
        }
      }
      return accept();
    });
  }
  validateRequired(record) {
    const requiredFields = this._validationSource.getRequiredFields();
    const invalidFields = [];
    new Set(requiredFields).forEach(field => {
      const value = record[field];
      if (value == undefined || value === "") {
        const errorMessage = this._validationSource.getMessageForField(field, record.__record__id__);
        if (errorMessage) {
          invalidFields.push({ name: field, message: errorMessage });
        }
        else {
          invalidFields.push({ name: field, message: this._i18n('utils.requiredField') });
        }
      }
    });
    if (invalidFields.length > 0) {
      return {
        isValid: false,
        invalidFields,
        infoMessage: this._i18n('utils.requiredFieldMessage')
      };
    }
    return undefined;
  }
  processValidationResult(validationResult, recordId) {
    validationResult.forEach(invalidResult => {
      const invalidFields = invalidResult.invalidFields;
      this._dataUnit.savingCanceled(invalidFields, recordId);
      if (invalidFields) {
        invalidFields.forEach(field => {
          this.markAsInvalid(field, recordId);
        });
      }
      if (invalidResult.infoMessage) {
        ApplicationUtils.info(invalidResult.infoMessage);
      }
      if (invalidResult.errorMessage) {
        const { errorTitle, errorMessage } = invalidResult;
        ApplicationUtils.error(errorTitle, errorMessage);
      }
    });
  }
  markAsInvalid(field, recordId) {
    this._dataUnit.setInvalidField(field.name, field.message, recordId);
    this._validationSource.markAsInvalid(field, recordId);
  }
}
