package analyticsauthoringv1

import (
	"bytes"
	"encoding/json"
	"errors"
	"io"
)

// CanonicalDefinitionJSON normalizes one definition document to the exact
// byte form used for transport validation and replay identity.
func CanonicalDefinitionJSON(payload []byte) ([]byte, error) {
	var value any
	decoder := json.NewDecoder(bytes.NewReader(payload))
	decoder.UseNumber()
	if err := decoder.Decode(&value); err != nil {
		return nil, err
	}
	if err := decoder.Decode(&struct{}{}); err != io.EOF {
		return nil, errors.New("analytics authoring: definition must contain one JSON value")
	}
	return json.Marshal(value)
}
