/**
* Create a query params Object
* Joi is already converting the query and params for the short name to the JSONAPI format
* ex: occupation to filter[occupation]
*/
const queryParamsAll = require('./query-params-all');
const queryParamsObjects = require('./query-params-objects');
const queryParamsPeople = require('./query-params-people');
const queryParmasDocuments = require('./query-params-documents');

module.exports = (typeFormat, queryParams) => {
  var result = {};

  // Main values
  result.query = queryParams.query;
  result.q = queryParams.query.q ? queryParams.query.q.trim() : undefined;
  result.pageNumber = parseInt(queryParams.query['page[number]']) || 0;
  result.pageSize = parseInt(queryParams.query['page[size]']) || 50;
  result.pageType = queryParams.query['page[type]'] || 'search';
  result.pageSort = queryParams.query['page[sort]'] || 'default';
  result.type = queryParams.params.type || queryParams.query['fields[type]'] || 'all';
  result.random = queryParams.query.random;

  result.filter = {all: {}, objects: {}, people: {}, documents: {}};

  // All
  Object.assign(result.filter.all, queryParamsAll(typeFormat, queryParams));

  // Objects
  Object.assign(result.filter.objects, queryParamsObjects(typeFormat, queryParams));
  Object.assign(result.filter.objects.categories, formatCategoryNames(result.filter.objects.categories));
  // People
  Object.assign(result.filter.people, queryParamsPeople(typeFormat, queryParams));

  // Documents
  Object.assign(result.filter.documents, queryParmasDocuments(typeFormat, queryParams));

  return result;
};

/*
Some cateogires contains a dash in their name.
By default the application convert space to dash on the front end
and dash to space on the backend. This function add dash for the
specific categories.
see issue #1008
*/
function formatCategoryNames (categories) {
  const categoryNames = {
    'x rays': 'x-rays',
    'medical ceramic ware': 'medical ceramic-ware',
    'medical glass ware': 'medical glass-ware',
    'pharmacy ware': 'pharmacy ware',
    'penn gaskell collection': 'penn-gaskell collection'
  };
  return categories.map(c => {
    return categoryNames[c.toLowerCase()] || c;
  });
}

@@ -1851,16 +1851,36 @@
 ries) %7B%0A
+ if (categories) %7B%0A 
   const 
@@ -1901,16 +1901,17 @@
 = %7B%0A    
+ 
 'x rays'
@@ -1922,16 +1922,17 @@
 -rays',%0A
+ 
     'med
@@ -1975,24 +1975,25 @@
 -ware',%0A    
+ 
 'medical gla
@@ -2020,24 +2020,25 @@
 lass-ware',%0A
+ 
     'pharmac
@@ -2067,16 +2067,17 @@
 e',%0A    
+ 
 'penn ga
@@ -2123,19 +2123,21 @@
 tion'%0A  
+ 
 %7D;%0A
+ 
   return
@@ -2159,16 +2159,17 @@
 (c =%3E %7B%0A
+ 
     retu
@@ -2214,10 +2214,46 @@
 ;%0A  
+ 
 %7D);%0A
+ %7D else %7B%0A   return categories;%0A %7D%0A
 %7D%0A

check categories exist, #1008