/**
 * Custom JS for theme elements
 */

/**
 * Wocommerce active class for category list
 */
let boldPhotographyblocksurl = window.location.href;
const boldPhotographyblockscatLink = document.querySelectorAll(
  ".wc-block-product-categories-list li a"
);
boldPhotographyblockscatLink.forEach((item) => {
  if (item.href === boldPhotographyblocksurl) {
    item.classList.add("active");
  }
});

/*
    Add class in body when search clicked
*/
let boldPhotographyblockssearchBtn = document.querySelector(".search-controller svg.search");

if (boldPhotographyblockssearchBtn !== null) {
  boldPhotographyblockssearchBtn.addEventListener("click", function (e) {
    document.body.classList.remove("open-social");
    document.body.classList.add("open-search");
    document.body.addEventListener("click", function () {
      document.body.classList.remove("open-search");
    });

    let boldPhotographyblockssearchContainer = document.querySelector(".search-container");
    boldPhotographyblockssearchContainer.addEventListener("click", function (e) {
      e.stopPropagation();
    });

    var boldPhotographyblockssearchInput = document.querySelector(".wp-block-search__input");
    window.setTimeout(() => boldPhotographyblockssearchInput.focus(), 0);
  });
}

var boldPhotographyblockssearchBtnClose = document.querySelector(
  ".search-controller svg.cross"
);

if (boldPhotographyblockssearchBtnClose !== null) {
  boldPhotographyblockssearchBtnClose.addEventListener("click", function (e) {
    document.body.classList.remove("open-search");

  });
}


/*
    Add class in body when social clicked
*/
let boldPhotographyblockssocialBtn = document.querySelector(".social-controller svg.social");

if (boldPhotographyblockssocialBtn !== null) {
  boldPhotographyblockssocialBtn.addEventListener("click", function (e) {
    document.body.classList.remove("open-search");
    document.body.classList.add("open-social");
    document.body.addEventListener("click", function () {
      document.body.classList.remove("open-social");
    });

    let boldPhotographyblockssocialContainer = document.querySelector(".social-container");
    boldPhotographyblockssocialContainer.addEventListener("click", function (e) {
      e.stopPropagation();
    });

    var boldPhotographyblockssocialInput = document.querySelector(".wp-block-social__input");
    window.setTimeout(() => boldPhotographyblockssocialInput.focus(), 0);
  });
}

var boldPhotographyblockssocialBtnClose = document.querySelector(
  ".social-controller svg.cross"
);

if (boldPhotographyblockssocialBtnClose !== null) {
  boldPhotographyblockssocialBtnClose.addEventListener("click", function (e) {
    document.body.classList.remove("open-social");

  });
}

/*
    Add blinker on input field when active
*/
let boldPhotographyblocksblinkerField = document.querySelector(".social-controller svg.search");

if (boldPhotographyblocksblinkerField !== null) {
  boldPhotographyblocksblinkerField.addEventListener("click", function () {
    var boldPhotographyblockssearchInput = document.querySelector(".wp-block-search__input");
        window.setTimeout(() => boldPhotographyblockssearchInput.focus(), 0);
  });
}

// ADD CUSTOM CSS FOR SHOWING CURRENT_MENU_ITEM CLASS

document.body.querySelectorAll('.wp-block-navigation').forEach(function (navBlock) {
  if (navBlock !== null) {
    navBlock.querySelectorAll('[href="' + window.location.href + '"]').forEach(function (navActiveLink) {
      navActiveLink.parentNode.classList.add('current-nav-item');
    });
  }
});


/**
 * Header Search Show/Hide js
 */

