import documentReady from '@assets/js/documentReady';

documentReady(() => {
  const category = document.querySelector('.js-aside-category');

  if (category) {
    const categoryContainer = category.querySelectorAll('.js-accordion-control');

    for (const categoryContainerItem of categoryContainer) {
      categoryContainerItem.addEventListener('click', event => {
        event.preventDefault();
        event.stopPropagation();
        handleAccordions(categoryContainerItem, category);
      });
    }
  }
});

function handleAccordions(element, context) {
  const accordionContentList = context.querySelectorAll('.js-accordion-content');
  const selectedCategory = element.parentElement.querySelector('.js-accordion-content');

  for (const item of accordionContentList) {
    if (item === selectedCategory && item.style.display === 'none') {
      item.setAttribute('style', 'display: block;');
    } else {
      item.setAttribute('style', 'display: none;');
    }
  }
}
