import formSchemas from '@src/utils/form/formSchemas';

function formatError(err) {
    const arr = err.split(' ');

    arr[0] = arr[0].replace(/([A-Z][a-z])/g, ' $1');
    arr[0] = arr[0].charAt(0).toUpperCase() + arr[0].slice(1);

    return arr.join(' ');
}

function validateObject(obj, schema) {
    return new Promise((resolve) => {
        schema.validate(obj, { abortEarly: false })
        .then(() => {
            resolve({});
        })
        .catch((res) => {
            const formatedErrors = {};
            const paths = [];

            const uniqueErrors = res.inner.filter((err) => {
                if (!paths.includes(err.path)) {
                    paths.push(err.path);
                    return true;
                }
                return false;
            });

            uniqueErrors.forEach((err) => {
                formatedErrors[err.path] = formatError(err.message);
            });

            resolve(formatedErrors);
        });
    });
}

const documentOffsetTop = (elem) => elem && elem.offsetTop + (elem.offsetParent ? documentOffsetTop(elem.offsetParent) : 0);

export default function formValidation(data, skipScroll = false) {
    return new Promise((resolve, reject) => {
        const promises = [];

        Object.keys(data).forEach((schemaName) => {
            if (formSchemas[schemaName]) {
                promises.push(validateObject(data[schemaName], formSchemas[schemaName]));
            }
        });

        Promise.all(promises).then((results) => {
            const formErrors = results.reduce((prev, curr) => ({ ...prev, ...curr }), {});

            const errorKeys = Object.keys(formErrors);

            // Scroll to first error
            if (!skipScroll && errorKeys.length) {
                const offsets = errorKeys.map((path) =>
                    documentOffsetTop(document.getElementById(`input_${path}`))
                );

                const elem = document.querySelector('.screen-content');
                const topOffset = parseInt(window.getComputedStyle(elem).getPropertyValue('padding'), 10);

                window.scrollTo(0, Math.min(...offsets) - topOffset);
            }

            if (errorKeys.length) {
                reject(formErrors);
            }
            else {
                resolve();
            }
        })
        .catch(() => resolve());
    });
}

@@ -491,85 +491,9 @@
 nst 
-formatedErrors = %7B%7D;%0A            const paths = %5B%5D;%0A%0A            const uniqueE
+e
 rror
@@ -510,16 +510,21 @@
 ner.
-filter((
+reduce((obj, 
 err)
@@ -554,31 +554,20 @@
 f (!
-paths.includes(
+obj%5B
 err.path
 )) %7B
@@ -562,17 +562,17 @@
 err.path
-)
+%5D
 ) %7B%0A    
@@ -591,61 +591,48 @@
     
-paths.push(err.path);%0A                    return true
+obj%5Berr.path%5D = formatError(err.message)
 ;%0A  
@@ -639,32 +639,33 @@
               %7D%0A
+%0A
                 
@@ -671,21 +671,19 @@
  return 
-false
+obj
 ;%0A      
@@ -693,137 +693,11 @@
    %7D
-);%0A%0A            uniqueErrors.forEach((err) =%3E %7B%0A                formatedErrors%5Berr.path%5D = formatError(err.message);%0A            
+, %7B
 %7D);%0A
@@ -721,17 +721,9 @@
 lve(
-formatedE
+e
 rror

use .reduce instead of filter and forEach