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

package otelconf // import "go.opentelemetry.io/contrib/otelconf"

import (
	"strings"

	"go.opentelemetry.io/otel/propagation"
	"golang.org/x/exp/maps"

	"go.opentelemetry.io/contrib/propagators/autoprop"
)

func newPropagator(prop OpenTelemetryConfigurationPropagator) (propagation.TextMapPropagator, error) {
	if prop == nil {
		return propagation.NewCompositeTextMapPropagator(), nil
	}

	p, ok := prop.(*PropagatorJson)
	if !ok {
		return nil, newErrInvalid("propagator")
	}

	n := len(p.Composite)
	if n == 0 && p.CompositeList == nil {
		return propagation.NewCompositeTextMapPropagator(), nil
	}

	names := map[string]struct{}{}
	for _, propagator := range p.Composite {
		if propagator.B3 != nil {
			names["b3"] = struct{}{}
		}
		if propagator.B3Multi != nil {
			names["b3multi"] = struct{}{}
		}
		if propagator.Baggage != nil {
			names["baggage"] = struct{}{}
		}
		if propagator.Jaeger != nil {
			names["jaeger"] = struct{}{}
		}
		if propagator.Ottrace != nil {
			names["ottrace"] = struct{}{}
		}
		if propagator.Tracecontext != nil {
			names["tracecontext"] = struct{}{}
		}
	}

	if p.CompositeList != nil {
		for _, v := range strings.Split(*p.CompositeList, ",") {
			names[v] = struct{}{}
		}
	}

	if len(names) == 0 {
		return autoprop.NewTextMapPropagator(), nil
	}

	return autoprop.TextMapPropagator(maps.Keys(names)...)
}
