export const DEFAULT_AUTHENTICATED_ROUTE = '/reservation/orders'

export function normalizeAuthenticatedRoutePath(path: unknown): string | null {
  if (typeof path !== 'string') {
    return null
  }
  const trimmedPath = path.trim()
  if (!trimmedPath.startsWith('/') || trimmedPath.startsWith('//')) {
    return null
  }
  const [pathOnly = ''] = trimmedPath.split(/[?#]/, 1)
  if (pathOnly === '/' || pathOnly.startsWith('/login')) {
    return null
  }
  return trimmedPath
}

export function resolveAuthenticatedEntryPath(firstMenuPath: string | null | undefined): string {
  return normalizeAuthenticatedRoutePath(firstMenuPath) ?? DEFAULT_AUTHENTICATED_ROUTE
}

export function resolveLoginRedirectPath(
  redirect: unknown,
  firstMenuPath: string | null | undefined,
): string {
  return normalizeAuthenticatedRoutePath(redirect) ?? resolveAuthenticatedEntryPath(firstMenuPath)
}
