"""
Module contains the OmniGraph node implementation of BundleConstructor
"""
import omni.graph.core as og


class OgnBundleConstructor:
    """Node to create a bundle out of dynamic attributes"""

    @staticmethod
    def compute(db) -> bool:
        """Compute the bundle from the dynamic input attributes"""

        # Start with an empty output bundle.
        output_bundle = db.outputs.bundle
        output_bundle.clear()

        # Walk the list of node attribute, looking for dynamic inputs
        for attribute in db.abi_node.get_attributes():
            if attribute.is_dynamic():

                if attribute.get_port_type() != og.AttributePortType.ATTRIBUTE_PORT_TYPE_INPUT:
                    continue

                if attribute.get_extended_type() != og.ExtendedAttributeType.EXTENDED_ATTR_TYPE_REGULAR:
                    db.log_warn(f"Cannot add extended attribute types like '{attribute.get_name()}' to a bundle")
                    continue

                if attribute.get_resolved_type().base_type in [og.BaseDataType.RELATIONSHIP]:
                    db.log_warn(f"Cannot add bundle attribute types like '{attribute.get_name()}' to a bundle")
                    continue

                # The bundled name does not need the port namespace
                new_name = attribute.get_name().replace("inputs:", "")
                output_bundle.insert((attribute.get_resolved_type(), new_name))

        return True
