1package trello
2
3import (
4	"sort"
5)
6
7// ActionCollection is an alias of []*Action, which sorts by the Action's ID.
8// Which is the same as sorting by the Action's time of occurrence
9type ActionCollection []*Action
10
11func (c ActionCollection) Len() int           { return len(c) }
12func (c ActionCollection) Swap(i, j int)      { c[i], c[j] = c[j], c[i] }
13func (c ActionCollection) Less(i, j int) bool { return c[i].ID < c[j].ID }
14
15// FirstCardCreateAction returns first card-create action
16func (c ActionCollection) FirstCardCreateAction() *Action {
17	sort.Sort(c)
18	for _, action := range c {
19		if action.DidCreateCard() {
20			return action
21		}
22	}
23	return nil
24}
25
26// ContainsCardCreation returns true if collection contains a card-create action
27func (c ActionCollection) ContainsCardCreation() bool {
28	return c.FirstCardCreateAction() != nil
29}
30
31// FilterToCardCreationActions returns this collection's card-create actions
32func (c ActionCollection) FilterToCardCreationActions() ActionCollection {
33	newSlice := make(ActionCollection, 0, len(c))
34	for _, action := range c {
35		if action.DidCreateCard() {
36			newSlice = append(newSlice, action)
37		}
38	}
39	return newSlice
40}
41
42// FilterToListChangeActions returns card-change-list actions
43func (c ActionCollection) FilterToListChangeActions() ActionCollection {
44	newSlice := make(ActionCollection, 0, len(c))
45	for _, action := range c {
46		if action.DidChangeListForCard() {
47			newSlice = append(newSlice, action)
48		}
49	}
50	return newSlice
51}
52
53// FilterToCardMembershipChangeActions returns the collection's card-change, archive and unarchive actions
54func (c ActionCollection) FilterToCardMembershipChangeActions() ActionCollection {
55	newSlice := make(ActionCollection, 0, len(c))
56	for _, action := range c {
57		if action.DidChangeCardMembership() || action.DidArchiveCard() || action.DidUnarchiveCard() {
58			newSlice = append(newSlice, action)
59		}
60	}
61	return newSlice
62}
63