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

package baggage // import "go.opentelemetry.io/otel/internal/baggage"

import 

type baggageContextKeyType int

const baggageKey baggageContextKeyType = iota

// SetHookFunc is a callback called when storing baggage in the context.
type SetHookFunc func(context.Context, List) context.Context

// GetHookFunc is a callback called when getting baggage from the context.
type GetHookFunc func(context.Context, List) List

type baggageState struct {
	list List

	setHook SetHookFunc
	getHook GetHookFunc
}

// ContextWithSetHook returns a copy of parent with hook configured to be
// invoked every time ContextWithBaggage is called.
//
// Passing nil SetHookFunc creates a context with no set hook to call.
func ( context.Context,  SetHookFunc) context.Context {
	var  baggageState
	if ,  := .Value(baggageKey).(baggageState);  {
		 = 
	}

	.setHook = 
	return context.WithValue(, baggageKey, )
}

// ContextWithGetHook returns a copy of parent with hook configured to be
// invoked every time FromContext is called.
//
// Passing nil GetHookFunc creates a context with no get hook to call.
func ( context.Context,  GetHookFunc) context.Context {
	var  baggageState
	if ,  := .Value(baggageKey).(baggageState);  {
		 = 
	}

	.getHook = 
	return context.WithValue(, baggageKey, )
}

// ContextWithList returns a copy of parent with baggage. Passing nil list
// returns a context without any baggage.
func ( context.Context,  List) context.Context {
	var  baggageState
	if ,  := .Value(baggageKey).(baggageState);  {
		 = 
	}

	.list = 
	 := context.WithValue(, baggageKey, )
	if .setHook != nil {
		 = .setHook(, )
	}

	return 
}

// ListFromContext returns the baggage contained in ctx.
func ( context.Context) List {
	switch v := .Value(baggageKey).(type) {
	case baggageState:
		if .getHook != nil {
			return .getHook(, .list)
		}
		return .list
	default:
		return nil
	}
}