// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package log // import "go.opentelemetry.io/otel/sdk/log"

import (
	
	
	
	

	
)

// setting is a configuration setting value.
type setting[ any] struct {
	Value 
	Set   bool
}

// newSetting returns a new [setting] with the value set.
func newSetting[ any]( ) setting[] {
	return setting[]{Value: , Set: true}
}

// resolver returns an updated setting after applying an resolution operation.
type resolver[ any] func(setting[]) setting[]

// Resolve returns a resolved version of s.
//
// It will apply all the passed fn in the order provided, chaining together the
// return setting to the next input. The setting s is used as the initial
// argument to the first fn.
//
// Each fn needs to validate if it should apply given the Set state of the
// setting. This will not perform any checks on the set state when chaining
// function.
func ( setting[]) ( ...resolver[]) setting[] {
	for ,  := range  {
		 = ()
	}
	return 
}

// clampMax returns a resolver that will ensure a setting value is no greater
// than n. If it is, the value is set to n.
func clampMax[ ~int | ~int64]( ) resolver[] {
	return func( setting[]) setting[] {
		if .Value >  {
			.Value = 
		}
		return 
	}
}

// clearLessThanOne returns a resolver that will clear a setting value and
// change its set state to false if its value is less than 1.
func clearLessThanOne[ ~int | ~int64]() resolver[] {
	return func( setting[]) setting[] {
		if .Value < 1 {
			.Value = 0
			.Set = false
		}
		return 
	}
}

// getenv returns a resolver that will apply an integer environment variable
// value associated with key to a setting value.
//
// If the input setting to the resolver is set, the environment variable will
// not be applied.
//
// If the environment variable value associated with key is not an integer, an
// error will be sent to the OTel error handler and the setting will not be
// updated.
//
// If the setting value is a [time.Duration] type, the environment variable
// will be interpreted as a duration of milliseconds.
func getenv[ ~int | ~int64]( string) resolver[] {
	return func( setting[]) setting[] {
		if .Set {
			// Passed, valid, options have precedence.
			return 
		}

		if  := os.Getenv();  != "" {
			,  := strconv.Atoi()
			if  != nil {
				otel.Handle(fmt.Errorf("invalid %s value %s: %w", , , ))
			} else {
				switch any(.Value).(type) {
				case time.Duration:
					// OTel duration envar are in millisecond.
					.Value = (time.Duration() * time.Millisecond)
				default:
					.Value = ()
				}
				.Set = true
			}
		}
		return 
	}
}

// fallback returns a resolve that will set a setting value to val if it is not
// already set.
//
// This is usually passed at the end of a resolver chain to ensure a default is
// applied if the setting has not already been set.
func fallback[ any]( ) resolver[] {
	return func( setting[]) setting[] {
		if !.Set {
			.Value = 
			.Set = true
		}
		return 
	}
}