async function _post(formObj, target, func){
    try{
        const form = await createFormData(formObj, target, func)
        const respJson = await fetch('/rss/php/handler.php', {
            method: 'POST',
            body: form
        });

        if(respJson){
            return await(respJson.json());
        }else{
            return 'Error';
        }
    }catch(errno){
        console.log('Post failed');
        console.log(errno)
    }
}

async function createFormData(data, target, func){
    const formData = new FormData();
    try{
        for(const key in data){
            if(data.hasOwnProperty(key)){
                formData.append(key, data[key]);
            }
        }
        formData.append('target', target);
        formData.append('function', func)
    }catch(errno){
        console.log('Incorrect use of object');
        return false;
    }
    return formData;
}

function createEl(el, classArr = null, elText = null, dtSet = null){
    const element = document.createElement(el);
    if(elText){
        element.appendChild(document.createTextNode(elText));
    }

    if(classArr && classArr.length){
        element.classList.add(...classArr);
    }

    if(dtSet && Object.keys(dtSet).length){
        for(const [data, value] of Object.entries(dtSet)){
            element.dataset[data] = value;
        }
    }

    return element;
}


function getRandomID(){
    return Math.random().toString(36).replace('0.', '');
}

async function generateAlert(type, text){
    const alert = document.createElement('div');
    alert.classList.add('alert');
    alert.classList.add(type);
    alert.appendChild(document.createTextNode(text));
    return alert;
}

async function removeAlert(){
    const alerts = document.querySelectorAll('.alert');
    if(alerts && alerts.length){
        for(const alert of alerts){
            alert.remove();
        }
    }
}

document.addEventListener('DOMContentLoaded', () =>{
    document.getElementById('menu-toggler').addEventListener('pointerdown', () =>{
        document.getElementById('mainMenu').classList.toggle('open');
    })

    const sections = document.querySelectorAll('section');
        const observer = new IntersectionObserver(
            (entries, observer) => {
                entries.forEach(entry => {
                    if (entry.isIntersecting) {
                        entry.target.classList.add('visible');
                        observer.unobserve(entry.target);
                    }
                });
            }, {
                threshold: 0.05,
                rootMargin: '0px 0px -50px 0px'
            }
        );

        sections.forEach(section => {
            section.classList.add('section-animation');
            observer.observe(section);
        });
});