const catList = document.querySelector(".wc-block-product-categories-list");

function addShowAllCat(shopUrl) {
    if (typeof catList !== 'undefined' && catList) {

        const showAllItem = document.createElement("li");
        showAllItem.classList.add("show-all-categories");

        const link = document.createElement("a");
        link.href = shopUrl;
        link.textContent = "Pokaż wszystkie";

        showAllItem.appendChild(link);

        catList.insertBefore(showAllItem, catList.firstChild);
    }
}


function setActiveCatColor() {
    const path = window.location.pathname;
    const catList = document.querySelector(".wc-block-product-categories-list");
    if (!catList) return;

    const listItems = catList.querySelectorAll("li");

    if (path === "/sklep" || path === "/sklep/") {
        const firstItem = catList.querySelector("li a");
        if (firstItem) {
            firstItem.classList.add("active-cat");
        }
    } else {
        const categorySlug = path.split("/").filter(Boolean)[1].split("-").join(" ");

        listItems.forEach(item => {
            const link = item.querySelector("a span")
            if(link) {
                let text = link.textContent.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase()
                
                console.log(text, categorySlug)

                if(text == categorySlug) {
                    link.classList.add("active-cat");
                }
            }  
        })
    }
}

addShowAllCat(pandaThemeVars.shopUrl);
setActiveCatColor();