(function () {
  if (!document.body) return;

  // 1) Language from URL
  var m = location.pathname.match(/^\/([a-z]{2}(?:-[a-z0-9]+)?)(\/|$)/i);
  if (!m) return;
  var lang = m[1].toLowerCase();
  if (lang === 'en') return;

  // 2) Product ID from body class
  var pid = null;
  document.body.className.split(/\s+/).forEach(function (c) {
    if (c.indexOf('postid-') === 0) pid = c.replace('postid-', '');
  });
  if (!pid) return;

  // 3) Possible short-description selectors (theme-agnostic)
  var selectors = [
    '.woocommerce-product-details__short-description',
    '.product-short-description',
    '.summary .woocommerce-product-details__short-description',
    '.summary .short-description',
    '.entry-summary .woocommerce-product-details__short-description',
    '.entry-summary p'
  ];

  var target = null;
  for (var i = 0; i < selectors.length; i++) {
    target = document.querySelector(selectors[i]);
    if (target) break;
  }
  if (!target) return;

  // 4) Fetch inline payload via REST
  fetch('/wp-json/wp/v2/product/' + pid)
    .then(r => r.json())
    .then(function (data) {
      if (!data || !data.meta) return;

      var key = '_reeid_wc_tr_' + lang;
      var pl  = data.meta[key];
      if (!pl || !pl.excerpt) return;

      target.innerHTML = pl.excerpt;
      target.classList.add('reeid-inline-excerpt');
    })
    .catch(function(){});
})();
