'use strict';

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

var _reselect = require('reselect');

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

var _best_fields2 = _interopRequireDefault(_best_fields);

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

var _field_analysis2 = _interopRequireDefault(_field_analysis);

var _filter_fields = require('./filter_fields');

var _filter_fields2 = _interopRequireDefault(_filter_fields);

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

var _date_field2 = _interopRequireDefault(_date_field);

var _constants = require('../constants');

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

var getTitleFieldName = (0, _reselect.createSelector)(_best_fields2.default, _date_field2.default, function (bestFilterFields, dateFieldName) {
  return bestFilterFields[0] && bestFilterFields[0].fieldname || dateFieldName || 'No Title';
});

var getSubtitleFieldName = (0, _reselect.createSelector)(_best_fields2.default, function (bestFilterFields) {
  return bestFilterFields[1] && bestFilterFields[1].fieldname;
});

var getMediaFieldName = (0, _reselect.createSelector)(_field_analysis2.default, function (fieldAnalysis) {
  var mediaField = void 0;
  for (var fieldname in fieldAnalysis.properties) {
    var field = fieldAnalysis.properties[fieldname];
    if (field.type !== _constants.FIELD_TYPE_IMAGE && field.type !== _constants.FIELD_TYPE_VIDEO) continue;
    if (!mediaField) {
      mediaField = field;
      continue;
    }
    var imageVsVideo = field.type === _constants.FIELD_TYPE_IMAGE && mediaField.type === _constants.FIELD_TYPE_VIDEO;
    var higherCount = field.type === mediaField.type && field.count > mediaField.count;
    if (imageVsVideo || higherCount) {
      mediaField = field;
    }
  }
  return mediaField && mediaField.fieldname;
});

var getColoredFieldName = (0, _reselect.createSelector)(function (state) {
  return state.fieldMapping;
}, _field_analysis2.default, _filter_fields2.default, function (fieldMapping, fieldAnalysis, visibleFilters) {
  if (fieldMapping.color) return fieldMapping.color;
  var discreteFilters = visibleFilters.filter(function (fieldName) {
    return fieldAnalysis.properties[fieldName].filterType === _constants.FILTER_TYPE_DISCRETE;
  });
  return discreteFilters[0];
});

var getFieldMapping = (0, _reselect.createSelector)(function (state) {
  return state.fieldMapping;
}, getTitleFieldName, getSubtitleFieldName, getMediaFieldName, getColoredFieldName, _date_field2.default, function (fieldMapping, titleFieldName, subtitleFieldName, mediaFieldName, coloredFieldName, dateFieldName) {
  return {
    title: fieldMapping.title || titleFieldName,
    subtitle: fieldMapping.subtitle || subtitleFieldName,
    media: fieldMapping.media || mediaFieldName,
    color: coloredFieldName,
    date: dateFieldName
  };
});

exports.default = getFieldMapping;
//# sourceMappingURL=field_mapping.js.map