"use client";

import { ROUTES } from "@/router/routes";

import { consumePendingChatImageReturn } from "./chat_image_return_session";

export type SubscriptionReturnTo = "chat" | null;

export function consumeSubscriptionExplicitExitUrl(): string | null {
  const pendingImageReturn = consumePendingChatImageReturn();
  if (pendingImageReturn) return pendingImageReturn.returnUrl;
  return null;
}

export function getSubscriptionFallbackExitUrl(
  returnTo: SubscriptionReturnTo,
): string {
  return returnTo === "chat" ? ROUTES.chat : ROUTES.sidebar;
}

export function consumeSubscriptionExitUrl(
  returnTo: SubscriptionReturnTo,
): string {
  return (
    consumeSubscriptionExplicitExitUrl() ??
    getSubscriptionFallbackExitUrl(returnTo)
  );
}
