//Dependencies.
let assign = require('object-assign');
let keys = require('lodash/object/keys');
let {clone, isEqual} = require('lodash/lang');
let _builder = require('./builder');
const ALL = 'ALL';




let _dispatchResult = (data) => {
    Focus.dispatcher.handleServerAction({
        data,
        type: 'update'
    });
};

module.exports = function(config){
  /**
   * Builded search action.
   * @param  {object} options - The options used to build the service, it should have the following structure:
   * ```javascript
   * {
   * 	criteria:{
   * 		query: 'The query typed by the user'
   * 		scope: 'The selected scope'
   * 	},
   * 	pageInfos: { //All this informations will be use to transform the data given the query, this will be passed into the URL.
   * 		page: 'The wanted page',
   * 		skip: 'Number of results to skip',
   * 		sortBy: 'The information on wich the data is sorted', // This is **OPTIONAL**
   * 		sortAsc: 'Is the sort ascending or descending, boolean'
   * 	},
   * 	group: 'On which FACET should we group' // This is **OPTIONAL**
   * 	selectedFacets: 'An object with the selected facets', // This is **OPTIONAL**
   * 	previousData: 'An object chich contains the previous data' // This is **OPTIONAL**.
   * 							It will be used only when the action is triggered by a pagination action such as a scroll or a pagination.
   * 	service:{
   * 		scope: "function which launch the scope search"
   * 		unScope: "function whoch launch the unscoped search"
   * 	}
   * }
   * ```
   * @return {function} - The builded search action.
   */
  return function searchAction(options){
    options = clone(options || {});


    ///////// COMMENT //////////
    // Could be nice to read all the info from the stores, so that no complex option needs to be passed to mutliple functions....

    let {scope, query} = options.criteria; //Cannot be read from the store ?
    if(query === ''){
      query = '*';
    }
    let urlData = assign(_builder.pagination(options.pageInfos), _builder.orderAndSort(options.pageInfos));

    if(scope === ALL){
      //Call the search action.
      options.service.scope(options);
    }else{
      //The component which call the serice should be know if it has all the data.
      if(options.previousData){ //Maybe rename pagination or something like that.
        config.service.unScope(options).then((response)=>{
            // Read the previous data from options.previous;
            return response;
        });
        //Read the totalCount

      }
      //The search is unscoped.


    }
  };
};

@@ -99,15 +99,8 @@
 et %7B
-clone, 
 isEq
@@ -167,16 +167,51 @@
 lder');%0A
+let _parser = require('./parser');%0A
 const AL
@@ -1632,296 +1632,257 @@
 ion(
-options)%7B%0A    options = clone(options %7C%7C %7B%7D);%0A%0A%0A    ///////// COMMENT //////////%0A    // Could be nice to read all the info from the stores, so that no complex option needs to be passed to mutliple functions....%0A%0A    let %7Bscope, query%7D = options.criteria; //Cannot be read from the store ?
+isScroll)%7B%0A    let %7B%0A      scope,%0A      query,%0A      facets,%0A      selectedFacets,%0A      groupingKey,%0A      sortBy,%0A      sortAsc,%0A      results,%0A      totalCount%0A      %7D = config.getSearchOptions();%0A    let nbSearchElement = config.nbSearchElement;
 %0A   
@@ -1950,16 +1950,23 @@
  assign(
+%0A      
 _builder
@@ -1981,71 +1981,250 @@
 ion(
-options.pageInfos), _builder.orderAndSort(options.pageInfos));%0A
+%7Bresults, totalCount, isScroll, nbSearchElement%7D),%0A      _builder.orderAndSort(%7BsortBy, sortAsc%7D)%0A    );%0A    let postData = %7B%0A      criteria: %7Bscope, query%7D,%0A      facets: _builder.facets(selectedFacets) %7C%7C %5B%5D,%0A      group: groupingKey%0A    %7D;
 %0A   
@@ -2281,23 +2281,22 @@
 .%0A      
-options
+config
 .service
@@ -2300,21 +2300,145 @@
 ice.
+un
 scope(
-options
+%7BurlData: urlData, data: postData%7D)%0A                    .then(_parser.unscopedResponse)%0A                    .then(_dispatchResult
 );%0A 
@@ -2534,90 +2534,8 @@
 ta.%0A
-      if(options.previousData)%7B //Maybe rename pagination or something like that.%0A
     
@@ -2553,19 +2553,17 @@
 service.
-unS
+s
 cope(opt
@@ -2648,16 +2648,57 @@
 evious;%0A
+            if(isScroll)%7B%0A%0A            %7D%0A
         
@@ -2765,49 +2765,8 @@
 nt%0A%0A
-      %7D%0A      //The search is unscoped.%0A%0A
 %0A   

Update search action