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

import { APP_LOCALE_COOKIE, normalizeAppLocale, type AppLocale } from '@/lib/i18n/core';

export function getServerAppLocale(): AppLocale {
  const cookieLocale = cookies().get(APP_LOCALE_COOKIE)?.value;
  if (cookieLocale) {
    return normalizeAppLocale(cookieLocale);
  }

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

  return 'zh-CN';
}
