import React, { createContext, type PropsWithChildren, useContext, useMemo, } from 'react'; import {type DateUnitType, DateUtils} from './date'; type ContextValue = { locale: string; sortedDateUnitTypes: DateUnitType[]; monthLongNames: string[]; }; const DatePickerLocaleContext = createContext( undefined, ); type DatePickerLocaleProviderProps = PropsWithChildren<{ locale?: string; }>; const DatePickerLocaleProvider = ({ locale = 'en', children, }: DatePickerLocaleProviderProps) => { const value = useMemo( () => ({ locale, monthLongNames: DateUtils.getLocalizedMonthNames(locale), sortedDateUnitTypes: DateUtils.getSortedDateUnitPositions(locale), }), [locale], ); return ( {children} ); }; export default DatePickerLocaleProvider; export const useDatePickerLocale = () => { const value = useContext(DatePickerLocaleContext); if (value === undefined) { throw new Error( 'useDatePickerLocale must be called from within DatePickerLocaleContext.Provider!', ); } return useContext(DatePickerLocaleContext)!; };