'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _keys = require('babel-runtime/core-js/object/keys');

var _keys2 = _interopRequireDefault(_keys);

var _reselect = require('reselect');

var _union = require('lodash/union');

var _union2 = _interopRequireDefault(_union);

var _intersection = require('lodash/intersection');

var _intersection2 = _interopRequireDefault(_intersection);

var _best_fields = require('./best_fields');

var _best_fields2 = _interopRequireDefault(_best_fields);

var _filterable_fields = require('./filterable_fields');

var _filterable_fields2 = _interopRequireDefault(_filterable_fields);

var _date_field = require('./date_field');

var _date_field2 = _interopRequireDefault(_date_field);

var _field_analysis = require('./field_analysis');

var _field_analysis2 = _interopRequireDefault(_field_analysis);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
 * If we have not defined which fields to show filters for, make a best
 * guess by choosing the first date field we find, and the best filter
 * field (see `./best_field.js` for how this is guessed)
 */
var getFilterFields = (0, _reselect.createSelector)(function (state) {
  return state.filters;
}, function (state) {
  return state.filterFields;
}, _best_fields2.default, _date_field2.default, _filterable_fields2.default, _field_analysis2.default, function (state) {
  return state.fieldMapping;
}, function (filters, filterFields, bestFilterFields, dateFieldName, filterableFields, fieldAnalysis, fieldMapping) {
  // If the user hasn't selected any filters to show, choose some good defaults
  if (filterFields == null) {
    filterFields = [];
    if (dateFieldName) filterFields.push(dateFieldName);
    if (bestFilterFields[0]) filterFields.push(bestFilterFields[0].fieldname);
  }
  // Don't show filters for fields that are not filterable
  filterFields = (0, _intersection2.default)(filterFields, filterableFields);
  // Add the colored field if it is defined
  var coloredField = fieldMapping.color ? [fieldMapping.color] : [];
  // Fields that are in the current filter, but only ones that exist in our dataset
  var fieldsInCurrentFilter = (0, _keys2.default)(filters).filter(function (fieldName) {
    return fieldAnalysis.properties[fieldName];
  });
  // Filters that should be shown in the UI are the union of fields that have been
  // selected by the user with any fields that are in the current filter
  return (0, _union2.default)(filterFields, fieldsInCurrentFilter, coloredField);
});

exports.default = getFilterFields;
//# sourceMappingURL=filter_fields.js.map