# typed: strict
# frozen_string_literal: true

module Stats
  # Base class for transformed equations that follow y = a * f(x) + b pattern
  class TransformedEquation < Equation
    extend T::Sig
    extend T::Helpers
    abstract!

    sig(:final) do
      params(x: Stats::Axis, y: Stats::Axis, coefficients: [Float, Float]).void
    end
    def initialize(x, y, coefficients)
      super(x, y)
      @norm_intercept = T.let(coefficients[0], Float)
      @norm_slope = T.let(coefficients[1], Float)
    end

    sig(:final) { override.returns(T::Array[Float]) }
    def coefficients
      [@norm_intercept, @norm_slope]
    end

    protected

    # Evaluate the transformed equation for a given x value
    sig(:final) { override.params(x_value: Float).returns(Float) }
    def evaluate_normalized(x_value)
      @norm_slope * x_value + @norm_intercept
    end

    sig { override.returns(String) }
    def format_equation
      slope_orig = @norm_slope * @y.scale
      intercept_orig = @norm_intercept * @y.scale + @y.min

      "y = #{Stats::Helpers.format_number(slope_orig)} * #{transform_symbol("x")} + #{Stats::Helpers.format_number(intercept_orig)}"
    end

    # Abstract method for the transformation symbol
    sig { abstract.params(x: String).returns(String) }
    def transform_symbol(x)
    end
  end
end
