/* ProofWarden — Catalog config preview (progressive enhancement).
 *
 * With JS on, each test's config panel shows a live "effective:" preview of the
 * overlay as you edit, and re-computes it after the native "Reset to defaults"
 * button restores the committed defaults. With JS off, the form + reset button
 * still work exactly the same — this only adds the read-only preview line.
 */
(function () {
  "use strict";
  var panels = document.querySelectorAll(".config-panel");
  if (!panels.length) return;

  panels.forEach(function (panel) {
    var inputs = panel.querySelectorAll("[name^='cfg.']");
    var wrap = panel.querySelector(".pw-cfg-effective");
    var out = panel.querySelector(".pw-cfg-eff");
    if (!inputs.length || !wrap || !out) return;

    function render() {
      var parts = [];
      inputs.forEach(function (inp) {
        parts.push(inp.getAttribute("name").slice(4) + "=" + inp.value); // strip "cfg."
      });
      out.textContent = parts.length ? parts.join(" ") : "(defaults)";
    }

    inputs.forEach(function (inp) {
      inp.addEventListener("input", render);
      inp.addEventListener("change", render);
    });
    // the native reset fires before values revert — recompute on the next tick
    var form = panel.closest("form");
    if (form) form.addEventListener("reset", function () { setTimeout(render, 0); });

    wrap.hidden = false;
    render();
  });
})();
