from .behaviors import INewsMeta
from plone import api
from plone.app.contenttypes.interfaces import INewsItem
from plone.indexer.decorator import indexer


@indexer(INewsMeta)
def section_uuid(obj, **kw):
    return obj.section.to_object.uuid


@indexer(INewsItem)
def languages(obj):
    pl = api.portal.get_tool("portal_languages")
    languages = pl.settings.available_languages

    banned_translations = set()
    for translation in obj.translations:
        tobj = translation.to_object
        if tobj is not None:
            banned_translations.add(tobj.language)

    if banned_translations:
        return [lang for lang in languages if lang not in banned_translations]

    if obj.language:
        return [obj.language]
    return languages
