package calendarops import ( "sort" basetypes "code.apps.glenux.net/glenux/kiwimix/pkg/basetypes" ) // MergeCalendarOperation fusionne les événements qui se chevauchent dans un calendrier. type MergeCalendarOperation struct { } // Ensure FilterCalendarOperation implements CalendarOperation var _ CalendarOperation = (*MergeCalendarOperation)(nil) // NewMergeCalendarOperation crée une nouvelle instance de MergeCalendarOperation. func NewMergeCalendarOperation() *MergeCalendarOperation { return &MergeCalendarOperation{} } // Execute réalise l'opération de fusion. func (op *MergeCalendarOperation) Execute(cal ...basetypes.Calendar) (basetypes.Calendar, error) { if len(cal.Events) == 0 { return cal, nil } sort.Slice(cal.Event, func(i, j int) bool { return cal.Event[i].Start.Before(cal.Event[j].Start) }) mergedEvents := []basetypes.Event{cal.Event[0]} for _, event := range cal.Event[1:] { lastEvent := &mergedEvents[len(mergedEvents)-1] if event.Start.Before(lastEvent.End) || event.Start.Equal(lastEvent.End) { if event.End.After(lastEvent.End) { lastEvent.End = event.End } } else { mergedEvents = append(mergedEvents, event) } } return &basetypes.Calendar{Events: mergedEvents}, nil }