package ezcx

import (
	"fmt"

	"google.golang.org/protobuf/types/known/structpb"
)

type contextKey int

const (
	Logger contextKey = iota
)

func anyToProto(value any) (*structpb.Value, error) {
	return structpb.NewValue(value)
}

func protoToAny(value *structpb.Value) any {
	return value.AsInterface()
}

func anyToProtoMap(m map[string]any) (map[string]*structpb.Value, error) {
	pm := make(map[string]*structpb.Value)
	for k, v := range m {
		pv, err := anyToProto(v)
		if err != nil {
			return nil, err
		}
		pm[k] = pv
	}
	return pm, nil
}

func protoToAnyMap(pm map[string]*structpb.Value) map[string]any {
	m := make(map[string]any)
	for k, pv := range pm {
		v := protoToAny(pv)
		m[k] = v
	}
	return m
}

func ErrUnmarshalWrapper(site string, err error) error {
	return fmt.Errorf("UNMARSHALLING ERROR | call site: %s, error: %w", site, err)
}