package sort

import (
	"errors"

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

type SUniqueUploaderSort struct {
}

func UniqueUploaderSort() *SUniqueUploaderSort {
	return &SUniqueUploaderSort{}
}
func (s *SUniqueUploaderSort) Sort(sortData *psort.SortData) error {
	if _, ok := sortData.Data.([]*module.Item); !ok {
		return errors.New("sort data type error")

	}
	return s.doSort(sortData)
}

// 将已存在的up主的视频过滤掉，不连续出现
func (s *SUniqueUploaderSort) doSort(sortData *psort.SortData) error {
	items := sortData.Data.([]*module.Item)
	newItems := make([] *module.Item, 0)
	uniq := make(map[string]bool)
	for _, i := range items {
		uploader := i.Properties["uid"].(string)
		//if unq[uploader] {
		if _, ok := uniq[uploader]; !ok{
			uniq[uploader] = true
			newItems = append(newItems, i)
		}
	}
	sortData.Data = newItems

	return nil
}
