package newssvc

import (
	newsdomain "legalgo-BE-go/internal/domain/news"
	"strings"
)

func (i *impl) GetAll(categoriesCode, tagCodes string) ([]newsdomain.News, error) {
	var err error

	categories := []string{}
	tags := []string{}
	news := []newsdomain.News{}

	tagCodeArr := strings.Split(tagCodes, " ")
	categoryCodeArr := strings.Split(categoriesCode, " ")

	if len(tagCodeArr) > 0 && tagCodeArr[0] != "" {
		tags, err = i.tagRepo.GetIDsByCodes(tagCodeArr)
		if err != nil {
			return news, err
		}

		if len(tags) < 1 {
			return news, nil
		}
	}

	if len(categoryCodeArr) > 0 && categoryCodeArr[0] != "" {
		categories, err = i.categoryRepo.GetIDByCode(categoryCodeArr)
		if err != nil {
			return news, err
		}

		if len(categories) < 1 {
			return news, nil
		}
	}

	filter := newsdomain.NewsFilter{
		Tags:     tags,
		Category: categories,
	}
	return i.newsRepo.GetAll(filter)
}
