package ii

type Items interface {
	Has(name Name) (*ItemInfo, bool)
	All() []*ItemInfo
}

type ItemIndex struct {
	itemMap map[string]*ItemInfo
	dbAlias map[string]string
}

func (idx *ItemIndex) Has(name Name) (*ItemInfo, bool) {
	info, ok := idx.itemMap[name.ItemName()]
	if !ok {
		if alias, found := idx.dbAlias[name.DbName()]; found {
			return idx.Has(NewName(alias, name.Collection()))
		}
		return nil, false
	}
	return info, true
}

func (idx *ItemIndex) All() []*ItemInfo {
	list := make([]*ItemInfo, 0, len(idx.itemMap))
	for _, info := range idx.itemMap {
		list = append(list, info)
	}
	return list
}
