#
#   Imandra Inc.
#
#   speclogician/presentation/builders/_links.py
#

from __future__ import annotations

from speclogician.presentation.ctx import RenderCtx
from speclogician.presentation.models.predicate import LinkedArtifactsPM
from speclogician.data.traces import TestTrace, LogTrace
from speclogician.data.refs import DocRef, SrcCodeRef


def build_links_pm(comp_id: str, ctx: RenderCtx) -> LinkedArtifactsPM:
    links = LinkedArtifactsPM()

    if ctx.art_map is None:
        return links

    art_ids = list(ctx.art_map.get_arts_for_component(comp_id))
    links.total = len(art_ids)
    links.art_ids = art_ids if ctx.show_art_ids else []

    if ctx.art_cont is None:
        links.unresolved_ids = len(art_ids)
        return links

    unresolved = 0
    for aid in art_ids:
        a = ctx.art_cont.get_by_art_id(aid)
        if a is None:
            unresolved += 1
        elif isinstance(a, TestTrace):
            links.test_traces += 1
        elif isinstance(a, LogTrace):
            links.log_traces += 1
        elif isinstance(a, DocRef):
            links.doc_refs += 1
        elif isinstance(a, SrcCodeRef):
            links.src_code_refs += 1

    links.unresolved_ids = unresolved
    return links