const { BaseNodeAdapter, configManager } = require('generalFunctions');
const PumpingStation = require('./specificClass');
const commands = require('./commands');

class nodeClass extends BaseNodeAdapter {
  static DomainClass = PumpingStation;
  static commands = commands;
  // Tick-driven: predicted-volume integrator needs delta-time per second.
  static tickInterval = 1000;
  static statusInterval = 1000;

  buildDomainConfig(uiConfig) {
    return {
      basin: {
        volume: uiConfig.basinVolume,
        height: uiConfig.basinHeight,
        inflowLevel: uiConfig.inflowLevel,
        outflowLevel: uiConfig.outflowLevel,
        overflowLevel: uiConfig.overflowLevel,
        inletPipeDiameter: uiConfig.inletPipeDiameter,
        outletPipeDiameter: uiConfig.outletPipeDiameter,
      },
      hydraulics: {
        refHeight: uiConfig.refHeight,
        minHeightBasedOn: uiConfig.minHeightBasedOn,
        basinBottomRef: uiConfig.basinBottomRef,
        maxInflowRate: uiConfig.maxInflowRate,
        staticHead: uiConfig.staticHead,
        maxDischargeHead: uiConfig.maxDischargeHead,
        pipelineLength: uiConfig.pipelineLength,
        defaultFluid: uiConfig.defaultFluid,
        temperatureReferenceDegC: uiConfig.temperatureReferenceDegC,
      },
      control: {
        mode: uiConfig.controlMode,
        levelbased: {
          minLevel: uiConfig.minLevel,
          startLevel: uiConfig.startLevel,
          stopLevel: uiConfig.stopLevel,
          holdLevel: uiConfig.holdLevel,
          maxLevel: uiConfig.maxLevel,
          // Editor names the field levelCurveType; runtime uses curveType.
          curveType: uiConfig.levelCurveType || uiConfig.curveType,
          logCurveFactor: uiConfig.logCurveFactor,
          enableShiftedRamp: uiConfig.enableShiftedRamp,
          shiftLevel: uiConfig.shiftLevel,
          shiftArmPercent: uiConfig.shiftArmPercent,
          deadZoneKeepAlivePercent: uiConfig.deadZoneKeepAlivePercent,
        },
      },
      safety: {
        enableDryRunProtection: uiConfig.enableDryRunProtection,
        dryRunThresholdPercent: uiConfig.dryRunThresholdPercent,
        enableHighVolumeSafety: uiConfig.enableHighVolumeSafety ?? uiConfig.enableOverfillProtection,
        highVolumeSafetyThresholdPercent: uiConfig.highVolumeSafetyThresholdPercent ?? uiConfig.overfillThresholdPercent,
        enableOverfillProtection: uiConfig.enableOverfillProtection,
        overfillThresholdPercent: uiConfig.overfillThresholdPercent,
        timeleftToFullOrEmptyThresholdSeconds: uiConfig.timeleftToFullOrEmptyThresholdSeconds,
      },
      output: {
        process: uiConfig.processOutputFormat,
        dbase: uiConfig.dbaseOutputFormat,
      },
    };
  }

  // Test-only entrypoint mirroring the basin-docs config-mapping surface.
  // Lets `NodeClass.prototype._loadConfig.call({name:'pumpingStation'}, ui, node)`
  // produce the merged config without instantiating a full Node-RED adapter.
  // Production wiring goes through BaseNodeAdapter; this is a thin shim.
  _loadConfig(uiConfig, node) {
    const cfgMgr = new configManager();
    const name = this.name || 'pumpingStation';
    const domain = nodeClass.prototype.buildDomainConfig.call(this, uiConfig);
    this.defaultConfig = cfgMgr.getConfig(name);
    this.config = cfgMgr.buildConfig(name, uiConfig, node && node.id, domain);
    return this.config;
  }
}

module.exports = nodeClass;
