package ask

import (
	"fmt"
	"time"
)

func Duration(query string, min, max time.Duration) time.Duration {
	return raw(query, '\n', func(s string) (v time.Duration, msg string) {
		v, err := time.ParseDuration(s)
		if err != nil {
			msg = "please enter a number"
			return v, msg
		}
		if min >= 0 && v < min {
			msg = fmt.Sprintf("your number must be bigger than %d", min)
			return v, msg
		}
		if max >= 0 && v > max {
			msg = fmt.Sprintf("your number must be smaller than %d", min)
			return v, msg
		}
		return v, msg
	})
}
