# list_available_archivers_action.py
#
# Project: AutoArchive
# License: GNU GPLv3
#
# Copyright (C) 2003 - 2025 Róbert Čerňanský



from typing import Iterable

from AutoArchive._infrastructure.configuration import OptionsUtils
from AutoArchive._services.archiver import ArchiverServiceIdentification
from AutoArchive._services.archiver.tar_archiver_provider_base import TarArchiverProviderBase
from .action_utils import ActionUtils
from .._utils import _Utils
from ..._action.action import Action



class ListAvailableArchiversAction(Action):

    def __init__(self, actionUtils: ActionUtils):
        super().__init__(actionUtils.actionResult)

        self.__actionUtils = actionUtils



    def executeAction_(self) -> bool:
        availableArchiverTypes = self.__getAvailableArchiverTypes()
        for archiverType in availableArchiverTypes:
            self.__actionUtils.componentUi.presentLine(OptionsUtils.archiverTypeToStr(archiverType))
        return True



    def __getAvailableArchiverTypes(self):
        availableArchiverTypes = set()

        allArchiverServices: Iterable[TarArchiverProviderBase] = \
            self.__actionUtils.serviceAccessor.getOrCreateServices(ArchiverServiceIdentification)

        for service in allArchiverServices:
            providerIdentification = self.__actionUtils.serviceAccessor.getProviderIdentification(
                ArchiverServiceIdentification, type(service))

            supportedArchiverTypes = frozenset(_Utils.backupTypesToArchiverTypes(service.getAvailableBackupTypes()))
            possibleArchiverTypes = frozenset(_Utils.providerIdToArchiverTypes(providerIdentification))

            availableArchiverTypes |= supportedArchiverTypes & possibleArchiverTypes

        return availableArchiverTypes
