# typed: strict
# frozen_string_literal: true

module Stats
  # Base class for transformations that follow y = a * f(x) + b pattern
  # where f(x) is a transformation function and denormalization only requires y-scaling
  class TransformedNormalizer < DataNormalizer
    extend T::Sig
    extend T::Helpers
    abstract!

    # Denormalize coefficients for simple transformations (only y-scaling needed)
    sig do
      params(norm_intercept: Float, norm_slope: Float).returns(T::Array[Float])
    end
    def denormalize_coefficients(norm_intercept, norm_slope)
      slope_orig = norm_slope * @y.scale
      intercept_orig = norm_intercept * @y.scale + @y.min
      [intercept_orig, slope_orig]
    end

    # Common denormalization logic using the transformation function
    sig(:final) do
      override
        .params(regression_x: T::Array[Float], weight_vec: T::Array[Float])
        .returns(T::Array[Float])
    end
    def denormalize_regression(regression_x, weight_vec)
      norm_slope = T.cast(weight_vec[1], Float)
      norm_intercept = T.cast(weight_vec[0], Float)
      regression_x.map do |x|
        # y = a * f(x) + b, where coefficients are in normalized space
        y_norm = norm_slope * @x.normalize(x) + norm_intercept
        @y.denormalize(y_norm)
      end
    end
  end
end
