export type FlashMessages = {
  success?: string;
  error?: string;
};

/**
 * Build a redirect target that carries an inline success/error message via
 * query params. Consumed by <AdminFlash /> on the destination page (the
 * Post/Redirect/Get pattern). Replaces the removed toast transport.
 */
export function withFlash(pathname: string, flash: FlashMessages): string {
  const params = new URLSearchParams();

  if (flash.success) {
    params.set("success", flash.success);
  }

  if (flash.error) {
    params.set("error", flash.error);
  }

  const query = params.toString();

  return query ? `${pathname}?${query}` : pathname;
}

/** Read the flash messages from resolved searchParams. */
export function readFlash(
  searchParams?: { success?: string; error?: string } | null,
): FlashMessages {
  return {
    success: searchParams?.success,
    error: searchParams?.error,
  };
}
