import { jsx as _jsx } from "react/jsx-runtime";
import { Fragment } from 'react';
/**
 * Function returns a function capable to translate tokens based on given translation pack and given locale.
 * The passed translation object should be correlated to given locale.
 * @param translation a translation pack (might be default or completely/partially overridden).
 * @param locale locale as BCP 47 language tag. Used for pluralization rules.
 */
const translatorFor = (translation, locale) => {
    const cardinalRules = new Intl.PluralRules(locale);
    const ordinalRules = new Intl.PluralRules(locale, { type: 'ordinal' });
    return ((literal, tokens = [], options) => {
        let template = translation[literal];
        if (!template)
            return `!unknown key: ${literal}`;
        const result = [];
        if (typeof template === 'object') {
            const type = (options?.pluralType === 'ordinal' ? ordinalRules : cardinalRules).select(options?.count || 0);
            template = template[type];
        }
        template.split(/\{(\d+)\}/g).forEach((chunk, index) => {
            const part = index % 2 ? tokens[Number(chunk)] : chunk;
            if (part !== undefined && part !== '')
                result.push(part);
        });
        if (result.every(part => typeof part === 'string' || typeof part === 'number'))
            return result.join('').trim();
        // eslint-disable-next-line react/no-array-index-key
        return result.map((part, i) => _jsx(Fragment, { children: part }, i));
    });
};
export default translatorFor;
//# sourceMappingURL=translate.js.map