package paymentkit

import (
	"encoding/json"
	"fmt"
	"strings"
)

func JSONObject(raw []byte) map[string]any {
	var value any
	if json.Unmarshal(raw, &value) != nil {
		return nil
	}
	object, _ := value.(map[string]any)
	return object
}

func StringAtPath(value any, path string) string {
	found := ValueAtPath(value, path)
	if found == nil {
		return ""
	}
	return strings.TrimSpace(fmt.Sprint(found))
}

func ValueAtPath(value any, path string) any {
	path = strings.TrimSpace(path)
	if path == "" {
		return nil
	}
	current := value
	for _, part := range strings.Split(path, ".") {
		object, ok := current.(map[string]any)
		if !ok {
			return nil
		}
		current = object[part]
	}
	return current
}
