import { cookies, headers } from 'next/headers';

import { PORTAL_LOCALE_COOKIE, normalizePortalLocale, type PortalLocale } from '@/lib/i18n/core';

export function getServerPortalLocale(): PortalLocale {
  const cookieLocale = cookies().get(PORTAL_LOCALE_COOKIE)?.value;
  if (cookieLocale) {
    return normalizePortalLocale(cookieLocale);
  }

  const acceptLanguage = headers().get('accept-language');
  if (acceptLanguage) {
    return normalizePortalLocale(acceptLanguage);
  }

  return 'zh-CN';
}
