function extender_update_tag_data(data) {
    let list = '';
    jQuery('.dropdown-list.filters').empty();
    jQuery.each(data, function (index, feature) {
        list += `<div class="dropdown-item" data-name="${feature.tag}">`;
        list += `<label for="${feature.tag}">${feature.human}</label>`;
        list += `<input class="input-tags" type="checkbox" id="${feature.tag}" name="${feature.tag}" value="${feature.human}" ${feature.checked ? 'checked' : ''}>`;
        list += `</div>`;
    });
    jQuery('.dropdown-list.filters').append(list);
}

// Store active dropdowns to prevent memory leaks
let activeDropdowns = [];

// Main initialization function - call this after AJAX updates
// filter checkboxes
function initMultiSelects() {
    // Clean up old event listeners
    activeDropdowns.forEach(cleanup => cleanup());
    activeDropdowns = [];

    const multiSelects = document.querySelectorAll('.multi-select');

    multiSelects.forEach(multiSelect => {
        const trigger = multiSelect.querySelector('.select-trigger');
        const dropdown = multiSelect.querySelector('.dropdown-list');
        const arrow = multiSelect.querySelector('.arrow');
        const selectText = multiSelect.querySelector('.select-text');
        const checkboxes = multiSelect.querySelectorAll('input[type="checkbox"]');
        const selectedList = multiSelect.closest('.dropdown-section').querySelector('.selected-list');
        const placeholder = multiSelect.getAttribute('data-placeholder') || 'Select options...';
        const name = multiSelect.getAttribute('data-name');

        // Store event handlers for cleanup
        const handlers = {
            triggerClick: null,
            dropdownClick: null,
            checkboxChanges: []
        };

        // Toggle dropdown
        handlers.triggerClick = (e) => {
            e.stopPropagation();

            // Close all other dropdowns
            multiSelects.forEach(ms => {
                if (ms !== multiSelect) {
                    ms.querySelector('.dropdown-list').classList.remove('open');
                    ms.querySelector('.select-trigger').classList.remove('open');
                    ms.querySelector('.arrow').classList.remove('open');
                }
            });

            dropdown.classList.toggle('open');
            trigger.classList.toggle('open');
            arrow.classList.toggle('open');
        };

        trigger.addEventListener('click', handlers.triggerClick);

        // Prevent dropdown from closing when clicking inside
        handlers.dropdownClick = (e) => {
            e.stopPropagation();
        }

        // Update selected text and list
        function updateSelection() {
            const selected = Array.from(checkboxes)
                .filter(cb => cb.checked)
                .map(cb => cb.name);

            if (selected.length === 0) {
                selectText.textContent = placeholder;
                selectText.classList.add('placeholder');
            } else if (selected.length === 1) {
                selectText.textContent = selected[0];
                selectText.classList.remove('placeholder');
            } else {
                selectText.innerHTML = `${selected[0]} <span class="selected-count">+${selected.length - 1}</span>`;
                selectText.classList.remove('placeholder');
            }
            function_handler = 'update';
            function_data[name] = selected.toString();
            extender_ajax_handler();
            function_data = {};
        }


        // Listen to checkbox changes
        checkboxes.forEach(checkbox => {
            const changeHandler = function() {
                updateSelection();
            };

            checkbox.addEventListener('change', changeHandler);
            handlers.checkboxChanges.push({ checkbox, handler: changeHandler });
        });

        // Store cleanup function
        activeDropdowns.push(() => {
            trigger.removeEventListener('click', handlers.triggerClick);
            dropdown.removeEventListener('click', handlers.dropdownClick);
            handlers.checkboxChanges.forEach(({ checkbox, handler }) => {
                checkbox.removeEventListener('change', handler);
            });
        });
    });

    // Close all dropdowns when clicking outside
    const documentClickHandler = () => {
        const allMultiSelects = document.querySelectorAll('.multi-select');
        allMultiSelects.forEach(multiSelect => {
            multiSelect.querySelector('.dropdown-list').classList.remove('open');
            multiSelect.querySelector('.select-trigger').classList.remove('open');
            multiSelect.querySelector('.arrow').classList.remove('open');
        });
    };

    document.removeEventListener('click', documentClickHandler);
    document.addEventListener('click', documentClickHandler);
}

// Initialize on page load
document.addEventListener('DOMContentLoaded', function() {
    initMultiSelects();
});