// ParseEvents function is left only for backward-compatibility. Events are parsed by events pkg.
func ParseEvents(rawEventsPtr []*json.RawMessage) (*[]events.Event, error) {
	rawEvents := make([]json.RawMessage, len(rawEventsPtr))
	for i, ptr := range rawEventsPtr {
		rawEvents[i] = *ptr
	}

	events, err := events.ParseRawJSONEvents(rawEvents)
	if err != nil {
		return nil, err
	}
	return &events, nil
}
func (ep *EventsPage) UnmarshalJSON(data []byte) error {
	// Clear object.
	*ep = EventsPage{}

	// Object with array of events and cursors is being sent on Message Events.
	var resultsWrapper struct {
		RawEvents  []json.RawMessage `json:"results"`
		TotalCount int               `json:"total_count,omitempty"`
		Links      []struct {
			Href string `json:"href"`
			Rel  string `json:"rel"`
		} `json:"links,omitempty"`
	}
	err := json.Unmarshal(data, &resultsWrapper)
	if err != nil {
		return err
	}

	ep.Events, err = events.ParseRawJSONEvents(resultsWrapper.RawEvents)
	if err != nil {
		return err
	}

	ep.TotalCount = resultsWrapper.TotalCount

	for _, link := range resultsWrapper.Links {
		switch link.Rel {
		case "next":
			ep.nextPage = link.Href
		case "previous":
			ep.prevPage = link.Href
		case "first":
			ep.firstPage = link.Href
		case "last":
			ep.lastPage = link.Href
		}
	}

	return nil
}