// helpers.go contains shared pointer and scalar conversion helpers.
// Layer: adapters/inbound/httpapi/presenter helper functions.
package presenter

import (
	"math"
	"time"
)

func celsiusToFahrenheitPtr(v *float64) *float64 {
	if v == nil {
		return nil
	}
	out := (*v * celsiusToFahrenheitScale) + celsiusToFahrenheitOffset
	return &out
}

func scalePtr(v *float64, factor float64) *float64 {
	if v == nil {
		return nil
	}
	out := *v * factor
	return &out
}

func copyFloat64Ptr(v *float64) *float64 {
	if v == nil {
		return nil
	}
	out := *v
	return &out
}

func copyBoolPtr(v *bool) *bool {
	if v == nil {
		return nil
	}
	out := *v
	return &out
}

func copyTimePtr(v *time.Time) *time.Time {
	if v == nil {
		return nil
	}
	out := *v
	return &out
}

func inLocationTime(v time.Time, loc *time.Location) time.Time {
	if loc == nil {
		return v
	}
	return v.In(loc)
}

func inLocationTimePtr(v *time.Time, loc *time.Location) *time.Time {
	if v == nil {
		return nil
	}
	out := inLocationTime(*v, loc)
	return &out
}

func boolText(v *bool) string {
	if v == nil {
		return ""
	}
	if *v {
		return "true"
	}
	return "false"
}

func roundedPtr(v *float64, precision int) *float64 {
	if v == nil {
		return nil
	}
	out := roundFloat(*v, precision)
	return &out
}

func roundFloat(v float64, precision int) float64 {
	if precision <= 0 {
		return math.Round(v)
	}
	factor := math.Pow10(precision)
	return math.Round(v*factor) / factor
}
