import { isValidBranchParam } from "@/lib/frontend/params";

export const PRIMARY_NAV = Object.freeze({
	EXPLORER: "explorer",
	SEARCH: "search",
});

/**
 * Determine which primary navigation item should be active based on pathname.
 *
 * Rules:
 * - Search is active when pathname matches "/:branch/search" (or below).
 * - Explorer is active for any "/:branch/..." route that is not search.
 * - If the first segment is not a valid branch, no nav item is active.
 *
 * @param {string} pathname
 * @returns {{ active: "explorer"|"search", branch: string } | null}
 */
export function getPrimaryNavFromPathname(pathname) {
	if (typeof pathname !== "string" || !pathname.startsWith("/")) return null;

	const parts = pathname.split("/").filter(Boolean);
	if (parts.length === 0) return null;

	const branch = parts[0];
	if (!isValidBranchParam(branch)) return null;

	const active =
		parts[1] === "search" ? PRIMARY_NAV.SEARCH : PRIMARY_NAV.EXPLORER;

	return { active, branch };
}
