/* Minification failed. Returning unminified contents.
(1,20): run-time error CSS1031: Expected selector, found '='
(1,20): run-time error CSS1025: Expected comma or open brace, found '='
(3,4): run-time error CSS1031: Expected selector, found '('
(3,4): run-time error CSS1025: Expected comma or open brace, found '('
(41,10): run-time error CSS1031: Expected selector, found 'handleResize('
(41,10): run-time error CSS1025: Expected comma or open brace, found 'handleResize('
(55,8): run-time error CSS1030: Expected identifier, found 'addEventListener('
(55,8): run-time error CSS1031: Expected selector, found 'addEventListener('
(55,8): run-time error CSS1025: Expected comma or open brace, found 'addEventListener('
(56,1): run-time error CSS1019: Unexpected token, found 'handleResize('
(56,14): run-time error CSS1019: Unexpected token, found ')'
(58,10): run-time error CSS1031: Expected selector, found 'addTextToItems('
(58,10): run-time error CSS1025: Expected comma or open brace, found 'addTextToItems('
(73,10): run-time error CSS1031: Expected selector, found 'checkScreenWidth('
(73,10): run-time error CSS1025: Expected comma or open brace, found 'checkScreenWidth('
(80,10): run-time error CSS1031: Expected selector, found 'removeTextFromItems('
(80,10): run-time error CSS1025: Expected comma or open brace, found 'removeTextFromItems('
(84,8): run-time error CSS1030: Expected identifier, found 'addEventListener('
(84,8): run-time error CSS1031: Expected selector, found 'addEventListener('
(84,8): run-time error CSS1025: Expected comma or open brace, found 'addEventListener('
(85,1): run-time error CSS1019: Unexpected token, found 'checkScreenWidth('
(85,18): run-time error CSS1019: Unexpected token, found ')'
 */
const analogsBlock = document.querySelector('.analogues__inner');

if (analogsBlock) {
    const sortButton = document.querySelector('.analogues__sort');
    const sortTitle = document.querySelector('.sort-title');
    const sortOptions = document.querySelector('.analogues__sort-select');
    const topSelect = document.querySelector('.analogues__top-select');

    if (sortButton) {
        sortButton.addEventListener('click', () => {
            topSelect.classList.toggle('active');
        });
    }

    const options = document.querySelectorAll('.sort-option');
    options.forEach(option => {
        option.addEventListener('click', () => {
            sortTitle.textContent = option.dataset.value;
            topSelect.classList.remove('active');
        });
    });

    const analoguesFormTitle = document.querySelector('.analogues__form h3');
    const analoguesForm = document.querySelector('.analogues__form');
    if (analoguesFormTitle) {
        analoguesFormTitle.addEventListener('click', () => {
            analoguesForm.classList.toggle('active');
        });
    }

    document.addEventListener('click', function (event) {
        if (!topSelect.contains(event.target)) {
            topSelect.classList.remove('active');
        }
        if (!document.querySelector('.analogues__form').contains(event.target)) {
            analoguesForm.classList.remove('active');
        }
    });
}

function handleResize() {
    const form = document.querySelector('.analogues__form');
    const content = document.querySelector('.analogues__content');
    const topBlock = document.querySelector('.analogues__top-block');
    if (window.innerWidth < 768 && topBlock) {
        if (!topBlock.contains(form)) {
            topBlock.appendChild(form);
        }
    } else {
        if (content && !content.contains(form)) {
            content.insertBefore(form, content.firstChild);
        }
    }
}
window.addEventListener('resize', handleResize);
handleResize();

function addTextToItems() {
    const blocks = document.querySelectorAll('.product__table-block');
    blocks.forEach(block => {
        const items = block.querySelectorAll('.product__table-item');
        const labels = ['СКЛАД:', 'ДОСТУПНО:', 'ЧАС ПОСТАВКИ:', 'ЦІНА:'];
        items.forEach((item, index) => {
            let label = document.createElement('p');
            label.classList.add('product__table-name');
            label.textContent = labels[index];
            if (!item.querySelector('.product__table-name')) {
                item.insertBefore(label, item.firstChild);
            }
        });
    });
}
function checkScreenWidth() {
    if (window.innerWidth < 768) {
        addTextToItems();
    } else {
        removeTextFromItems();
    }
}
function removeTextFromItems() {
    const labels = document.querySelectorAll('.product__table-name');
    labels.forEach(label => label.remove());
}
window.addEventListener('resize', checkScreenWidth);
checkScreenWidth();

