package posthog

// Properties is used to represent properties in messages that support it.
// It is a free-form object so the application can set any value it sees fit but
// a few helper method are defined to make it easier to instantiate properties with
// common fields.
// Here's a quick example of how this type is meant to be used:
//
//	posthog.Capture{
//		DistinctId: "0123456789",
//		Event:      "order completed",
//		Properties: posthog.NewProperties()
//			.Set("revenue", 10.0)
//			.Set("currency", "USD"),
//	}
type Properties map[string]interface{}

// NewProperties creates an empty Properties map for fluent construction.
func NewProperties() Properties {
	return newStringInterfaceMap[Properties]()
}

func newStringInterfaceMap[M ~map[string]interface{}]() M {
	return make(M, 10)
}

// Set assigns a property value and returns the receiver.
func (p Properties) Set(name string, value interface{}) Properties {
	return setStringInterfaceMapValue(p, name, value)
}

func setStringInterfaceMapValue[M ~map[string]interface{}](m M, name string, value interface{}) M {
	m[name] = value
	return m
}

// Merge adds the properties from the provided `props` into the receiver `p`.
// If a property in `props` already exists in `p`, its value will be overwritten.
func (p Properties) Merge(props Properties) Properties {
	if props == nil {
		return p
	}

	for k, v := range props {
		p[k] = v
	}

	return p
}

// mergeDefaults adds properties from defaults only when the receiver does not
// already contain the key.
func (p Properties) mergeDefaults(defaults Properties) Properties {
	if p == nil {
		p = Properties{}
	}
	if defaults == nil {
		return p
	}

	for k, v := range defaults {
		if _, exists := p[k]; !exists {
			p[k] = v
		}
	}

	return p
}
