console.log('SF Accordions Init - 1/3');

onWindowLoad(()=>{
  console.log('SF Accordions Loading - 2/3');

  /* eslint-disable */
  //
  //   █████╗  ██████╗ ██████╗ ██████╗ ██████╗ ██████╗ ██╗ ██████╗ ███╗   ██╗███████╗
  //  ██╔══██╗██╔════╝██╔════╝██╔═══██╗██╔══██╗██╔══██╗██║██╔═══██╗████╗  ██║██╔════╝
  //  ███████║██║     ██║     ██║   ██║██████╔╝██║  ██║██║██║   ██║██╔██╗ ██║███████╗
  //  ██╔══██║██║     ██║     ██║   ██║██╔══██╗██║  ██║██║██║   ██║██║╚██╗██║╚════██║
  //  ██║  ██║╚██████╗╚██████╗╚██████╔╝██║  ██║██████╔╝██║╚██████╔╝██║ ╚████║███████║
  //  ╚═╝  ╚═╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚═╝  ╚═╝╚═════╝ ╚═╝ ╚═════╝ ╚═╝  ╚═══╝╚══════╝
  //  ===============================================================================
  //  Sagefrog - AN, 2021-09-19
  //  ===============================================================================
  //  Purpose
  //        Works with accordions default module to provide an accurate
  //        max-height when expanding an accordion field
  //  ===============================================================================
  //  Input :  (obj) jquery element
  //  Output : (int) pixels
  (function (sf_accordions, $, undefined) {
    sf_accordions.objects = {
      header: document.querySelector('header'),
      wrapper_scroll_menu: document.querySelector('.wrapper_scroll_menu'),
      last_scrolled: 0,
      last_headerchange_scrolled: null
    };

    sf_accordions.getHeightOfChildren = function(el){
      _height = false;
  
      el.children().map(function(i, val){
        _height += val.offsetHeight;
      })
  
      return _height;
    };
  }(window.sf_accordions = window.sf_accordions || {}, jQuery));
  /* eslint-enable */
  
  $(document).ready(function(){
    // data-accordion_parent > accordion_header, accordion_content 
    $('[data-accordion_control]').on('click',function(e){
      e.preventDefault();
      var card = $(this).closest('[data-accordion_parent]');1
      var expand_el = card.find('[data-accordion_content]:eq(0)');
      let hide_on_active = [
        ".preview"
      ];
    
      // Minimize
      //  if card is active, minimize
      if(card.hasClass('active')) {
        // clear timeout func(max-height:5000)
        //    wouldnt want to inactivate the card only to set the max-height to 5000
        let _timeout_id = (parseInt(expand_el.attr('acrdn_timeout'))!==0)?expand_el.attr('acrdn_timeout'):false;
        if(_timeout_id){ clearTimeout(_timeout_id); }
        //  on minimize, find current height so we're not minimizing from max-height 5000 (jarring fast minimize)
        var _prev_transition = expand_el.css('transition'); //  save original transition
        expand_el.css('transition','0s all'); //  we set transition to 0s before setting the max-height to current height
        var _actual_height = sf_accordions.getHeightOfChildren(expand_el)+"px";  
        //  this means we don't transition from the transitioning max-height value (between actual and 5000)
        expand_el.css('max-height',_actual_height);
        expand_el.css('max-height'); // for some reason , without this, it doesn't set max_height to actual quick enough
        expand_el.css('transition',_prev_transition);
        //  transition is back to original and max-height is set to the current height, ready to minimize
        expand_el.css('max-height',0);
        card.removeClass('active');
        _timeout_id = setTimeout(function(){
          if(hide_on_active.length) {
            hide_on_active.forEach(el_selector => {
              card.find(el_selector).css('display','unset');
            });
            expand_el.css('min-height', 0);
          }
        },280);
        expand_el.attr('acrdn_timeout',_timeout_id);
      }
      // Expand
      //  if card is inactive, set a smart height
      else {
        var _height = sf_accordions.getHeightOfChildren(expand_el); // get height of elements to show
        expand_el.css('max-height',_height); // set max height
        card.addClass('active'); // activate card
        if(hide_on_active.length) {
          console.log("EXPAND");
          hide_on_active.forEach(el_selector => {
            let min_height = card.find(el_selector).closest('p')[0].offsetHeight;
            console.log(`el_selector: ${el_selector}`);
            console.log(min_height);
            expand_el.css('min-height',`${min_height}px`)
            console.log(expand_el);
            card.find(el_selector).css('display','none');
          });
        }
        let _timeout_id = setTimeout(function(){
          expand_el.css('max-height',5000); // prevents the need from finding max-height on resize
        },500);
        expand_el.attr('acrdn_timeout',_timeout_id);
      }
    })
  })

  // // on scroll with lag prevention  
  // $(document).on('scroll', function () {
  //   if (true || Date.now() + 50 > sf_accordions.objects.last_scrolled) {
  //       function getHeaderHeight( input = "",element = sf_accordions.objects.header) { return element.offsetHeight + input; }
        
  //       sf_accordions.objects.wrapper_scroll_menu.style.setProperty('--header_height', getHeaderHeight('px'));
  //       sf_accordions.objects.last_scrolled = Date.now();
  //       sf_accordions.objects.last_headerchange_from_scrolled = Date.now();
  //   }
  // });
  
  
  console.log('SF Accordions Loaded - 3/3');
})