/* eslint prefer-arrow-callback:0 */

import 'intl';
import {Suite} from 'benchmark';
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import {
    IntlProvider,
    FormattedNumber,
    FormattedDate,
    FormattedMessage,
    FormattedRelative,
} from '../../lib/';

const suite = new Suite('renderToString', {
    onCycle: function (e) {
        console.log(String(e.target));
    },
});

suite.on('error', function (e) {
    throw e.target.error;
});

const intlProvider = new IntlProvider({locale: 'en'}, {});

suite.add('intlProvider.getChildContext()', function () {
    intlProvider.getChildContext();
});

const intlProviderContext = intlProvider.getChildContext();
const intlProvider2 = new IntlProvider({locale: 'en'}, intlProviderContext);

suite.add('intlProvider.shouldComponentUpdate()', function () {
    intlProvider2.shouldComponentUpdate(
        {locale: 'en'},
        intlProvider2.state,
        intlProviderContext
    );
});

suite.add('<div>', function () {
    ReactDOMServer.renderToString(
        <div />
    );
});

suite.add('<IntlProvider>', function () {
    ReactDOMServer.renderToString(
        <IntlProvider locale='en'>
            <div />
        </IntlProvider>
    );
});

suite.add('100 x <FormattedNumber>', function () {
    let formattedNumbers = [];
    for (let i = 0, len = 100; i < len; i += 1) {
        formattedNumbers.push(
            <FormattedNumber value={i} key={i} />
        );
    }

    ReactDOMServer.renderToString(
        <IntlProvider locale='en'>
            <div>{formattedNumbers}</div>
        </IntlProvider>
    );
});

suite.add('100 x <FormattedDate>', function () {
    let now = Date.now();
    let formattedDates = [];
    for (let i = 0, len = 100; i < len; i += 1) {
        formattedDates.push(
            <FormattedDate value={now - (1000 * 60 * i)} key={i} />
        );
    }

    ReactDOMServer.renderToString(
        <IntlProvider locale='en'>
            <div>{formattedDates}</div>
        </IntlProvider>
    );
});

suite.add('100 x <FormattedMessage>', function () {
    let messages = {};
    let formattedMessages = [];
    for (let i = 0, len = 100; i < len; i += 1) {
        messages[i] = `message ${i}`;
        formattedMessages.push(
            <FormattedMessage id={`${i}`} key={i} />
        );
    }

    ReactDOMServer.renderToString(
        <IntlProvider locale='en' messages={messages}>
            <div>{formattedMessages}</div>
        </IntlProvider>
    );
});

suite.add('100 x <FormattedMessage> with placeholder', function () {
    let messages = {};
    let formattedMessages = [];
    for (let i = 0, len = 100; i < len; i += 1) {
        messages[i] = `message {${i}, number}`;
        formattedMessages.push(
            <FormattedMessage id={`${i}`} values={{[i]: i}} key={i} />
        );
    }

    ReactDOMServer.renderToString(
        <IntlProvider locale='en' messages={messages}>
            <div>{formattedMessages}</div>
        </IntlProvider>
    );
});

suite.add('100 x <FormattedRelative>', function () {
    let now = Date.now();
    let formattedRelativeTimes = [];
    for (let i = 0, len = 100; i < len; i += 1) {
        formattedRelativeTimes.push(
            <FormattedRelative value={now - (1000 * 60 * i)} key={i} />
        );
    }

    ReactDOMServer.renderToString(
        <IntlProvider locale='en'>
            <div>{formattedRelativeTimes}</div>
        </IntlProvider>
    );
});

suite.run();

@@ -537,33 +537,33 @@
 %7D);%0A%0Asuite.add('
-i
+I
 ntlProvider.getC
@@ -549,33 +549,33 @@
 dd('IntlProvider
-.
+#
 getChildContext(
@@ -782,17 +782,17 @@
 te.add('
-i
+I
 ntlProvi
@@ -794,17 +794,17 @@
 Provider
-.
+#
 shouldCo
@@ -2998,32 +2998,550 @@
 er%3E%0A    );%0A%7D);%0A%0A
+suite.add('100 x %3CFormattedMessage%3E with placeholder, cached', function () %7B%0A    let messages = %7B%7D;%0A    let formattedMessages = %5B%5D;%0A    for (let i = 0, len = 100; i %3C len; i += 1) %7B%0A        messages%5Bi%5D = %60message %7B$%7B0%7D, number%7D%60;%0A        formattedMessages.push(%0A            %3CFormattedMessage id=%7B%60$%7Bi%7D%60%7D values=%7B%7B0: i%7D%7D key=%7Bi%7D /%3E%0A        );%0A    %7D%0A%0A    ReactDOMServer.renderToString(%0A        %3CIntlProvider locale='en' messages=%7Bmessages%7D%3E%0A            %3Cdiv%3E%7BformattedMessages%7D%3C/div%3E%0A        %3C/IntlProvider%3E%0A    );%0A%7D);%0A%0A
 suite.add('100 x

Tweak benchmark tests (#484)