"""UKO Layer 3: TypeScript-specific vocabulary (``uko-ts:``).

Extends the Layer 2 Object-Oriented paradigm (``uko-oo:``) with
TypeScript-specific semantic refinements:

Classes:
- ``TypeScriptModule``    -- TypeScript module
- ``TypeScriptClass``     -- TypeScript class
- ``TypeScriptInterface`` -- TypeScript interface with generic params
- ``TypeScriptFunction``  -- TypeScript function/method

TypeScript extends at the SIGNATURES level with:
- ``export``/``default export`` markers
- ``declare`` ambient types
- Generic type parameters
- Mapped/conditional types

Based on ``docs/specification.md`` ~lines 44405-44420.
"""

from __future__ import annotations

from cleveragents.acms.uko.vocabulary import (
    OO_EFFECTIVE_LEVELS,
    UKO_CODE_IRI,
    UKO_OO_IRI,
    Layer2Dependency,
    UKOClass,
    UKOProperty,
    UKOVocabulary,
    build_detail_level_map,
)

__all__: list[str] = [
    "TYPESCRIPT_DETAIL_LEVELS",
    "TYPESCRIPT_VOCABULARY",
    "TypeScriptClass",
    "TypeScriptFunction",
    "TypeScriptInterface",
    "TypeScriptModule",
]

# ---------------------------------------------------------------------------
# Namespace constants
# ---------------------------------------------------------------------------

UKO_TS_IRI = "https://cleveragents.ai/ontology/uko/ts#"
UKO_TS_PREFIX = "uko-ts:"


# ---------------------------------------------------------------------------
# OWL Class definitions
# ---------------------------------------------------------------------------

TypeScriptModule = UKOClass(
    uri=f"{UKO_TS_IRI}TypeScriptModule",
    label="TypeScriptModule",
    comment="A TypeScript module (.ts/.tsx file) — extends uko-code:Module.",
    subclass_of=(f"{UKO_CODE_IRI}Module",),
)

TypeScriptClass = UKOClass(
    uri=f"{UKO_TS_IRI}TypeScriptClass",
    label="TypeScriptClass",
    comment="A TypeScript class definition — extends uko-oo:Class.",
    subclass_of=(f"{UKO_OO_IRI}Class",),
)

TypeScriptInterface = UKOClass(
    uri=f"{UKO_TS_IRI}TypeScriptInterface",
    label="TypeScriptInterface",
    comment=(
        "A TypeScript interface — extends uko-oo:Interface. "
        "Includes generic type parameters, mapped and conditional types."
    ),
    subclass_of=(f"{UKO_OO_IRI}Interface",),
)

TypeScriptFunction = UKOClass(
    uri=f"{UKO_TS_IRI}TypeScriptFunction",
    label="TypeScriptFunction",
    comment=(
        "A TypeScript function or method — extends uko-oo:Method. "
        "Includes export markers and generic type parameters."
    ),
    subclass_of=(f"{UKO_OO_IRI}Method",),
)


# ---------------------------------------------------------------------------
# OWL Property definitions
# ---------------------------------------------------------------------------

_ts_export_kind = UKOProperty(
    uri=f"{UKO_TS_IRI}exportKind",
    label="exportKind",
    comment=(
        "Export marker: 'named', 'default', 'none'. "
        "Indicates how this symbol is exported from its module."
    ),
    domain=(
        f"{UKO_TS_IRI}TypeScriptClass",
        f"{UKO_TS_IRI}TypeScriptInterface",
        f"{UKO_TS_IRI}TypeScriptFunction",
        f"{UKO_TS_IRI}TypeScriptModule",
    ),
    range_uri="xsd:string",
    is_object_property=False,
)

_ts_is_ambient = UKOProperty(
    uri=f"{UKO_TS_IRI}isAmbient",
    label="isAmbient",
    comment=(
        "Whether this is a 'declare' ambient type definition (e.g. in a .d.ts file)."
    ),
    domain=(
        f"{UKO_TS_IRI}TypeScriptClass",
        f"{UKO_TS_IRI}TypeScriptInterface",
        f"{UKO_TS_IRI}TypeScriptFunction",
    ),
    range_uri="xsd:boolean",
    is_object_property=False,
)

