export const ROUTE_PRE = process.env.NEXT_PUBLIC_ROUTE_PRE || '';

function normalizePath(path: string): string {
  return path.replace(/\/+$/, '');
}

export function buildRoute(path: string): string {
  return normalizePath(path.startsWith('/') ? path : `/${path}`);
}

export function matchRoute(pathname: string, route: string): boolean {
  const normalizedPathname = normalizePath(pathname);
  const normalizedRoute = normalizePath(route.startsWith('/') ? route : `/${route}`);
  
  if (!ROUTE_PRE) {
    return normalizedPathname === normalizedRoute;
  }
  
  const basePath = `/${ROUTE_PRE}`;
  const strippedPathname = normalizedPathname.startsWith(basePath) 
    ? normalizedPathname.slice(basePath.length) 
    : normalizedPathname;
  
  return strippedPathname === normalizedRoute;
}