// to prevent form resubmission on reload
if (window.history.replaceState) {
    window.history.replaceState(null, null, window.location.href);
}

// active dashboard navlink
let currentPath = location.pathname.split('/');
let currentPage = currentPath[currentPath.length - 1].slice(0, -4);

let dashboard_navlinks = document.querySelector('.dashboard-navlinks').querySelectorAll('a');

let dashboardActiveClass = (navItem) => {
    navItem.setAttribute('class', 'dashboard-activenav');
}

switch (currentPage) {
    case 'all-exchanges-db':
        dashboardActiveClass(dashboard_navlinks[1]);
        break;
    case 'nft-collectible-rankings-db':
        dashboardActiveClass(dashboard_navlinks[2]);
        break;
    case 'events-db':
        dashboardActiveClass(dashboard_navlinks[3]);
        break;
    case 'new-partnerships-db':
        dashboardActiveClass(dashboard_navlinks[4]);
        break;
    case 'ama-series-db':
        dashboardActiveClass(dashboard_navlinks[5]);
        break;
    case 'perpetual-contract-liquidations-db':
        dashboardActiveClass(dashboard_navlinks[6]);
        break;
    default:
        break;
}

// navbar toggle
let navbar_toggle_func = () => {
    if (window.innerWidth < 768) {
        let hamburger_menu = document.querySelector('.hamburger-menu');

        let nav_bg_overlay = document.querySelector('.nav-bg-overlay');
        let dashboard_nav = document.querySelector('.dashboard-nav');

        hamburger_menu.onclick = () => {
            nav_bg_overlay.style.display = 'block';
            setTimeout(() => {
                dashboard_nav.style.transform = 'translateX(0)';
            }, 100);
        }

        nav_bg_overlay.onclick = () => {
            dashboard_nav.style.transform = 'translateX(-100%)';
            setTimeout(() => {
                nav_bg_overlay.style.display = 'none';
            }, 100);
        }
    }
}

navbar_toggle_func();

window.onresize = () => {
    navbar_toggle_func();
}

// "+ Add new" button
let addnew_exchange_btn = document.querySelector('.addnew-exchange-btn');
let addexchange_select = document.querySelector('.addexchange-select');

let addnew_exchange_btn_flag = false;

if (addnew_exchange_btn) {
    addnew_exchange_btn.onclick = (e) => {
        let exchange_nameandlogo = document.querySelector('.exchange-nameandlogo');
        let plus_or_minus = document.querySelector('.plus-or-minus');

        if (e.target.innerText === 'Add new') {
            exchange_nameandlogo.style.display = 'grid';
            e.target.innerText = 'Cancel';
            plus_or_minus.innerText = '-';
            addexchange_select.setAttribute('disabled', 'disabled');

            addnew_exchange_btn_flag = true;
        } else {
            let exchangename_input = document.querySelector('.exchange-name-input');
            let exchangename_image = document.querySelector('.exchange-image-input');
            exchangename_input.value = '';
            exchangename_image.value = '';

            exchange_nameandlogo.style.display = 'none';
            e.target.innerText = 'Add new';
            plus_or_minus.innerText = '+';
            addexchange_select.removeAttribute('disabled');

            addnew_exchange_btn_flag = false;
        }
    }
}