package domain

import (
	"path"
)

type Resources []*Resource

func (r Resources) GetType(target ResourceType) Resources {
	out := make(Resources, 0, len(r))

	for i := range r {
		if r[i].resourceType != target {
			continue
		}

		out = append(out, r[i])
	}

	return out
}

func (r Resources) Get(path string) *Resource {
	for i := range r {
		if r[i].key != path {
			continue
		}

		return r[i]
	}

	return nil
}

func (r Resources) Match(pattern string) Resources {
	out := make(Resources, 0, len(r))

	for i := range r {
		if matched, err := path.Match(pattern, r[i].key); err != nil || !matched {
			continue
		}

		out = append(out, r[i])
	}

	return out
}

func (r Resources) GetMatch(pattern string) *Resource {
	for i := range r {
		if matched, err := path.Match(pattern, r[i].key); err != nil || !matched {
			continue
		}

		return r[i]
	}

	return nil
}
