package icecast

import "time"

type ReconnectConfig struct {
	Enabled          bool
	InitialBackoffMs int
	MaxBackoffMs     int
}

func (c ReconnectConfig) nextBackoff(attempt int) time.Duration {
	if !c.Enabled {
		return 0
	}
	initial := c.InitialBackoffMs
	if initial <= 0 {
		initial = 1000
	}
	max := c.MaxBackoffMs
	if max <= 0 {
		max = 15000
	}
	d := time.Duration(initial) * time.Millisecond
	for i := 1; i < attempt; i++ {
		d *= 2
		if d >= time.Duration(max)*time.Millisecond {
			return time.Duration(max) * time.Millisecond
		}
	}
	return d
}
