// import ChartI18NContext from 'app/pages/ChartWorkbenchPage/contexts/Chart18NContext';
import { DATART_TRANSLATE_HOLDER} from "../../globalConstants";
import i18n from 'i18next';
// import get from 'lodash/get';
import { useCallback } from 'react';
import { useTranslation } from 'react-i18next';


export function prefixI18N(key) {
  return i18n.t(key);
}

function usePrefixI18N(prefix) {
  const { t, i18n } = useTranslation();
  // const { i18NConfigs: vizI18NConfigs } = useContext(ChartI18NContext);

  const cachedTranslateFn = useCallback(
    (key, disablePrefix = false, options) => {
      let translationKey = key;
      const usePrefix =
        !disablePrefix && !translationKey.includes(DATART_TRANSLATE_HOLDER);
      if (usePrefix && prefix) {
        translationKey = `${prefix}.${translationKey}`;
      }
      if (translationKey.includes(DATART_TRANSLATE_HOLDER)) {
        translationKey = translationKey.replace(
          `${DATART_TRANSLATE_HOLDER}.`,
          '',
        );
      }

      // const langTrans = vizI18NConfigs?.find(c =>
      //   c.lang.includes(i18n.language),
      // )?.translation;
      // const contextTranslation = get(langTrans, translationKey);
      return (
        // contextTranslation ||
        (t.call(Object.create(null), translationKey, options))
    );
    },
    [i18n.language, prefix, t],
  );

  // [i18n.language, prefix, t, vizI18NConfigs],

  return cachedTranslateFn;
}

export default usePrefixI18N;
