import { useCallback } from 'react';
import { useTranslation } from 'react-i18next';

import type allTranslations from '@humanddev/hu-translations';
import { type TOptions } from 'i18next';

type TranslationsType = typeof allTranslations;

type Namespaces =
  | keyof TranslationsType['es']
  | 'dashboard_sidebar_app'
  | 'clock';

export type LokaliseTFunction = (key: string, options?: TOptions) => string;

export const useLokaliseTranslation = (ns?: Namespaces | Namespaces[]) => {
  const { t, ...other } = useTranslation(ns);

  const newT = useCallback(
    (key: string, options?: TOptions) => {
      const lowercasedKey = key.toLowerCase();
      const lowercaseContext = options?.context?.toLowerCase();

      const translationWithLowercase = t(lowercasedKey, {
        ...options,
        context: lowercaseContext,
        defaultValue: '',
      });

      return translationWithLowercase || t(key, options);
    },
    [t],
  );

  return { t: newT, ...other };
};
