/*!
 * React Native Globalize
 *
 * Copyright 2015-2020 Josh Swan
 * Released under the MIT license
 * https://github.com/joshswan/react-native-globalize/blob/master/LICENSE
 */
import React, { useContext } from 'react';
import { Text } from 'react-native';
import { GlobalizeContext } from '../context';
export const filterTextProps = ({ accessible = true, accessibilityLabel = '', adjustsFontSizeToFit = false, allowFontScaling = true, style, ...props }) => [props, {
        accessible,
        accessibilityLabel,
        adjustsFontSizeToFit,
        allowFontScaling,
        style,
    }];
export const createPropFilter = (fn) => (props) => {
    const [filtered, textProps] = filterTextProps(props);
    return [fn(filtered), textProps];
};
var DisplayName;
(function (DisplayName) {
    DisplayName["formatCurrency"] = "FormattedCurrency";
    DisplayName["formatDate"] = "FormattedDate";
    DisplayName["formatMessage"] = "FormattedMessage";
    DisplayName["formatNumber"] = "FormattedNumber";
    DisplayName["formatRelativeTime"] = "FormattedRelativeTime";
    DisplayName["formatUnit"] = "FormattedUnit";
})(DisplayName || (DisplayName = {}));
const extractArgs = {
    formatCurrency: createPropFilter(({ currency, numberStyle = 'symbol', value, ...options }) => [value, currency, {
            useGrouping: true,
            ...options,
            style: numberStyle,
        }]),
    formatDate: createPropFilter(({ value, ...options }) => [value, options]),
    formatMessage: createPropFilter(({ defaultMessage, id, 
    /**
     * @deprecated Use "id" - still works for backwards compatibility
     */
    message, values = {}, ...rest }) => [id || message, { ...values, ...rest }, { defaultMessage }]),
    formatNumber: createPropFilter(({ numberStyle = 'decimal', value, ...options }) => [value, {
            useGrouping: true,
            ...options,
            style: numberStyle,
        }]),
    formatRelativeTime: createPropFilter(({ form, unit, value }) => [value, unit, { form }]),
    formatUnit: createPropFilter(({ unit, value, ...options }) => [value, unit, options]),
};
export function createFormattedComponent(name) {
    const Component = ({ children, ...props }) => {
        const globalize = useContext(GlobalizeContext);
        const [args, textProps] = extractArgs[name](props);
        // @ts-ignore
        const formatted = globalize[name](...args);
        return (React.createElement(Text, Object.assign({}, textProps),
            formatted,
            children));
    };
    Component.displayName = DisplayName[name];
    return Component;
}
