import get from 'lodash/get';
import { createErrorHandler, getDefaultFormState, getUiOptions, PROPERTIES_KEY, toErrorSchema, unwrapErrorHandler, validationDataMerge, } from '@rjsf/utils';
/** Transforming the error output from ajv to format used by @rjsf/utils.
 * At some point, components should be updated to support ajv.
 *
 * @param errors - The list of AJV errors to convert to `RJSFValidationErrors`
 * @param [uiSchema] - An optional uiSchema that is passed to `transformErrors` and `customValidate`
 */
export function transformRJSFValidationErrors(errors = [], uiSchema) {
    return errors.map((e) => {
        const { instancePath, keyword, params, schemaPath, parentSchema, ...rest } = e;
        let { message = '' } = rest;
        let property = instancePath.replace(/\//g, '.');
        let stack = `${property} ${message}`.trim();
        if ('missingProperty' in params) {
            property = property ? `${property}.${params.missingProperty}` : params.missingProperty;
            const currentProperty = params.missingProperty;
            let uiSchemaTitle = getUiOptions(get(uiSchema, `${property.replace(/^\./, '')}`)).title;
            if (uiSchemaTitle === undefined) {
                const uiSchemaPath = schemaPath
                    .replace(/\/properties\//g, '/')
                    .split('/')
                    .slice(1, -1)
                    .concat([currentProperty]);
                uiSchemaTitle = getUiOptions(get(uiSchema, uiSchemaPath)).title;
            }
            if (uiSchemaTitle) {
                message = message.replace(`'${currentProperty}'`, `'${uiSchemaTitle}'`);
            }
            else {
                const parentSchemaTitle = get(parentSchema, [PROPERTIES_KEY, currentProperty, 'title']);
                if (parentSchemaTitle) {
                    message = message.replace(`'${currentProperty}'`, `'${parentSchemaTitle}'`);
                }
            }
            stack = message;
        }
        else {
            const uiSchemaTitle = getUiOptions(get(uiSchema, `${property.replace(/^\./, '')}`)).title;
            if (uiSchemaTitle) {
                stack = `'${uiSchemaTitle}' ${message}`.trim();
            }
            else {
                const parentSchemaTitle = parentSchema === null || parentSchema === void 0 ? void 0 : parentSchema.title;
                if (parentSchemaTitle) {
                    stack = `'${parentSchemaTitle}' ${message}`.trim();
                }
            }
        }
        // put data in expected format
        return {
            name: keyword,
            property,
            message,
            params,
            stack,
            schemaPath,
        };
    });
}
/** This function processes the `formData` with an optional user contributed `customValidate` function, which receives
 * the form data and a `errorHandler` function that will be used to add custom validation errors for each field. Also
 * supports a `transformErrors` function that will take the raw AJV validation errors, prior to custom validation and
 * transform them in what ever way it chooses.
 *
 * @param validator - The `ValidatorType` implementation used for the `getDefaultFormState()` call
 * @param rawErrors - The list of raw `ErrorObject`s to process
 * @param formData - The form data to validate
 * @param schema - The schema against which to validate the form data
 * @param [customValidate] - An optional function that is used to perform custom validation
 * @param [transformErrors] - An optional function that is used to transform errors after AJV validation
 * @param [uiSchema] - An optional uiSchema that is passed to `transformErrors` and `customValidate`
 */
export default function processRawValidationErrors(validator, rawErrors, formData, schema, customValidate, transformErrors, uiSchema) {
    const { validationError: invalidSchemaError } = rawErrors;
    let errors = transformRJSFValidationErrors(rawErrors.errors, uiSchema);
    if (invalidSchemaError) {
        errors = [...errors, { stack: invalidSchemaError.message }];
    }
    if (typeof transformErrors === 'function') {
        errors = transformErrors(errors, uiSchema);
    }
    let errorSchema = toErrorSchema(errors);
    if (invalidSchemaError) {
        errorSchema = {
            ...errorSchema,
            $schema: {
                __errors: [invalidSchemaError.message],
            },
        };
    }
    if (typeof customValidate !== 'function') {
        return { errors, errorSchema };
    }
    // Include form data with undefined values, which is required for custom validation.
    const newFormData = getDefaultFormState(validator, schema, formData, schema, true);
    const errorHandler = customValidate(newFormData, createErrorHandler(newFormData), uiSchema);
    const userErrorSchema = unwrapErrorHandler(errorHandler);
    return validationDataMerge({ errors, errorSchema }, userErrorSchema);
}
//# sourceMappingURL=processRawValidationErrors.js.map