_ts_generic_params = UKOProperty(
    uri=f"{UKO_TS_IRI}genericParameters",
    label="genericParameters",
    comment=("Generic type parameter string (e.g. '<T extends Foo, U = Bar>')."),
    domain=(
        f"{UKO_TS_IRI}TypeScriptClass",
        f"{UKO_TS_IRI}TypeScriptInterface",
        f"{UKO_TS_IRI}TypeScriptFunction",
    ),
    range_uri="xsd:string",
    is_object_property=False,
)

_ts_mapped_type = UKOProperty(
    uri=f"{UKO_TS_IRI}isMappedType",
    label="isMappedType",
    comment="Whether this interface defines a mapped type.",
    domain=(f"{UKO_TS_IRI}TypeScriptInterface",),
    range_uri="xsd:boolean",
    is_object_property=False,
)

_ts_conditional_type = UKOProperty(
    uri=f"{UKO_TS_IRI}isConditionalType",
    label="isConditionalType",
    comment="Whether this interface/type alias uses conditional types.",
    domain=(f"{UKO_TS_IRI}TypeScriptInterface",),
    range_uri="xsd:boolean",
    is_object_property=False,
)

_ts_implements_interface = UKOProperty(
    uri=f"{UKO_TS_IRI}implementsInterface",
    label="implementsInterface",
    comment="Links a TypeScript class to the interface(s) it implements.",
    domain=(f"{UKO_TS_IRI}TypeScriptClass",),
    range_uri=f"{UKO_TS_IRI}TypeScriptInterface",
    is_object_property=True,
)


# ---------------------------------------------------------------------------
# Layer 2 dependencies
# ---------------------------------------------------------------------------

_TS_LAYER2_DEPS = (
    Layer2Dependency(
        uri=f"{UKO_OO_IRI}Class",
        prefix="uko-oo:",
        description="OO class (TypeScriptClass extends this).",
    ),
    Layer2Dependency(
        uri=f"{UKO_OO_IRI}Interface",
        prefix="uko-oo:",
        description="OO interface (TypeScriptInterface extends this).",
    ),
    Layer2Dependency(
        uri=f"{UKO_OO_IRI}Method",
        prefix="uko-oo:",
        description="OO method (TypeScriptFunction extends this).",
    ),
    Layer2Dependency(
        uri=f"{UKO_CODE_IRI}Module",
        prefix="uko-code:",
        layer=1,
        description="General code module (TypeScriptModule extends this).",
    ),
)


# ---------------------------------------------------------------------------
# DetailLevelMap: extends uko-oo: at SIGNATURES level
# ---------------------------------------------------------------------------

# TypeScript extends at SIGNATURES level — enriches the content at
# SIGNATURES depth with export markers, declare ambient types,
# generics, mapped/conditional types.  No new depth levels are
# inserted; the existing SIGNATURES level is enriched.
_TS_INSERTIONS: tuple[tuple[str, int], ...] = ()

TYPESCRIPT_DETAIL_LEVELS: tuple[tuple[str, int], ...] = build_detail_level_map(
    OO_EFFECTIVE_LEVELS, _TS_INSERTIONS
)


# ---------------------------------------------------------------------------
# Vocabulary definition
# ---------------------------------------------------------------------------

TYPESCRIPT_VOCABULARY = UKOVocabulary(
    namespace=UKO_TS_IRI,
    prefix=UKO_TS_PREFIX,
    layer=3,
    label="TypeScript Technology Vocabulary",
    comment=(
        "Layer 3 technology-specific vocabulary for TypeScript. "
        "Extends uko-oo: with export/default export markers, declare "
        "ambient types, generic type parameters, mapped and conditional types."
    ),
    parent_prefixes=("uko-oo:",),
    classes=(
        TypeScriptModule,
        TypeScriptClass,
        TypeScriptInterface,
        TypeScriptFunction,
    ),
    properties=(
        _ts_export_kind,
        _ts_is_ambient,
        _ts_generic_params,
        _ts_mapped_type,
        _ts_conditional_type,
        _ts_implements_interface,
    ),
    layer2_dependencies=_TS_LAYER2_DEPS,
    inserted_levels=(),
)
