import 'package:open_location_code/open_location_code.dart';

/// Kampala center - used to shorten Plus Codes for Uganda
const _kampalaRef = LatLng(0.3476, 32.5825);

/// Builds a Plus Code string with locality (e.g. "9G5V+GV4, Kampala")
/// for storing with addresses and for display. Same format the driver uses for map directions.
String buildPlusCodeWithLocality(double lat, double lng, [String? locality]) {
  final loc = LatLng(lat, lng);
  final full = PlusCode.encode(loc);
  String codePart;
  try {
    codePart = full.shorten(_kampalaRef).toString();
  } catch (_) {
    codePart = full.toString();
  }
  final localityPart = locality?.trim();
  if (localityPart != null && localityPart.isNotEmpty) {
    return '$codePart, $localityPart';
  }
  return '$codePart, Kampala';
}
