import re


class FileTargeting:
    _path_pattern = re.compile(r"([A-Za-z0-9_.\-/]+?\.[A-Za-z0-9_]+)")

    def extract_target_path(self, message: str) -> str | None:
        text = (message or "").replace("\\", "/")
        candidates = self._path_pattern.findall(text)
        if not candidates:
            return None
        for candidate in candidates:
            cleaned = candidate.strip("`'\".,:;()[]{}")
            if "/" in cleaned or cleaned.startswith("."):
                return cleaned
        return candidates[0].strip("`'\".,:;()[]{}")

    def lookup_file(self, files_map: dict[str, dict], path: str | None) -> dict | None:
        if not path:
            return None
        normalized = path.replace("\\", "/")
        if normalized in files_map:
            return files_map[normalized]
        low = normalized.lower()
        for key, value in files_map.items():
            if key.lower() == low:
                return value
        return None
