import type { Role } from "@/lib/permissions";

/**
 * After login/register: preserve deep links; map bare `/dashboard` to hồ sơ for viewers (ApplicationReviewPage).
 */
export function resolvePostLoginPath(role: Role, fromPathname?: string | null): string {
  const raw = fromPathname?.trim() || "";
  const normalized = raw.replace(/\/$/, "") || "";
  if (normalized && normalized !== "/dashboard") {
    return raw.startsWith("/") ? raw : `/${raw}`;
  }
  if (role === "viewer") {
    return "/dashboard/documents";
  }
  return "/dashboard";
}

/** Header/hero “Dashboard” entry: staff stay on list home; applicants open hồ sơ. */
export function getAuthenticatedDashboardPath(hasRole: (role: Role) => boolean): string {
  if (hasRole("admin") || hasRole("editor")) {
    return "/dashboard";
  }
  return "/dashboard/documents";
}
