import csv

from datatypes.epc.domain.historic_epc import HistoricEpc


def _normalise(value: str | None) -> str:
    if value is None:
        return ""
    return value.replace("\xa0", " ")


def read_historic_epc_csv(path: str) -> list[HistoricEpc]:
    with open(path, newline="", encoding="utf-8") as f:
        reader = csv.DictReader(f)
        return [
            HistoricEpc(**{k.lower(): _normalise(v) for k, v in row.items()})
            for row in reader
        ]
