// (c) 2023 W2 Co.,Ltd.
window.addEventListener('load', function () {
  /* Versatile modal behavior */
  window.functionLib.scrollBehaviorScope('js-scrollBehaviorScope', 's-header', -10);
  /* for Mobile */
  const accordionTrigger = document.querySelectorAll('.js-accordionTrigger');
  accordionTrigger.forEach(function (e) {
    e.addEventListener('click', function () {
      if (e.getAttribute('data-activeAccordion') == 'inactive') {
        e.setAttribute('data-activeAccordion', 'active');
      } else if (e.getAttribute('data-activeAccordion') == 'active') {
        e.setAttribute('data-activeAccordion', 'inactive');
      }
    });
  });
  /* for Desktop */
  const detectIntersectTarget = document.querySelectorAll('.js-intersectTarget');
  const options = {
    root: null,
    rootMargin: '0% 0% -80% 0%',
    threshold: 0
  };
  const observer = new IntersectionObserver(intersectedFunc, options);
  detectIntersectTarget.forEach(function (elm) {
    observer.observe(elm);
  });
  function intersectedFunc(entries) {
    entries.forEach(function (entry) {
      if (entry.isIntersecting) {
        if (clickFlg == 0) {
          const currentActiveIndex = document.querySelector('.c-userNav-link[data-index="active"]');
          currentActiveIndex.setAttribute('data-index', 'inactive');

          const newActiveIndex = document.querySelector(`a[href='#${entry.target.id}']`);
          newActiveIndex.setAttribute('data-index', 'active');
        }
      }
    });
  }
  // Processing when the index is clicked
  let clickFlg = 0;
  const indexList = document.querySelectorAll('.c-userNav-link');
  indexList.forEach(function(e) {
    e.addEventListener('click', function() {
      clickFlg = 1;
      indexList.forEach(function(changeAttrElm) {
        changeAttrElm.setAttribute('data-index', 'inactive');
      });
      e.setAttribute('data-index', 'active');
      window.setTimeout(function() {
        clickFlg = 0;
      }, 500);
    });
  });
});
