/**
 * Custom JS for theme elements
 */

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

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

if (petPalsisearchBtn !== null) {
  petPalsisearchBtn.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 petPalsisearchContainer = document.querySelector(".search-container");
    petPalsisearchContainer.addEventListener("click", function (e) {
      e.stopPropagation();
    });

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

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

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

  });
}


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

if (petPalsisocialBtn !== null) {
  petPalsisocialBtn.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 petPalsisocialContainer = document.querySelector(".social-container");
    petPalsisocialContainer.addEventListener("click", function (e) {
      e.stopPropagation();
    });

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

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

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

  });
}

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

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