package filter

import (
	"errors"

	"gitlab.alibaba-inc.com/pai_biz_arch/pairec/module"
)

// remove duplicate item
type UniqueFilter struct {
}

func NewUniqueFilter() *UniqueFilter {
	filter := UniqueFilter{}

	return &filter
}
func (f *UniqueFilter) Filter(filterData *FilterData) error {
	if _, ok := filterData.Data.([]*module.Item); !ok {
		return errors.New("filter data type error")

	}
	return f.doFilter(filterData)
}

func (f *UniqueFilter) doFilter(filterData *FilterData) error {
	items := filterData.Data.([]*module.Item)
	newItems := make([]*module.Item, 0)
	uniq := make(map[module.ItemId]bool)

	for _, item := range items {
		if _, ok := uniq[item.Id]; !ok {
			uniq[item.Id] = true
			newItems = append(newItems, item)
		}
	}

	filterData.Data = newItems
	return nil
}
func (f *UniqueFilter) MatchTag(tag string) bool {
	// default filter, so filter all tag
	return true
}
