function kkWpInvoicesSelectList() {
    const wrapper = document.querySelector(".kkwi-inv-preview-widget .kkwi-inv-preview--wrapper");
    const selectBtn = wrapper.querySelector(".kkwi-inv-preview--select-btn");
    const searchInp = wrapper.querySelector(".kkwi-inv-preview--search input");
    const options = wrapper.querySelector(".kkwi-inv-preview--options");

    searchInp.addEventListener("keyup", () => {
        let no_results = true;
        let searchWord = searchInp.value.toLowerCase();
        options.querySelector('.kkwi-inv-preview--no-invoices').classList.add('hidden');
        
        if(searchWord.trim() == '') {            
            let list = options.querySelectorAll("li");
            list.forEach((item) => {
                item.classList.remove("hidden");
            })

            options.querySelector('.kkwi-inv-preview--no-invoices').classList.add('hidden');
            return;
        }

        let list = options.querySelectorAll("li");
        let countNotFound = 0;
        list.forEach((item) => {
            if(!item.classList.contains('kkwi-inv-preview--no-invoices')) {
                let linkText = item.querySelector('a').textContent.trim();
                if(!linkText.startsWith(searchWord)) {
                    countNotFound++;
                    item.classList.add("hidden");
                } else {
                    countNotFound--;
                    item.classList.remove("hidden");
                }
            }
        })

        no_results = countNotFound === list.length - 1;
        if(no_results) {
            options.querySelector('.kkwi-inv-preview--no-invoices').classList.remove('hidden');
        }
    });

    selectBtn.addEventListener("click", () => wrapper.classList.toggle("active"));
}

kkWpInvoicesSelectList();