import { dsButtonToggle } from '@bonniernews/dn-design-system-web/components/button/button-interactions.js';

export { dsListItem, dsListItemAll };

function dsListItem(listElements = []) {
  if (!listElements.length) return;
  listElements.forEach((listEl) => {
    if (listEl.classList.contains('ds-list-item--js-handled')) return;
    listEl.classList.add('ds-list-item--js-handled');
    if (listEl.classList.contains('ds-list-item--toggle')) {
      dsListItemToggle(listEl);
    } else if (listEl.classList.contains('ds-list-item--accordion')) {
      dsListItemAccordion(listEl);
    }
  });
}

function dsListItemAccordion(accordionEl) {
  accordionEl.addEventListener('click', () => {
    accordionEl.classList.toggle('ds-list-item--accordion-active');
  });
}

function dsListItemToggle(toggleEl) {
  const toggleElements = Array.from(toggleEl.getElementsByClassName('ds-btn--toggle'));
  dsButtonToggle(toggleElements);
}

function dsListItemAll() {
  const listElements = Array.from(document.getElementsByClassName('ds-list-item__content'));
  dsListItem(listElements);
}
