import { replaceAllSafe } from "@/lib/helpers/core.helper";
import _ from "lodash";
import faStorage from "./locales/fa.json";

export enum I18nLanguage {
    FA = 'FA'
}

export const i18nStorage: Record<I18nLanguage, any> = {
    FA: faStorage,
};

export const i18n = (identifier: string, values: Record<string, any> = {}, locale: I18nLanguage.FA = I18nLanguage.FA): string => {
    const [ns, key] = identifier.split(':');
    if (ns && key) {
        let result = i18nStorage[locale][ns];
        if (!result) {
            return identifier;
        }
        let result2: string = result[key];
        _.map(values, (value, key) => {
            result2 = replaceAllSafe(result2, `{${key}}`, value);
        });
        return result2;
    }
    return identifier
}