import six


def _get_model_dependencies(model):
    """Return a list of dependency DTMIs for a given model"""
    dependencies = []

    if "contents" in model:
        components = [item["schema"] for item in model["contents"] if item["@type"] == "Component"]
        dependencies += components

    if "extends" in model:
        # Models defined in a DTDL can implement extensions of up to two interfaces.
        # These interfaces can be in the form of a DTMI reference, or a nested model (which would
        # have it's own dependencies)
        if isinstance(model["extends"], six.text_type):
            # If it's just a string, that's a single DTMI reference, so just add that to our list
            dependencies.append(model["extends"])
        elif isinstance(model["extends"], list):
            # If it's a list, could have DTMIs or nested models
            for item in model["extends"]:
                if isinstance(item, six.text_type):
                    # If there are strings in the list, that's a DTMI reference, so add it
                    dependencies.append(item)
                elif isinstance(item, dict):
                    # This is a nested model. Now go get its dependencies and add them
                    dependencies += _get_model_dependencies(item)

    # Remove duplicate dependencies
    dependencies = list(set(dependencies))
    return dependencies