/**
 * debouncing is used to delay the execution of a function for {timeout}
 * if we call the function again within the {timeout} the previous calls will be cancelled
 * e.g. -> user types fast on the search box, we dont want to spam the server for every letter the user types,
 * so we schedule the execution for later and only send a request when the user has stoped typed for {timeout} s
 */
import React from "react";


export function debounce(func:any, timeout = 300) {

    let timer:any;

    function debounced (...args: any) {
        clearTimeout(timer);
        timer = setTimeout(() => {
                // @ts-ignore
                func(...args)

            }
            , timeout);
    };

    function cancel() {
        timer && clearTimeout(timer);
    }

    debounced.cancel = cancel
    return debounced
}

/**
 * throttling (the reverse of debnoucing) is used to prevent execution of the same function for {timeout} ms
 * if we call the function again within the {timeout} it will be ignored
 */
function throttle(func:any, timeout = 300) {

    let timer:any;
    return (...args:any) => {
        if (timer) return;
        func(...args);
        timer = setTimeout(() => {
                timer = null;
            }
            , timeout);
    };
}

export function getRate(data: number[]){

    const from = data[0]
    const to = data[data.length - 1]
    if (from==0 && to ==0) return '0'
    return ((to-from)/from).toFixed(2)

}

export function arraySum(array:number[]){
    return  array.reduce((sum,current)=>{
        return sum + current
    })
}

export function normaliseGreek(text:string) {

    text = text.replace(/Ά|Α|ά/g, 'α')
       .replace(/Έ|Ε|έ/g, 'ε')
       .replace(/Ή|Η|ή/g, 'η')
       .replace(/Ί|Ϊ|Ι|ί|ΐ|ϊ/g, 'ι')
       .replace(/Ό|Ο|ό/g, 'ο')
       .replace(/Ύ|Ϋ|Υ|ύ|ΰ|ϋ/g, 'υ')
       .replace(/Ώ|Ω|ώ/g, 'ω')
       .replace(/Σ|ς/g, 'σ');

    return text;
}

export function forceUppercaseGreek(text:string) {

    return normaliseGreek(text).toUpperCase().replace(/[^Α-Ω\- ]/gi, '').replace("--", "-")
}

export function forceUppercaseEnglish(text:string) {
    return text.toUpperCase().replace(/[^A-Z\-]/gi, '')
}
export function forceUppercase(text:string) {
    return text.toUpperCase()
}

// validation for afm activation form (2nd step)
export function validateForm(data:any){
    const newErrors = []
    if (data.afm.length != 9) newErrors.push('afm')
    if (!data.firstname || data.firstname.length < 3) newErrors.push('firstname')
    if (!data.lastname || data.lastname.length < 3) newErrors.push('lastname')
    if (!data.father_name || data.father_name.length < 3) newErrors.push('father_name')
    if (!data.firstname_en || data.firstname_en.length < 3) newErrors.push('firstname_en')
    if (!data.lastname_en || data.lastname_en.length < 3) newErrors.push('lastname_en')
    if (!data.father_name_en || data.father_name_en.length < 3) newErrors.push('father_name_en')
    if (!data.social_sec_number || data.social_sec_number.length != 11) newErrors.push('social_sec_number')
    if (!data.work_relation_id) newErrors.push('work_relation_id')
    if (!data.education_category_id) newErrors.push('education_category_id')
    if (!data.staff_category_id) newErrors.push('staff_category_id')
    if (!data.specialization_id) newErrors.push('specialization_id')
    if (data.mobile.length != 0 && data.mobile.length != 10) newErrors.push('mobile')
    return newErrors
}


// Helper function to extract plain text from a child element
export const extractPlainTextFromChild = (child: React.ReactNode): string => {
    if (typeof child === 'string') {
        return child;
    }
    if (Array.isArray(child)) {
        return child.map(extractPlainTextFromChild).join(' ');
    }
    if (React.isValidElement(child) && child.props.children) {
        return extractPlainTextFromChild(child.props.children);
    }
    return '';
};