from tanim.utils.config_ops import digest_config
from tanim.utils.iterables import list_update


# Currently, this is only used by both Scene and Mobject.
# Still, we abstract its functionality here, albeit purely nominally.
# All actual implementation has to be handled by derived classes for now.


class Container(object):
    def __init__(self, **kwargs):
        digest_config(self, kwargs)
        self.submobjects = []  # Is it really better to name it submobjects?

    def add(self, *mobjects):
        if self in mobjects:
            raise Exception("Mobject cannot contain self")
        self.submobjects = list_update(self.submobjects, mobjects)
        return self

    def add_to_back(self, *mobjects):
        self.remove(*mobjects)
        self.submobjects = list(mobjects) + self.submobjects
        return self

    def remove(self, *mobjects, ):
        for mobject in mobjects:
            for submod in self.submobjects:
                if isinstance(submod, GroupContainer):
                    submod.remove(mobject)
                elif mobject == submod:
                    self.submobjects.remove(mobject)
        return self


class GroupContainer(Container):
    def __init__(self, *containers, **kwargs):
        self.add(*containers)

[((12, 8, 12, 35), 'tanim.utils.config_ops.digest_config', 'digest_config', ({(12, 22, 12, 26): 'self', (12, 28, 12, 34): 'kwargs'}, {}), '(self, kwargs)', False, 'from tanim.utils.config_ops import digest_config\n'), ((18, 27, 18, 66), 'tanim.utils.iterables.list_update', 'list_update', ({(18, 39, 18, 55): 'self.submobjects', (18, 57, 18, 65): 'mobjects'}, {}), '(self.submobjects, mobjects)', False, 'from tanim.utils.iterables import list_update\n')]