import "package:fixnum/fixnum.dart";
import "package:google_maps_flutter/google_maps_flutter.dart";

List<LatLng> decodePolyline(String polyline) {
  final points = <LatLng>[]; // List to hold the decoded coordinates
  var index = 0;
  final len = polyline.length;
  var lat = Int32();
  var lng = Int32();

  // While there are still characters to process in the polyline
  while (index < len) {
    int b;
    var shift = 0;
    var result = Int32();
    final oneF = Int32(0x1F);

    // Decode latitude
    do {
      b = polyline.codeUnitAt(index++) - 63; // Get the next character
      result |= (Int32(b) & oneF) << shift; // Update the result
      shift += 5; // Increase the shift
    } while (b >= 0x20); // Continue until a character less than 0x20 is found

    final dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); // Decode the latitude delta
    lat = (lat + dlat) as Int32; // Update the latitude

    shift = 0; // Reset shift for longitude decoding
    result = Int32(); // Reset result for longitude decoding

    // Decode longitude
    do {
      b = polyline.codeUnitAt(index++) - 63; // Get the next character
      result |= (Int32(b) & oneF) << shift; // Update the result
      shift += 5; // Increase the shift
    } while (b >= 0x20); // Continue until a character less than 0x20 is found

    final dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); // Decode the longitude delta
    lng = (lng + dlng) as Int32; // Update the longitude

    // Add the new LatLng poInt32 to the list of points
    points.add(LatLng(lat.toInt() / 1E5, lng.toInt() / 1E5));
  }
  return points;
}
