import { Location } from "react-router-dom";

/**
 * Determines which city layout/navigation should be active.
 * 
 * It reads the current pathname and the last known city (from sessionStorage)
 * to decide whether to show Landing or Melbourne navigation.
 */
export function getAutoNavigationSource(location: Location): "landing" | "melbourne" {
  const path = location.pathname;
  const storedCity = sessionStorage.getItem("lastCity");

  // ✅ When user is on a Melbourne page, remember it
  if (path.startsWith("/melbourne")) {
    sessionStorage.setItem("lastCity", "melbourne");
    return "melbourne";
  }

  // ✅ Shared pages — should use last city if available
  if (
    path.startsWith("/passes") ||
    path.startsWith("/how-it-works") || // added
    path.startsWith("/your-card") ||    // added
    path.startsWith("/checkout") ||
    path.startsWith("/faqs") ||
    path.startsWith("/about") ||
    path.startsWith("/contact")
  ) {
    // Use stored city if exists, otherwise default to landing
    return storedCity === "melbourne" ? "melbourne" : "landing";
  }

  // ✅ Default to landing
  return "landing";
}
