# Path: schema_salad/exceptions.py
# class SchemaException(SchemaSaladException):
#     """Indicates error with the provided schema definition."""
# 
# Path: schema_salad/utils.py
# def add_dictlist(di, key, val):  # type: (Dict[Any, Any], Any, Any) -> None
# def aslist(thing):  # type: (Any) -> MutableSequence[Any]
# def flatten(thing, ltypes=(list, tuple)):
# def onWindows():
# def convert_to_dict(j4):  # type: (Any) -> Any
# def json_dump(
#     obj,  # type: Any
#     fp,  # type: IO[str]
#     **kwargs  # type: Any
# ):  # type: (...) -> None
# def json_dumps(
#     obj,  # type: Any
#     **kwargs  # type: Any
# ):  # type: (...) -> str
# def stdout() -> BufferedWriter:
#     def construct_yaml_timestamp(self: Any, node: Any, values: Any = None) -> Any:
# def yaml_no_ts() -> YAML:
# class _RoundTripNoTimeStampConstructor(RoundTripConstructor):


import logging
import unicodedata
import rdflib
import rdflib.namespace
from typing import (
    Any,
    Dict,
    Iterable,
    List,
    MutableMapping,
    MutableSequence,
    Optional,
    Tuple,
    Union,
    cast,
)
from urllib.parse import urldefrag, urlsplit
from rdflib import Graph, URIRef
from rdflib.namespace import RDF, RDFS
from ruamel.yaml.comments import CommentedMap, CommentedSeq
from .exceptions import SchemaException
from .utils import ContextType, aslist, json_dumps


def pred(
    datatype: MutableMapping[str, Union[Dict[str, str], str]],
    field: Optional[Dict[str, Any]],
    name: str,
    context: ContextType,
    defaultBase: str,
    namespaces: Dict[str, rdflib.namespace.Namespace],
) -> Union[Dict[str, Union[str, None]], str]:
    split = urlsplit(name)

    vee = None  # type: Optional[str]

    if split.scheme != "":
        vee = name
        (ns, ln) = rdflib.namespace.split_uri(str(vee))
        name = ln
        if ns[0:-1] in namespaces:
            vee = str(namespaces[ns[0:-1]][ln])
        _logger.debug("name, v %s %s", name, vee)

    v = None  # type: Optional[Union[Dict[str, Union[str, None]], str]]

    if field is not None and "jsonldPredicate" in field:
        if isinstance(field["jsonldPredicate"], MutableMapping):
            v = {}
            for k, val in field["jsonldPredicate"].items():
                v[("@" + k[1:] if k.startswith("_") else k)] = val
            if "@id" not in v:

# Path: schema_salad/exceptions.py
# class SchemaException(SchemaSaladException):
#     """Indicates error with the provided schema definition."""
# 
# Path: schema_salad/utils.py
# def add_dictlist(di, key, val):  # type: (Dict[Any, Any], Any, Any) -> None
# def aslist(thing):  # type: (Any) -> MutableSequence[Any]
# def flatten(thing, ltypes=(list, tuple)):
# def onWindows():
# def convert_to_dict(j4):  # type: (Any) -> Any
# def json_dump(
#     obj,  # type: Any
#     fp,  # type: IO[str]
#     **kwargs  # type: Any
# ):  # type: (...) -> None
# def json_dumps(
#     obj,  # type: Any
#     **kwargs  # type: Any
# ):  # type: (...) -> str
# def stdout() -> BufferedWriter:
#     def construct_yaml_timestamp(self: Any, node: Any, values: Any = None) -> Any:
# def yaml_no_ts() -> YAML:
# class _RoundTripNoTimeStampConstructor(RoundTripConstructor):


# Path: schema_salad/jsonld_context.py
import logging
import unicodedata
import rdflib
import rdflib.namespace
from typing import (
    Any,
    Dict,
    Iterable,
    List,
    MutableMapping,
    MutableSequence,
    Optional,
    Tuple,
    Union,
    cast,
)
from urllib.parse import urldefrag, urlsplit
from rdflib import Graph, URIRef
from rdflib.namespace import RDF, RDFS
from ruamel.yaml.comments import CommentedMap, CommentedSeq
from .exceptions import SchemaException
from .utils import ContextType, aslist, json_dumps



def pred(
    datatype: MutableMapping[str, Union[Dict[str, str], str]],
    field: Optional[Dict[str, Any]],
    name: str,
    context: ContextType,
    defaultBase: str,
    namespaces: Dict[str, rdflib.namespace.Namespace],
) -> Union[Dict[str, Union[str, None]], str]:
    split = urlsplit(name)

    vee = None  # type: Optional[str]

    if split.scheme != "":
        vee = name
        (ns, ln) = rdflib.namespace.split_uri(str(vee))
        name = ln
        if ns[0:-1] in namespaces:
            vee = str(namespaces[ns[0:-1]][ln])
        _logger.debug("name, v %s %s", name, vee)

    v = None  # type: Optional[Union[Dict[str, Union[str, None]], str]]

    if field is not None and "jsonldPredicate" in field:
        if isinstance(field["jsonldPredicate"], MutableMapping):
            v = {}
            for k, val in field["jsonldPredicate"].items():
                v[("@" + k[1:] if k.startswith("_") else k)] = val
            if "@id" not in v:

                v["@id"] = vee