/** @jsx React.DOM */
'use strict';


var React = require('react');

var models = require('./models'),
  webservices = require('./webservices');


var appconfig = global.appconfig;


function init() {
  window.onerror = function(errorMsg, url, lineNumber) {
    if ( ! appconfig.debug) {
      alert(appconfig.i18n.onerrorMessage);
      // TODO call send mail webservice.
      // window.location.reload();
    }
    return false;
  };

  var enabledModules = appconfig.enabledModules;
  if (enabledModules.auth) {
    var auth = require('./auth');
    auth.init(enabledModules.auth);
  }
  var jsModal = document.getElementById('js-modal');
  if (enabledModules.acceptCookiesModal) {
    var AcceptCookiesModal = require('./components/accept-cookies-modal');
    webservices.fetchCurrentLocaleMessages(messages => {
      React.renderComponent(
        <AcceptCookiesModal actionUrlPath={enabledModules.acceptCookiesModal.actionUrlPath} messages={messages} />,
        jsModal
      );
    });
  }
  else if (enabledModules.acceptCnilConditionsModal) {
    var AcceptCnilConditionsModal = require('./components/accept-cnil-conditions-modal');
    webservices.fetchCurrentLocaleMessages(messages => {
      React.renderComponent(
        <AcceptCnilConditionsModal
          actionUrlPath={enabledModules.acceptCnilConditionsModal.actionUrlPath}
          messages={messages}
          privacyPolicyUrlPath={enabledModules.acceptCnilConditionsModal.privacyPolicyUrlPath}
        />,
        jsModal
      );
    });
  }
  if ( ! enabledModules.acceptCookiesModal && ! enabledModules.acceptCnilConditionsModal) {
    if (enabledModules.disclaimer) {
      var disclaimer = require('./disclaimer');
      disclaimer.init(enabledModules.disclaimer);
    }
  }
  if (enabledModules.legislation) {
    var legislation = require('./legislation');
    legislation.init(enabledModules.legislation);
  }
  if (enabledModules.situationForm) {
    // TODO use promise.all()
    webservices.fetchEntitiesMetadata(entitiesMetadata => {
      webservices.fetchCurrentLocaleMessages(messages => {
        webservices.fetchFields(entitiesMetadata, fields => {
          if (fields) {
            if (fields.error) {
               throw new Error(fields.error);
            }
            var {columns, columnsTree} = fields;
            var Simulator = require('./components/simulator'),
              mountNode = document.getElementById('simulator-container');
            var formats = {
              number: {
                currencyStyle: {
                  currency: 'EUR', // TODO parametrize in appconfig
                  style: 'currency',
                },
              },
            };
            React.renderComponent(
              <Simulator
                columns={columns}
                columnsTree={columnsTree}
                disableSave={ !! enabledModules.acceptCookiesModal}
                entitiesMetadata={entitiesMetadata}
                formats={formats}
                locales={appconfig.i18n.lang}
                messages={messages}
              />,
              mountNode
            );
          } else {
            throw new Error('Unable to fetch fields');
          }
        });
      });
    });
  }
}

module.exports = {init: init};

@@ -2027,60 +2027,288 @@
     
-webservices.fetchCurrentLocaleMessages(messages =%3E %7B
+if (! entitiesMetadata) %7B%0A        throw new Error('entitiesMetadata are empty');%0A      %7D%0A      webservices.fetchCurrentLocaleMessages(messages =%3E %7B%0A        if (! messages) %7B%0A          throw new Error('messages are empty');%0A        %7D%0A        // TODO fetch fields after loading app?
 %0A   
@@ -2376,24 +2376,26 @@
         if (
+! 
 fields) %7B%0A  
@@ -2390,24 +2390,83 @@
 fields) %7B%0A  
+          throw new Error('fields are empty');%0A          %7D%0A
           if
@@ -2479,26 +2479,24 @@
 ds.error) %7B%0A
-  
             
@@ -2529,38 +2529,34 @@
 ror);%0A          
-  %7D%0A  
+%7D%0A
           var %7Bc
@@ -2592,26 +2592,24 @@
 ;%0A          
-  
 var Simulato
@@ -2647,18 +2647,16 @@
 ator'),%0A
-  
         
@@ -2725,26 +2725,24 @@
 ;%0A          
-  
 var formats 
@@ -2757,18 +2757,16 @@
         
- 
- 
 number: 
@@ -2763,26 +2763,24 @@
   number: %7B%0A
-  
             
@@ -2794,26 +2794,24 @@
 ncyStyle: %7B%0A
-  
             
@@ -2880,18 +2880,16 @@
         
- 
- 
 style: '
@@ -2905,39 +2905,35 @@
 ,%0A              
-  
 %7D,%0A
-  
             %7D,%0A 
@@ -2923,34 +2923,32 @@
 %0A            %7D,%0A
-  
           %7D;%0A   
@@ -2950,26 +2950,24 @@
 ;%0A          
-  
 React.render
@@ -2989,18 +2989,16 @@
         
-  
 %3CSimulat
@@ -3000,18 +3000,16 @@
 mulator%0A
-  
         
@@ -3042,26 +3042,24 @@
             
-  
 columnsTree=
@@ -3072,18 +3072,16 @@
 nsTree%7D%0A
-  
         
@@ -3152,18 +3152,16 @@
         
-  
 entities
@@ -3202,18 +3202,16 @@
         
- 
- 
 formats=
@@ -3220,18 +3220,16 @@
 ormats%7D%0A
-  
         
@@ -3270,34 +3270,32 @@
 %7D%0A              
-  
 messages=%7Bmessag
@@ -3290,34 +3290,32 @@
 ages=%7Bmessages%7D%0A
-  
             /%3E,%0A
@@ -3318,34 +3318,32 @@
 /%3E,%0A            
-  
 mountNode%0A      
@@ -3350,101 +3350,11 @@
     
-  
 );%0A
-          %7D else %7B%0A            throw new Error('Unable to fetch fields');%0A          %7D%0A  
     

Enforce AJAX responses checks