// Payment gateway return URLs (Telebirr/Waafi/D-Money success & failure pages) are fixed,
// app-wide URLs configured once in the payment provider — they can't carry a per-request
// query param telling the return page which flow initiated payment. The normal
// results -> seats -> review -> payment flow always ends at /booking/confirmation, which
// reads its data from useBookingStore. But paying for an existing booking from the
// Manage Booking page (/booking/detail) doesn't populate that store, so returning to
// /booking/confirmation there would render blank/broken.
//
// This marker records "the last payment was initiated from Manage Booking for booking
// ref X" right before redirecting to the gateway, so the return page can send the user
// back to that booking's detail view instead. It's consumed (read + cleared) exactly once.
const STORAGE_KEY = 'edr_manage_booking_payment_ref';

export function markManageBookingPaymentReturn(bookingRef: string) {
  if (typeof window === 'undefined' || !bookingRef) return;
  localStorage.setItem(STORAGE_KEY, bookingRef);
}

export function consumeManageBookingPaymentReturn(): string | null {
  if (typeof window === 'undefined') return null;
  const ref = localStorage.getItem(STORAGE_KEY);
  if (ref) localStorage.removeItem(STORAGE_KEY);
  return ref;
}
