let _trace_bin=[];
const utils = {
    /* To avoid checking element and global css tasks handling 
       utils.css('toggle','main-header','',document,'h-10!')
       utils.css('get','img','tag:3')
    */
    css: (task, css_target, typ='object', root=document, css_name=['hidden'] ) => {
        let target = typeof css_target == 'string' ? undefined : css_target;
        let is_array = false;
        const elem_toggle = (elem, css_class) => {
            if (elem.classList) {
                if (elem.classList.contains(css_class)) {
                    elem.classList.remove(css_class);
                } else {
                    elem.classList.add(css_class);
                }
            }
        }
        let css_typ = typ; 
        if (task == 'get' && typ === 'object') { css_typ='id';}
        let pos = -1;
        if (css_typ.includes(':')) {
            const arr_css = typ.split(':');
            css_typ = arr_css[0];
            pos = arr_css[1];
        }
        if (typeof target === 'undefined') {
            switch(css_typ) {
                case 'object':
                case 'id':
                    target = document.getElementById(css_target);
                    break;
                case 'class':
                    if (root.getElementsByClassName) {
                        is_array=true;
                        if (pos != -1) {
                            const class_targets = root.getElementsByClassName(css_target);
                            if (class_targets && class_targets[pos]) { target = class_targets[pos]; }
                        } else {
                            target = root.getElementsByClassName(css_target);
                        }
                    }
                    break;
                case 'tag':
                    if (root.getElementsByClassName) {
                        is_array=true;
                        if (pos != -1) {
                            const tag_targets = root.getElementsByTagName(css_target);
                            if (tag_targets && tag_targets[pos]) { target = tag_targets[pos]; }
                        } else {
                            target = root.getElementsByTagName(css_target);
                        }
                    }
                    break;
                default:
                    target = document.getElementById(css_target);
            }
        }
        if (target) {
            if (typeof target.classList == 'undefined') { is_array = true;}
            switch (task) {
                case 'get':
                    return target;
                    break;
                case 'is_add':
                case 'is_hide':
                    if (is_array) {
                        return target[0].classList && target[0].classList.contains(css_name) ? true : false;
                    } else { 
                        return target.classList && target.classList.contains(css_name) ? true : false;
                    }
                    break;
                case 'add':
                case 'hide':
                    if (is_array) {
                        [...target].forEach(it => { if (it.classList) { it.classList.add(css_name);}  });
                    } else { 
                        if (target.classList) target.classList.add(css_name);
                    }
                    return target;
                    break;
                case 'remove':
                case 'show':
                    if (is_array) {
                        [...target].forEach(it => { if (it.classList) { it.classList.remove(css_name);} } );
                    } else { 
                        if (target.classList) target.classList.remove(css_name);
                    }
                    return target;
                    break;
                case 'is_show':
                case 'is_show':
                    if (is_array) {
                        return target[0].classList && !target[0].classList.contains(css_name) ? true : false;
                    } else { 
                        return target.classList && !target.classList.contains(css_name) ? true : false;
                    }
                    break;
                case 'toggle':
                    if (is_array) {
                        [...target].forEach(it => elem_toggle(it, css_name));
                    } else {
                        elem_toggle(target, css_name);
                    }       
                    return target;
                default:
                    break;
            }
        }
        return target;
    },
    update_session: async (data) => {
        if (UPDATE_SESSION_URL) {
            const response = await fetch(UPDATE_SESSION_URL, {
                method: 'POST',
                body: JSON.stringify(data),
                headers: {
                    'Content-type': 'application/json; charset=UTF-8',
                }
            });
            if (!response.ok && response.status != 200 ) {
                throw new Error('Update session was not OK');
                return false;
            }
        }
    },
};