import copy


class ActionRegistry:
    """
    Simple registry class for registering actions to the TaskIndexView.
    """

    def __init__(self):
        self._actions = {}
        self._hidden_actions = []

    def register(self, cls=None, **kwargs):
        if kwargs.pop("hide", False):
            self._hidden_actions.append(cls.code)
        if cls is not None:
            instance = cls(**kwargs)
            if instance.code in self._actions:
                raise ValueError(
                    f"Action with code {instance.code} already registered."
                )
            self._actions[instance.code] = instance
            return cls

        def wrapper(cls):
            instance = cls(**kwargs)
            if instance.code in self._actions:
                raise ValueError(
                    f"Action with code {instance.code} already registered."
                )
            self._actions[instance.code] = instance
            return cls

        return wrapper

    def unregister(self, code):
        self._actions.pop(code, None)

    def get(self, code):
        if code not in self._actions:
            return

        original = self._actions[code]
        return copy.deepcopy(original)

    def get_actions_to_display(self):
        return {
            code: instance
            for code, instance in self._actions.items()
            if code not in self._hidden_actions
        }

    def get_actions(self):
        return self._actions


action_registry = ActionRegistry()
