"""Geometry utilities for dashboard visualizations."""

import antimeridian
import streamlit as st
from shapely.geometry import shape


def fix_hex_geometry(geom):
    """Fix hexagon geometry crossing the antimeridian.

    This function handles geometries that cross the antimeridian (180° longitude)
    which can cause rendering issues in visualization libraries. Uses the antimeridian
    library to split and fix such geometries.

    Args:
        geom: A geometry object (typically from shapely or geojson).

    Returns:
        Fixed geometry object with antimeridian issues resolved.

    Note:
        If an error occurs, returns the original geometry unchanged.

    """
    try:
        return shape(antimeridian.fix_shape(geom))
    except ValueError as e:
        st.error(f"Error fixing geometry: {e}")
        return geom
