document.addEventListener("DOMContentLoaded", function () {
  accordeonInit({
    accordeonBlockSelector: '.accordeon_block', // Класс блока с аккордеоном
    isBind: false, // Активна только одна вкладка (на которой был клик) (true - да / false - нет)
    showFirstItem: false // Открыть первую вкладку (true - да / false - нет)
  });
});

function accordeonInit({ accordeonBlockSelector = '.accordeon_block', isBind = false, showFirstItem = false }) {
  const accordeonBlocks = document.querySelectorAll(accordeonBlockSelector);

  accordeonBlocks.forEach(accordeonBlock => {
    const acItems = accordeonBlock.querySelectorAll('.accordeon_item');

    if (!acItems.length) return;

    // Добавляем обертку для контента
    acItems.forEach(item => {
      const contentWrapper = document.createElement('div');
      contentWrapper.classList.add('ac_content_wrapper');
      contentWrapper.style.display = 'none';

      // Перемещаем все последующие элементы, пока не встретим новый .accordeon_item
      let sibling = item.nextElementSibling;
      while (sibling && !sibling.classList.contains('accordeon_item')) {
        contentWrapper.appendChild(sibling);
        sibling = item.nextElementSibling;
      }

      item.appendChild(contentWrapper);

      const header = item.querySelector('.f-header');
      header.addEventListener('click', function () {
        const acItemContent = contentWrapper;

        if (isBind) {
          // Закрываем другие элементы, если режим isBind включен
          acItems.forEach(otherItem => {
            if (otherItem !== item) {
              otherItem.classList.remove('active');
              const otherContent = otherItem.querySelector('.ac_content_wrapper');
              otherContent.style.display = 'none';
            }
          });
        }

        // Переключаем класс active и показываем/скрываем контент
        item.classList.toggle('active');
        if (acItemContent.style.display === 'none') {
          acItemContent.style.display = 'block';
        } else {
          acItemContent.style.display = 'none';
        }
      });
    });

    // Открыть первый элемент, если опция включена
    if (showFirstItem && acItems.length > 0) {
      const firstAcItem = acItems[0];
      firstAcItem.classList.add('active');
      const firstContent = firstAcItem.querySelector('.ac_content_wrapper');
      firstContent.style.display = 'block';
    }
  });
}