document.addEventListener('DOMContentLoaded', () => {
	const serviceItems = [...document.querySelectorAll('.service-item__title')];

  const isVisible = (element) => {
    if (!element) {
      return true;
    }

    const style = window.getComputedStyle(element);

    if (style.display === 'none' || style.visibility === 'hidden') {
      return false;
    }

    if (element.style.display === 'none' || element.style.visibility === 'hidden') {
      return false;
    }

    return isVisible(element.parentElement);
  };
  
  // For index
  const visibleItems = serviceItems.filter(isVisible);
	for (let serviceTitle of visibleItems) {
      const service = services.find(s => s.title === serviceTitle.innerText);
      const parent = serviceTitle.parentElement?.querySelector('.service__badges-wrapper');
      const badges = [];
      const regex = /##([^<]+)/g;
      let match;

      while ((match = regex.exec(service.description)) !== null) {
        badges.push(match[1].trim());
      }
  
      for (let badgeText of badges) {
			  const badge = document.createElement('div');
			  badge.classList.add('badge', 'brand');
			  badge.innerText = badgeText;
			  parent.appendChild(badge);
		  }
	 }
	// For detail page
  const urlPathname = window.location.pathname;
  const urlRegex = /\/\d+\/service_detail$/; // Expresión regular para el patrón "/{numero}/service_detail"

  if (urlRegex.test(urlPathname)) {
    const serviceDescriptions = document.querySelectorAll('.service-description__text');
    serviceDescriptions.forEach(element => {
      element.childNodes.forEach(child => {
        const textContent = child.textContent.trim();
        if (textContent.startsWith('##')) {
          child.remove();
        }
			});  
    });
  }
});