from pathlib import Path

from geo.errors import FormatError

EXTENSION_MAP: dict[str, str] = {
    ".shp": "ESRI Shapefile",
    ".gpkg": "GPKG",
    ".geojson": "GeoJSON",
    ".json": "GeoJSON",
    ".topojson": "TopoJSON",
    ".fgb": "FlatGeobuf",
}

WRITABLE_DRIVERS: set[str] = {
    "ESRI Shapefile",
    "GPKG",
    "GeoJSON",
    "FlatGeobuf",
}


def driver_for_path(path: str) -> str:
    ext = Path(path).suffix.lower()
    driver = EXTENSION_MAP.get(ext)
    if driver is None:
        raise FormatError(f"Unrecognized file extension: {ext}")
    return driver


def validate_writable(driver: str) -> None:
    if driver not in WRITABLE_DRIVERS:
        raise FormatError(f"Driver '{driver}' is read-only (no write support in OGR)")
