'use client';

import React, { createContext } from 'react';
import { getTranslateFn } from '../utils';
import { usePzParams } from '../hooks/use-pz-params';
import { Currency, Locale } from '../types';
import settings from 'settings';

export const LocalizationContext = createContext(
  {} as {
    translate: (path: string) => string;
    locale: string;
    currency: string;
    locales: Locale[];
    currencies: Currency[];
    defaultLocaleValue: string;
    defaultCurrencyCode: string;
    localeUrlStrategy?: string;
  }
);

export default function LocalizationProvider({
  children,
  translations
}: {
  children: React.ReactNode;
  translations: any;
}) {
  const {
    locales,
    currencies,
    defaultLocaleValue,
    defaultCurrencyCode,
    localeUrlStrategy
  } = settings.localization;

  const { locale, currency } = usePzParams();

  return (
    <LocalizationContext.Provider
      value={{
        translate: (path: string) => getTranslateFn(path, translations),
        locale,
        currency,
        locales,
        currencies,
        defaultLocaleValue,
        defaultCurrencyCode,
        localeUrlStrategy
      }}
    >
      {children}
    </LocalizationContext.Provider>
  );
}
