import { ROUTING_LOCALES } from '../../shared/constants';

type Locale = (typeof ROUTING_LOCALES)[number];

function stripLocalePrefix(pathname: string): string {
  const segments = pathname.split('/');
  const [_, potentialLocale, ...rest] = segments;

  if (ROUTING_LOCALES.includes(potentialLocale as Locale)) {
    const remainder = rest.join('/');
    return remainder ? `/${remainder}` : '/';
  }

  return pathname;
}

export const getPathname = () => {
  return typeof window !== 'undefined' ? stripLocalePrefix(window.location.pathname) : '/';
};
