/**
 * Checkout Navigation Utilities
 *
 * Provides URL building functions for checkout flow navigation.
 */

/**
 * Builds the URL to navigate back to the configure page from checkout.
 * Preserves query parameters except 'type'.
 */
export function buildConfigureBackUrl(searchParams: URLSearchParams | null): string {
  const params = new URLSearchParams(searchParams?.toString() ?? "");
  const type = (params.get("type") ?? "").toLowerCase();
  params.delete("type");
  const planSku = params.get("planSku")?.trim();
  if (!planSku) {
    params.delete("planSku");
  }

  const queryString = params.toString();
  const query = queryString ? `?${queryString}` : "";

  if (type === "sim") {
    return `/account/services/sim/configure${query}`;
  }
  if (type === "internet" || type === "") {
    return `/account/services/internet/configure${query}`;
  }
  return "/account/services";
}

/**
 * Builds the URL to redirect to verification page, preserving the return path.
 */
export function buildVerificationRedirectUrl(
  pathname: string,
  searchParams: URLSearchParams | null
): string {
  const queryString = searchParams?.toString();
  const next = pathname + (queryString ? `?${queryString}` : "");
  return `/account/settings/verification?returnTo=${encodeURIComponent(next)}`;
}

/**
 * Builds the URL to navigate to order success page.
 */
export function buildOrderSuccessUrl(sfOrderId: string): string {
  return `/account/orders/${encodeURIComponent(sfOrderId)}?status=success`;
}

/**
 * Gets the shop href based on pathname prefix.
 */
export function getShopHref(pathname: string): string {
  return pathname.startsWith("/account") ? "/account/services" : "/services";
}
