const { pick } = require('lodash');

function initFields(options = {}) {
  if (!options.default || !options.base) throw new Error('Options default and base are required');

  return (req, res, next) => {
    if (req.query.fields) {
      req.fields = new Set(req.query.fields.split(','));
    } else {
      req.fields = new Set(options.default);
    }
    options.base.forEach(field => req.fields.add(field));
    next();
  };
}

function initFormat(options = {}) {
  if (options.geometries && !options.defaultGeometry) throw new Error('defaultGeometry is required');
  if (options.defaultGeometry && !options.geometries.includes(options.defaultGeometry))
    throw new Error('defaultGeometry is not in geometry list');
  const acceptedFormats = ['json'];
  if (options.geometries) acceptedFormats.push('geojson');

  return (req, res, next) => {
    req.outputFormat = acceptedFormats.includes(req.query.format) ? req.query.format : 'json';
    if (req.outputFormat === 'geojson') {
      req.geometries = options.geometries;
      req.defaultGeometry = options.defaultGeometry;
      options.geometries.forEach(geometry => req.fields.delete(geometry));
    }
    next();
  };
}

function formatOne(req, target) {
  const properties = pick(target, Array.from(req.fields));

  if (target.codeDepartement && req.fields.has('departement')) {
    const departement = req.db.departements.queryByCode(target.codeDepartement)[0];
    properties.departement = pick(departement, 'code', 'nom');
  }

  if (target.codeRegion && req.fields.has('region')) {
    const region = req.db.regions.queryByCode(target.codeRegion)[0];
    properties.region = pick(region, 'code', 'nom');
  }

  if (req.outputFormat === 'geojson') {
    const geom = req.geometries.includes(req.query.geometry) ? req.query.geometry : req.defaultGeometry;
    return {
      type: 'Feature',
      properties,
      geometry: target[geom],
    };
  } else {
    return properties;
  }
}

module.exports = { initFields, initFormat, formatOne };

@@ -1378,28 +1378,31 @@
 tements.
-queryByCode(
+search(%7B code: 
 target.c
@@ -1415,16 +1415,18 @@
 artement
+ %7D
 )%5B0%5D;%0A  
@@ -1584,20 +1584,23 @@
 ons.
-queryByCode(
+search(%7B code: 
 targ
@@ -1612,16 +1612,18 @@
 deRegion
+ %7D
 )%5B0%5D;%0A  

Fix #65