"""Public API for parsing H5P semantics.json into directive IR."""

from __future__ import annotations

from typing import Any, Dict, List

from .classify import classify_fields, machine_name_to_directive
from .ir import DirectiveIR


def parse(
    machine_name: str,
    library: str,
    semantics: List[Dict[str, Any]],
) -> DirectiveIR:
    """Parse an H5P semantics field list into a DirectiveIR.

    Parameters
    ----------
    machine_name:
        H5P machine name, e.g. ``"H5P.TrueFalse"``.
    library:
        Full library string, e.g. ``"H5P.TrueFalse 1.8"``.
    semantics:
        The parsed ``semantics.json`` content (a list of field dicts).

    Returns
    -------
    DirectiveIR
        Typed intermediate representation of the directive.
    """
    directive_name = machine_name_to_directive(machine_name)
    body, options, structural_children, library_children = classify_fields(
        semantics, directive_name,
    )
    return DirectiveIR(
        machine_name=machine_name,
        directive_name=directive_name,
        library=library,
        body=body,
        options=options,
        structural_children=structural_children,
        library_children=library_children,
    )
