(function($) {
  function addDataLabelsToTables() {
    const tables = document.querySelectorAll('table');
    if (tables.length === 0) {
      return;
    }
    tables.forEach(table => {
      let headers = [];
      const thead = table.querySelector('thead');
      if (thead) {
        // Get headers from <thead>
        headers = Array.from(thead.querySelectorAll('th')).map(th => th.innerText.trim());
      } else {
        // Get headers from the first row if no <thead>
        const firstRow = table.querySelector('tr');
        if (firstRow) {
          headers = Array.from(firstRow.querySelectorAll('td, th')).map(cell => cell.innerText.trim());
        }
      }

      // Add data labels to each <td>
      table.querySelectorAll('tr').forEach(tr => {
        const cells = tr.querySelectorAll('td');
        cells.forEach((td, index) => {
          const headerText = headers[index] || '';
          td.setAttribute('data-label', headerText);
        });
      });
    });
  }

  function processH2Elements() {
    const h2Elements = document.querySelectorAll('.grid-item h2');
    for (let i = 0; i < h2Elements.length - 1; i++) {
      const currentH2 = h2Elements[i];
      const nextH2 = h2Elements[i + 1];
      const currentTextLength = currentH2.textContent.trim().length;
      const nextTextLength = nextH2.textContent.trim().length;
      if (currentTextLength <= 12 && nextTextLength <= 12) {
        currentH2.closest('.grid-item').classList.add('short-text');
        nextH2.closest('.grid-item').classList.add('short-text');
        i++;
      }
    }
  }

  addDataLabelsToTables();
  processH2Elements();
})(jQuery);
