"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.validateForm = validateForm;
exports.validateFormArray = validateFormArray;
exports.validateFormField = validateFormField;
exports.validateFormFieldArray = validateFormFieldArray;
exports.validateSchema = validateSchema;
var _register = require("@inkline/inkline/validation/register");
var _constants = require("@inkline/inkline/constants");
var _i18n = require("@inkline/inkline/i18n");
var _types = require("@inkline/inkline/types");
async function validateFormField(schema, path = "", rootSchema) {
  const errors = [];
  const resolvedSchema = {
    ...schema
  };
  let valid = true;
  for (const rawValidator of resolvedSchema.validators || []) {
    const validator = {
      ...(typeof rawValidator === "string" ? {
        name: rawValidator
      } : rawValidator),
      schema: rootSchema,
      path
    };
    const valueIsValid = await _register.validators[validator.name](resolvedSchema.value, validator);
    if (!valueIsValid) {
      const {
        name,
        message,
        ...params
      } = validator;
      const i18nParams = {
        name: path.split(".").pop(),
        value: schema.value,
        params
      };
      const errorMessage = (message instanceof Function ? message() : message) || (0, _i18n.translate)(`validation.${name}`, i18nParams);
      errors.push({
        name,
        message: errorMessage,
        path
      });
    }
    valid = valid && valueIsValid;
  }
  resolvedSchema.valid = valid;
  resolvedSchema.invalid = !valid;
  resolvedSchema.errors = errors;
  return resolvedSchema;
}
async function validateFormFieldArray(schema, path = "", rootSchema) {
  return Promise.all(schema.map((item, index) => {
    return validateFormField(item, path ? `${path}.${index}` : `${index}`, rootSchema);
  }));
}
async function validateFormArray(schema, path = "", rootSchema) {
  return Promise.all(schema.map((item, index) => {
    return validateForm(item, path ? `${path}.${index}` : `${index}`, rootSchema);
  }));
}
async function validateForm(schema, name = "", rootSchema) {
  const resolvedSchema = {
    ...schema
  };
  let valid = true;
  const resolvedSchemaKeys = Object.keys(resolvedSchema).filter(key => !_constants.reservedValidationFields.includes(key));
  for (const key of resolvedSchemaKeys) {
    const field = resolvedSchema[key];
    let fieldIsValid = true;
    if ((0, _types.isFormFieldArray)(field)) {
      resolvedSchema[key] = await validateFormFieldArray(field, name ? `${name}.${key}` : `${key}`, rootSchema);
    } else if ((0, _types.isFormGroupArray)(field)) {
      resolvedSchema[key] = await validateFormArray(field, name ? `${name}.${key}` : `${key}`, rootSchema);
    } else if ((0, _types.isFormField)(field)) {
      resolvedSchema[key] = await validateFormField(field, name ? `${name}.${key}` : key, rootSchema);
    } else if ((0, _types.isFormGroup)(field)) {
      resolvedSchema[key] = await validateForm(field, name ? `${name}.${key}` : key, rootSchema);
    }
    if (Array.isArray(resolvedSchema[key])) {
      fieldIsValid = resolvedSchema[key].every(item => item.valid);
    } else {
      fieldIsValid = resolvedSchema[key].valid;
    }
    valid = valid && fieldIsValid;
  }
  resolvedSchema.valid = valid;
  resolvedSchema.invalid = !valid;
  return resolvedSchema;
}
async function validateSchema(schema) {
  return validateForm(schema, "", schema);
}