import React, { Fragment } from 'react';
import { SoftI18nConsumer } from '../Context';
import { processStringToChildren } from './SoftI18nUtil';
function throwError() {
    throw new Error('asdf');
}
function lookupToken(token, i18nMapping, valueDefault, i18nMappingFunc, values) {
    let renderable = (i18nMapping && i18nMapping[token]) || valueDefault;
    if (typeof renderable === 'function') {
        if (values === undefined) {
            return throwError();
        }
        else {
            // @ts-ignore-next-line
            // TypeScript complains that `DEFAULT` doesn't have a call signature
            // but we verified `renderable` is a function
            return renderable(values);
        }
    }
    else if (values === undefined || typeof renderable !== 'string') {
        if (i18nMappingFunc && typeof valueDefault === 'string') {
            renderable = i18nMappingFunc(valueDefault);
        }
        // there's a hole in the typings here as there is no guarantee that i18nMappingFunc
        // returned the same type of the default value, but we need to keep that assumption
        return renderable;
    }
    // @ts-ignore
    const children = processStringToChildren(renderable, values, i18nMappingFunc);
    if (typeof children === 'string') {
        // likewise, `processStringToChildren` returns a string or ReactChild[] depending on
        // the type of `values`, so we will make the assumption that the default value is correct.
        return children;
    }
    const Component = () => {
        return React.createElement(Fragment, null, children);
    };
    // same reasons as above, we can't promise the transforms match the default's type
    return React.createElement(Component, values);
}
function hasTokens(x) {
    return x.tokens != null;
}
// Must use the generics <T extends {}>
// If instead typed with React.FunctionComponent there isn't feedback given back to the dev
// when using a `values` object with a renderer callback.
const SoftI18n = (props) => (React.createElement(SoftI18nConsumer, null, i18nConfig => {
    const { mapping, mappingFunc } = i18nConfig;
    if (hasTokens(props)) {
        return props.children(props.tokens.map((token, idx) => lookupToken(token, mapping, props.defaults[idx], mappingFunc)));
    }
    const tokenValue = lookupToken(props.token, mapping, props.default, mappingFunc, props.values);
    if (props.children) {
        return props.children(tokenValue);
    }
    else {
        return tokenValue;
    }
}));
export { SoftI18n };
//# sourceMappingURL=SoftI18n.js.map