package server

import (
	"strings"

	"warpbox/lib/boxstore"
	"warpbox/lib/models"
)

func (app *App) retentionAllowed(key string) bool {
	key = strings.TrimSpace(key)
	if key == "" {
		return true
	}
	for _, option := range app.retentionOptions() {
		if option.Key == key {
			return true
		}
	}
	return false
}

func (app *App) retentionOptions() []models.RetentionOption {
	allOptions := boxstore.RetentionOptions()
	options := make([]models.RetentionOption, 0, len(allOptions))
	for _, option := range allOptions {
		if option.Key == boxstore.OneTimeDownloadRetentionKey && !app.config.OneTimeDownloadsEnabled {
			continue
		}
		if option.Seconds > 0 && app.config.MaxGuestExpirySeconds > 0 && option.Seconds > app.config.MaxGuestExpirySeconds {
			continue
		}
		options = append(options, option)
	}
	if len(options) == 0 {
		return allOptions[:1]
	}
	return options
}

func (app *App) defaultRetentionOption() models.RetentionOption {
	options := app.retentionOptions()
	for _, option := range options {
		if option.Seconds == app.config.DefaultGuestExpirySeconds {
			return option
		}
	}
	return options[0]
}
