export const DEFAULT_LOGIN_URL = '/login';
export const DEFAULT_LOGIN_NEXT_URL = '/home';

export function getLoginRedirectURL(pathname: string, search: string): string {
  const urlSearchParams = new URLSearchParams(search);
  const nextPathname = pathname === DEFAULT_LOGIN_URL ? urlSearchParams.get('next') : pathname;
  if (!nextPathname || nextPathname === DEFAULT_LOGIN_URL) return '/login';
  urlSearchParams.delete('next');
  if (!urlSearchParams.toString()) return `${DEFAULT_LOGIN_URL}?next=${nextPathname}`;
  let next = nextPathname;
  if (urlSearchParams.toString()) {
    next += '?';
    next += urlSearchParams.toString().replace('&', ';');
  }
  return `${DEFAULT_LOGIN_URL}?next=${next}`;
}

export function getNextRedirectURL(next = DEFAULT_LOGIN_NEXT_URL) {
  return next.replace(';', '&');
}
