import { property, state } from "lit/decorators.js";
import { computeAtSetpoint, getSetpointAnimationDurationMs } from "./setpoint.js";
var __defProp = Object.defineProperty;
var __decorateClass = (decorators, target, key, kind) => {
  var result = void 0;
  for (var i = decorators.length - 1, decorator; i >= 0; i--)
    if (decorator = decorators[i])
      result = decorator(target, key, result) || result;
  if (result) __defProp(target, key, result);
  return result;
};
function SetpointMixin(superClass, options) {
  const {
    defaultDeadband = 2,
    defaultZeroDeadband = 0.5,
    angularWraparound = false
  } = options ?? {};
  class SetpointMixinClass extends superClass {
    constructor() {
      super(...arguments);
      this.atSetpoint = false;
      this.touching = false;
      this.autoAtSetpoint = true;
      this.autoAtSetpointDeadband = defaultDeadband;
      this.setpointAtZeroDeadband = defaultZeroDeadband;
      this.setpointOverride = false;
      this.animateSetpoint = false;
    }
    /** Public getter for renderers/wrappers to access the departing value. */
    get departingNewSetpoint() {
      return this._departingNewSetpoint;
    }
    // ------------------------------------------------------------------
    // Computation (replaces per-instrument atSetpointCalc methods)
    // ------------------------------------------------------------------
    /**
     * Compute whether the current value is at the setpoint.
     *
     * Delegates to the unified `computeAtSetpoint()` from `setpoint.ts`,
     * which standardizes the behavior across all instrument types:
     * - Inclusive deadband comparison (`<=`)
     * - `touching` guard (returns false while touching)
     * - `Number.isFinite()` deadband validation
     * - Optional 360° angular wraparound
     *
     * @param currentValue - The instrument's current value
     * @returns Whether `currentValue` is within deadband of the setpoint
     */
    computeAtSetpoint(currentValue) {
      return computeAtSetpoint({
        value: currentValue,
        setpoint: this.setpoint,
        touching: this.touching,
        auto: this.autoAtSetpoint,
        deadband: this.autoAtSetpointDeadband,
        atSetpointManual: this.atSetpoint,
        angularWraparound
      });
    }
    /**
     * Override point for subclasses that need to run logic in `willUpdate`.
     * Always call `super.willUpdate(changed)` to preserve mixin behavior.
     *
     * Detects "confirm" transitions (newSetpoint defined → undefined) and
     * stores the departing value for animation purposes.
     */
    willUpdate(changed) {
      super.willUpdate(changed);
      if (changed.has("newSetpoint") && this.animateSetpoint) {
        const prev = changed.get("newSetpoint");
        if (prev !== void 0 && this.newSetpoint === void 0) {
          this._departingNewSetpoint = prev;
          clearTimeout(this._animationTimer);
          const duration = getSetpointAnimationDurationMs(this);
          this._animationTimer = setTimeout(() => {
            this._departingNewSetpoint = void 0;
          }, duration);
        }
      }
    }
    disconnectedCallback() {
      super.disconnectedCallback();
      clearTimeout(this._animationTimer);
    }
  }
  __decorateClass([
    property({ type: Number })
  ], SetpointMixinClass.prototype, "setpoint");
  __decorateClass([
    property({ type: Number })
  ], SetpointMixinClass.prototype, "newSetpoint");
  __decorateClass([
    property({ type: Boolean, attribute: false })
  ], SetpointMixinClass.prototype, "atSetpoint");
  __decorateClass([
    property({ type: Boolean })
  ], SetpointMixinClass.prototype, "touching");
  __decorateClass([
    property({ type: Boolean, attribute: false })
  ], SetpointMixinClass.prototype, "autoAtSetpoint");
  __decorateClass([
    property({ type: Number })
  ], SetpointMixinClass.prototype, "autoAtSetpointDeadband");
  __decorateClass([
    property({ type: Number })
  ], SetpointMixinClass.prototype, "setpointAtZeroDeadband");
  __decorateClass([
    property({ type: Boolean })
  ], SetpointMixinClass.prototype, "setpointOverride");
  __decorateClass([
    property({ type: Boolean })
  ], SetpointMixinClass.prototype, "animateSetpoint");
  __decorateClass([
    state()
  ], SetpointMixinClass.prototype, "_departingNewSetpoint");
  return SetpointMixinClass;
}
export {
  SetpointMixin
};
//# sourceMappingURL=setpoint-mixin.js.map
