// rates.js — display reaction rates (theoretical model) with stoichiometry-locked rounding
// Reaction: A + B → 2Γ
// Convention shown in UI:
//   υA = k[A][B]   (positive number, consumption speed)
//   υB = k[A][B]
//   υΓ = 2·k[A][B]
//
// Key requirement (Νίκος):
// - Round ONE rate (υA) and compute the others stoichiometrically from it.

(function () {
  function round3(x) {
    // avoid "-0.000"
    const r = Number((x || 0).toFixed(3));
    return Math.abs(r) < 0.0005 ? 0 : r;
  }

  function getK(T) {
    if (window.Kinetics && typeof window.Kinetics.kFromT === "function") {
      return window.Kinetics.kFromT(T);
    }
    // fallback: +10 K ~ double
    const K_REF = 0.2, T_REF = 300;
    return K_REF * Math.pow(2, (T - T_REF) / 10);
  }

  function update() {
    const sp = window.simulationParams || {};
    const V = Math.max(1e-6, +sp.V || 0.5); // L
    const T = +sp.T || 300;                 // K

    const nA = Math.max(0, +sp.nA || 0);    // mol
    const nB = Math.max(0, +sp.nB || 0);    // mol
    const nG = Math.max(0, +sp.nG || 0);    // mol

    const CA = nA / V;
    const CB = nB / V;
    // const CG = nG / V; // not needed for v here

    const k = getK(T);           // L/(mol·s)

    
const CG = nG / V;

const kf = getK(T) * (window.kineticsState?.catalyst ? 5 : 1);
const kr = kf / KC_EQ;


const vf = kf * CA * CB;
const vr = kr * CG * CG;
const vnet = vf - vr;

const vA = round3(vnet);
const vB = vA;
const vG = round3(2 * vA);






    const elA = document.getElementById("rateA");
    const elB = document.getElementById("rateB");
    const elG = document.getElementById("rateG");

    if (elA) elA.textContent = `υA = ${vA.toFixed(3)} mol/L·s`;
    if (elB) elB.textContent = `υB = ${vB.toFixed(3)} mol/L·s`;
    if (elG) elG.textContent = `υΓ = ${vG.toFixed(3)} mol/L·s`;
  }

  // Compatibility no-ops (some versions of particles.js call these)
  function push() {}
  function maybeCompute() {}

  window.Rates = { reset: update, push, maybeCompute };

  setInterval(update, 100);
  window.addEventListener("load", update);
})();
