'use strict';

const filter = require('./filter-d5040f65.js');

const getDefaultFilterValues = (config, fields) => {
  const filterValues = {};
  config.fields.forEach(fieldConfig => {
    const field = fieldConfig.field || fields.find(f => f.id === fieldConfig.fieldId);
    if (field.value) {
      filterValues[field.id] = field.value;
    }
  });
  return filterValues;
};
const getFilterFields = (config, fields) => (config.fields.map(fieldConfig => fieldConfig.field || fields.find(f => f.id === fieldConfig.fieldId)));
const generateDefaultFilters = (config, fields) => {
  const filterFields = getFilterFields(config, fields);
  return generateFilters(filterFields, getDefaultFilterValues(config, filterFields));
};
const generateFilters = (filterFields, filterValues) => {
  const filters = [];
  const fieldFilters = generateFieldFilters(filterFields, filterValues);
  if (fieldFilters.filters.length) {
    filters.push(fieldFilters);
  }
  if (filters.length === 0) {
    return undefined;
  }
  else if (filters.length === 1) {
    return filters[0];
  }
  else {
    return {
      operator: 'and',
      filters,
    };
  }
};
const generateFieldFilters = (filterFields, filterValues) => ({
  operator: 'and',
  filters: filterFields
    .filter(field => !!filterValues[field.id])
    .map(field => ({
    comparison: filter.Comparison.Eq,
    id: field.id,
    value: filterValues[field.id],
  }))
});

exports.generateDefaultFilters = generateDefaultFilters;
exports.generateFilters = generateFilters;
exports.getDefaultFilterValues = getDefaultFilterValues;
