// PumpingStation editor — shared namespace + helpers.
// Loaded first by pumpingStation.html via /pumpingStation/editor/index.js.
// Each sibling module attaches additional members to window.PSEditor.

(function () {
  const ns = window.PSEditor = window.PSEditor || {};

  // Read a numeric value from an input by node-input-<id>; null if blank/NaN.
  ns.fNum = (id) => {
    const v = parseFloat(document.getElementById(`node-input-${id}`)?.value);
    return Number.isFinite(v) ? v : null;
  };

  // Set a numeric input's value, or blank if not finite. Accepts numeric
  // strings (Node-RED's auto-form-binding stores form values as strings).
  ns.setNumberField = (id, val) => {
    const el = document.getElementById(id);
    if (!el) return;
    const num = typeof val === 'number' ? val : parseFloat(val);
    el.value = Number.isFinite(num) ? num : '';
  };

  // Add input + change listeners to a list of node-input-* ids.
  ns.bindRedraw = (ids, handler) => {
    ids.forEach((id) => {
      const el = document.getElementById(`node-input-${id}`);
      if (el) {
        el.addEventListener('input', handler);
        el.addEventListener('change', handler);
      }
    });
  };
})();
