export function validateFields(fieldsArray: (string | boolean | any[])[][]): (string | (() => void))[][] {
    const errors = []
    let selectedGroup: number
    const numberRegex = /^[+]?([.]\d+|\d+([.]\d+)?)$/
    const emailRegex =
        /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/
    const dateRegex = /^\d{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])$/
    const timeRegex = /^\d{1,2}:\d{2}-\d{1,2}:\d{2}$/
    const phoneRegex = /^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*$/

    const wholeBlockInvalid = () => {
        const blockContainer = document.getElementsByClassName("blockContainer")

        if (blockContainer.length == 0) return
        blockContainer[0].classList.add("invalidBlocks")
    }

    const validateNumber = (value: string, field: any, element: HTMLElement) => {
        if (!numberRegex.test(`${value}`)) errors.push(["block", () => element.classList.add("border-red")])
    }

    fieldsArray.forEach(([field, value]) => {
        if (field === "blockGroups" || (typeof field === "string" && field.includes("numberLabel"))) {
            if (!field.includes("numberLabel")) return
            // @ts-ignore
            const [elementValue, element, group, boolean] = value
            if (!elementValue) return
            if (selectedGroup !== undefined) {
                if (group !== selectedGroup) errors.push(["block", wholeBlockInvalid])
                else validateNumber(elementValue, field, element)
            } else {
                selectedGroup = group
                validateNumber(elementValue, field, element)
            }
            return
        }
        value = value as any[]
        let required = value[1]
        value = value[0]
        if (!required && !value) return
        if (!value) {
            errors.push(["Eingabe ist erforderlich.", field])
        } else if (typeof field === "string") {
            if (field.includes("number_")) {
                if (!numberRegex.test(`${value}`)) errors.push(["Ungültiger numerischer Wert.", field])
            } else if (field.includes("agreement_")) {
                if (value !== true) errors.push(["Bitte das Kontrollkästchen anklicken.", field])
            } else if (field.includes("Email_")) {
                if (!emailRegex.test(value as string)) errors.push(["Ungültiges E-Mail-Format.", field])
            } else if (field.includes("date_")) {
                if (!dateRegex.test(value as string)) errors.push(["Ungültiges Datumsformat.", field])
            } else if (field.includes("times_")) {
                if (!timeRegex.test(value as string)) errors.push(["Ungültiges Zeitformat.", field])
            } else if (field.includes("Telefon_")) {
                if (!phoneRegex.test(value as string)) errors.push(["Ungültiges Telefonnummernformat.", field])
            }
        }
    })
    const blockContainer = document.getElementsByClassName("blockContainer")
    // @ts-ignore
    if (blockContainer.length && selectedGroup === undefined) {
        errors.push(["block", wholeBlockInvalid])
    }

    return errors
}
