from .hantzsch_widman import hw_spec_for_name, spec_cites_indicated_hydrogen
from .naming_data import load_json_table
from .nomenclature import RULES


def _indicated_h_retained_names() -> frozenset[str]:
    """
    Retained parents that carry an indicated hydrogen, read off the templates.
    """

    return frozenset(
        row["indicated_hydrogen_name"]
        for row in load_json_table("retained_fused_graph_templates.json").get("parents", ())
        if "indicated_hydrogen_name" in row
    )


INDICATED_H_RETAINED_NAMES = _indicated_h_retained_names()


def cites_indicated_hydrogen(retained_name: str | None) -> bool:
    """
    Whether a parent name spells its own indicated hydrogen ("1H-azepine").
    """

    if retained_name is None:
        return False
    spec = hw_spec_for_name(retained_name) or _monocycle_spec(retained_name)
    if spec is not None:
        return spec_cites_indicated_hydrogen(spec)
    return retained_name in INDICATED_H_RETAINED_NAMES


def _monocycle_spec(name: str) -> dict | None:
    return next((spec for spec in RULES.retained.monocycle_specs if spec["name"] == name), None)


INDICATED_H_ELEMENTS = frozenset({"C", "N", "P", "As", "Sb", "Bi", "B", "Si", "Ge", "Sn", "Pb"})
ALKYL_OXY_PREFIXES = RULES.heteroatoms.alkyl_oxy_prefixes
SIMPLE_SULFANYL_PREFIXES = RULES.heteroatoms.simple_sulfanyl_prefixes
SIMPLE_SELANYL_PREFIXES = RULES.heteroatoms.simple_selanyl_prefixes
HALOGEN_PREFIXES = RULES.heteroatoms.halogen_prefixes
HALOGEN_LAMBDA_SUFFIXES = RULES.heteroatoms.halogen_lambda_suffixes
RETAINED_RING_ELEMENTS = RULES.retained.ring_elements
DIRECT_GROUP_PREFIXES = RULES.prefixes.direct_group_prefixes
CHAIN_EXTERNAL_CARBONYL_GROUPS = RULES.components.chain_external_carbonyl_groups
PREFIX_GROUPS_TO_SKIP = RULES.prefixes.skip_groups
ESTER_LIKE_PREFIX_GROUPS = RULES.prefixes.ester_like_groups
PEROXY_ESTER_GROUPS = RULES.prefixes.peroxy_ester_groups
AMIDE_LIKE_PREFIX_GROUPS = RULES.prefixes.amide_like_groups
AMIDE_PREFIX_BASES = RULES.prefixes.amide_bases
CARBOXY_PREFIX_GROUPS = RULES.prefixes.carboxy_groups
CYANO_PREFIX_GROUPS = RULES.prefixes.cyano_groups
ACID_HALIDE_PREFIXES = RULES.prefixes.acid_halide_prefixes
PEROXY_ACID_PREFIX_GROUPS = RULES.prefixes.peroxy_acid_groups
SULFONYL_PREFIX_GROUPS = RULES.prefixes.sulfonyl_groups
DIRECT_PREFIX_GROUPS = RULES.prefixes.direct_prefixes
FRONT_MODIFIER_PRINCIPAL_GROUPS = RULES.components.front_modifier_principal_groups
N_SUBSTITUENT_PRINCIPAL_GROUPS = RULES.components.n_substituent_principal_groups
HYDRAZONE_PRINCIPAL_GROUPS = RULES.components.hydrazone_principal_groups
SINGLE_ATOM_CATIONS = RULES.ions.single_atom_cations
SINGLE_ATOM_ANIONS = RULES.ions.single_atom_anions
SALT_METAL_NAMES = RULES.components.salt_metal_names
