/* === HostX: calculează corect prețul afisat din prețul tăiat + reducere === */
(function(){
  function parseNumSmart(str){
    if(!str) return NaN;
    str = String(str).replace(/\u00A0/g,' ').trim();
    const digits = str.replace(/[^\d.,\-]/g,'');
    const hasC = digits.indexOf(',')>-1, hasD = digits.indexOf('.')>-1;
    let s = digits;
    if(hasC && hasD){
      if(digits.lastIndexOf(',') > digits.lastIndexOf('.')){ s = digits.replace(/\./g,'').replace(',', '.'); }
      else { s = digits.replace(/,/g,''); }
    }else if(hasC && !hasD){ s = digits.replace(/\./g,'').replace(',', '.'); }
    else { s = digits.replace(/,/g,''); }
    return parseFloat(s);
  }

  function detectFormat(txt){
    txt = (txt||'').replace(/\u00A0/g,' ').trim();
    const m = txt.match(/[\d.,\s]+/); const num = m?m[0]:'';
    const symbol = txt.replace(num,'').trim(); // ce rămâne e simbolul (ex: €, RON)
    const prefix = !!symbol && txt.startsWith(symbol);
    let decimal='.', thousand=',';
    if(num.includes(',') && num.includes('.')){
      decimal  = (num.lastIndexOf(',')>num.lastIndexOf('.')) ? ',' : '.';
      thousand = (decimal===',') ? '.' : ',';
    }else if(num.includes(',')){
      const tail = num.split(',').pop();
      decimal  = (tail.length===2) ? ',' : '.';
      thousand = (decimal===',') ? '.' : ',';
    }
    return {symbol, prefix, decimal, thousand};
  }

  function formatMoneyLike(n, example){
    const fmt = detectFormat(example);
    const fixed = Number(n).toFixed(2);
    let [i,d] = fixed.split('.');
    i = i.replace(/\B(?=(\d{3})+(?!\d))/g, fmt.thousand);
    const numStr = d ? i + fmt.decimal + d : i;
    return fmt.symbol ? (fmt.prefix ? (fmt.symbol + numStr) : (numStr + ' ' + fmt.symbol)) : numStr;
  }

  function recalc(scope){
    scope.querySelectorAll('.pricing-vps-sec-lower').forEach(function(sec){
      const struck = sec.querySelector('.striked-price');
      const badge  = sec.querySelector('.price_V1_discount-discount');
      const strong = sec.querySelector('strong');
      if(!struck || !badge || !strong) return;

      const spanTerm = strong.querySelector('span'); // partea /luna etc.
      const pricePart = spanTerm ? strong.innerHTML.split(spanTerm.outerHTML)[0] : strong.textContent;

      const rawOld = parseNumSmart(struck.textContent);
      if(!(rawOld>0)) return;

      // întâi încercăm să citim data-*; dacă lipsesc, încercăm să extragem cifrele din textul badge-ului
      let type = badge.dataset.strikedType || '0';
      let amt  = parseNumSmart(badge.dataset.strikedAmount || '') ;
      if(isNaN(amt) || !(amt>0)){
        // fallback: caută număr în text (ex: "Economisești 35%")
        const m = (badge.textContent||'').match(/(\d+(?:[.,]\d+)?)\s*%?/);
        if(m){ amt = parseNumSmart(m[1]); }
      }
      if(!(amt>0)) return;

      let newPrice = (type === '1') ? (rawOld - amt) : (rawOld * (1 - amt/100));
      if(newPrice < 0) newPrice = 0;

      const display = formatMoneyLike(newPrice, struck.textContent);
      strong.innerHTML = display + (spanTerm ? spanTerm.outerHTML : '');
    });
  }

  document.addEventListener('DOMContentLoaded', function(){
    const root = document;
    recalc(root);

    // recalc după schimbarea taburilor
    const tabs = root.querySelectorAll('#changeBillingCycle a');
    tabs.forEach(function(a){
      a.addEventListener('click', function(){ setTimeout(function(){ recalc(root); }, 80); });
    });
  });
})();
