import { useSessionStorage } from './useSessionStorage';

const AUTH_REDIRECT_KEY = 'auth_redirect_url';

export interface AuthRedirectOptions {
  fallbackUrl?: string;
  clearOnUse?: boolean;
}

/**
 * Read redirect URL directly from sessionStorage (bypasses React state)
 * This is needed because different component instances have different React states
 */
const getRedirectFromStorage = (): string => {
  if (typeof window === 'undefined') return '';
  try {
    const item = window.sessionStorage.getItem(AUTH_REDIRECT_KEY);
    return item ? JSON.parse(item) : '';
  } catch {
    return '';
  }
};

/**
 * Clear redirect URL directly from sessionStorage
 */
const clearRedirectFromStorage = (): void => {
  if (typeof window === 'undefined') return;
  try {
    window.sessionStorage.removeItem(AUTH_REDIRECT_KEY);
    window.sessionStorage.removeItem(`${AUTH_REDIRECT_KEY}_timestamp`);
  } catch {
    // Ignore errors
  }
};

export const useAuthRedirectManager = (options: AuthRedirectOptions = {}) => {
  const { fallbackUrl = '/dashboard', clearOnUse = true } = options;
  const [redirectUrl, setRedirectUrl, removeRedirectUrl] = useSessionStorage<string>(AUTH_REDIRECT_KEY, '');

  const setRedirect = (url: string) => {
    setRedirectUrl(url);
  };

  const getRedirect = () => {
    // Read directly from storage to get latest value across components
    return getRedirectFromStorage() || redirectUrl;
  };

  const clearRedirect = () => {
    removeRedirectUrl();
    clearRedirectFromStorage();
  };

  const hasRedirect = () => {
    const stored = getRedirectFromStorage();
    return stored.length > 0 || redirectUrl.length > 0;
  };

  const getFinalRedirectUrl = () => {
    // Read directly from storage to get latest value across components
    const stored = getRedirectFromStorage();
    return stored || redirectUrl || fallbackUrl;
  };

  const useAndClearRedirect = () => {
    // Read directly from storage to ensure we get the latest saved URL
    // even if it was saved by a different component instance
    const stored = getRedirectFromStorage();
    const finalUrl = stored || redirectUrl || fallbackUrl;

    if (clearOnUse) {
      clearRedirect();
    }
    return finalUrl;
  };

  return {
    redirectUrl,
    setRedirect,
    getRedirect,
    clearRedirect,
    hasRedirect,
    getFinalRedirectUrl,
    useAndClearRedirect
  };
}; 