let searchContainer = document.querySelectorAll('.wp-primary-search');
searchContainer.forEach((searchForm) => {
  let body = document.body;
  if (searchForm !== null) {
    let toggleIcon = searchForm.querySelector('.wp-search-toggle-container')
    let searchInnerContainer = searchForm.querySelector('.wp-search-container')
    let searchInput = searchForm.querySelector('.wp-block-search__input')
    let fseSearch = document.querySelectorAll('.search-controller .svg, .social-controller .svg')
    let body = document.body;

    fseSearch.forEach((searchButton) => {
      searchButton.addEventListener('click', function () {
        console.log('clicked')
        body.classList.remove('show-search')
        searchForm.classList.remove('toggled-on')
      })
    })

    toggleIcon.addEventListener('click', function () {
      searchForm.classList.toggle('toggled-on')
      body.classList.toggle('show-search')
      searchInput.focus()
    })

    document.addEventListener('click', function (e) {
      if (!searchInput.contains(e.target) && !searchInnerContainer.contains(e.target) && !toggleIcon.contains(e.target) && searchForm.classList.contains('toggled-on')) {
        searchForm.classList.remove('toggled-on')
        body.classList.remove('show-search')
      }
    })
  }
})

// Function to Add/remove class for mobile Woocommerce sidebar Widgets/filters
const toggleFunc = (targetElem) => {
  let filterButtons = document.querySelectorAll(targetElem);

  filterButtons.forEach((filterButton) => {
    if (filterButton !== null) {
      filterButton.addEventListener("click", () => {
        if (filterButton.classList.contains("toggled-on")) {
          // if has 'toggled-on' class remove class
          filterButton.classList.remove("toggled-on");
        } else {
          // otherwise add 'toggled-on' class
          filterButton.classList.add("toggled-on");
        }
      });
    }
  });
};

toggleFunc(".btn-mobile-filters");

// Plus minus function added on product input counter
function elevateBizQtyChange() {
  let qtyWrap = document.querySelectorAll('.quantity');
  qtyWrap.forEach((wrap) => {
    let qNav = document.createElement('div');
    let qUp = document.createElement('div');
    let qDown = document.createElement('div');
    let input = wrap.querySelectorAll('.qty');
    qUp.innerHTML = '+';
    qDown.innerHTML = '-';
    qNav.appendChild(qUp);
    qNav.appendChild(qDown);
    wrap.appendChild(qNav)
    qUp.setAttribute('class', 'quantity-button quantity-up');
    qDown.setAttribute('class', 'quantity-button quantity-down');
    max = 99999;
    let min = '';
    input.forEach((inputItem) => {
      min = inputItem.getAttribute('min');
      if (qtyWrap.length > 1) {
        // Set default value to 0
        inputItem.value = 0;
      }
      qNav.setAttribute('class', 'quantity-nav');
      let btnUp = wrap.querySelectorAll('.quantity-up');
      btnUp.forEach((btnItem) => {
        btnItem.addEventListener('click', function () {
          let oldValue = parseFloat(inputItem.value);
          if (oldValue >= max) {
            var newVal = oldValue;
          } else {
            var newVal = oldValue + 1;
          }
          inputItem.value = newVal
          var element = document.createEvent('HTMLEvents');
          var event = new Event('change', {
            bubbles: true
          });
          inputItem.dispatchEvent(event);
          return event;
        })
      })
      let btnDown = wrap.querySelectorAll('.quantity-down');
      btnDown.forEach((btnItem) => {
        btnItem.addEventListener('click', function () {
          let oldValue = parseFloat(inputItem.value);
          if (oldValue >= max) {
            var newVal = oldValue;
          } else if (oldValue <= 0) {
            var newVal = 0;
          } else {
            var newVal = oldValue - 1;
          }
          inputItem.value = newVal
          var element = document.createEvent('HTMLEvents');
          var event = new Event('change', {
            bubbles: true
          });
          inputItem.dispatchEvent(event);
          return event;
        })
      })
    })
  })
}
elevateBizQtyChange();

function elevateBizCartUpdate() {
  let btnTrigger = document.querySelector('button[name="update_cart"]');
  if (btnTrigger !== null) {
    btnTrigger.addEventListener('click', function () {
      setTimeout(function () {
        elevateBizQtyChange();
      }, 5000);
      setTimeout(function () {
        elevateBizCartUpdate();
      }, 5000);
    });
  }
}
elevateBizCartUpdate();
