from re import findall
from pathlib import Path
from typing import Dict


def _check_site(filepath: Path, site_code: str, gc_params: Dict) -> str:
    """Check if the site passed in matches that in the filename

    Args:
        filepath: Path to data file
        site: Site code
        gc_params: Dictionary of GCWERKS parameters
    Returns:
        str: Site code
    """

    site_data = gc_params["sites"]
    name_code_conversion = {value["gcwerks_site_name"]: site_code for site_code, value in site_data.items()}

    site_code = site_code.lower()
    site_name = findall(r"[\w']+", str(filepath.name))[0].lower()

    if len(site_code) > 3:
        raise ValueError("Please pass in a 3 letter site code as the site argument.")

    try:
        confirmed_code = name_code_conversion[site_name].lower()
    except KeyError:
        raise ValueError(f"Cannot match {site_name} to a site code.")

    if site_code != confirmed_code:
        raise ValueError(
            f"Mismatch between code reasd from filename: {confirmed_code} and that given: {site_code}"
        )

    return site_code