"""Cell-line name utilities shared across SSL scripts.

This module intentionally has no heavy dependencies (no torch/numpy/PIL)
so that data-preprocessing scripts can import it quickly.
"""

from __future__ import annotations

CELL_LINE_NAMES = ["A172", "BT474", "BV2", "Huh7", "MCF7", "SHSY5Y", "SkBr3", "SK-OV-3"]


def normalize_cell_line_name(name: str) -> str:
    """Alphanumeric-only uppercase form, e.g. 'SK-OV-3' -> 'SKOV3'."""
    return "".join(ch for ch in name.upper() if ch.isalnum())


def infer_cell_line(file_name: str) -> int:
    """Return index into CELL_LINE_NAMES, or -1 if unknown."""
    name_upper = normalize_cell_line_name(file_name)
    for idx, cl in enumerate(CELL_LINE_NAMES):
        if normalize_cell_line_name(cl) in name_upper:
            return idx
    return -1


def match_cell_line(name: str) -> str:
    """Return canonical cell-line name matching user input.

    Raises ValueError if no match is found.
    """
    target = normalize_cell_line_name(name)
    if not target:
        raise ValueError(f"Empty cell line name (known: {CELL_LINE_NAMES})")
    for cl in CELL_LINE_NAMES:
        if target == normalize_cell_line_name(cl):
            return cl
    raise ValueError(f"Unknown cell line: {name} (known: {CELL_LINE_NAMES})")
