'use strict';

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

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

var _keys2 = _interopRequireDefault(_keys);

var _reactRedux = require('react-redux');

var _FeatureDetail = require('../components/FeatureDetail');

var _FeatureDetail2 = _interopRequireDefault(_FeatureDetail);

var _features_by_id = require('../selectors/features_by_id');

var _features_by_id2 = _interopRequireDefault(_features_by_id);

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

var _field_analysis2 = _interopRequireDefault(_field_analysis);

var _action_creators = require('../action_creators');

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

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

exports.default = (0, _reactRedux.connect)(function (state, ownProps) {
  var featuresById = (0, _features_by_id2.default)(state);
  var fieldAnalysis = (0, _field_analysis2.default)(state);
  var id = ownProps.id || state.ui.featureId;
  var feature = featuresById[id];
  return {
    media: getMedia(feature, fieldAnalysis),
    coordFormat: state.settings.coordFormat,
    feature: feature,
    detailViewButtons: ownProps.detailViewButtons,
    fieldAnalysis: fieldAnalysis,
    fieldOrder: state.fieldOrder
  };
}, function (dispatch) {
  return {
    onEditFeature: function onEditFeature(feature) {
      return dispatch((0, _action_creators.editFeature)(feature));
    },
    onDeleteFeature: function onDeleteFeature(id) {
      return dispatch((0, _action_creators.deleteFeature)(id));
    }
  };
})(_FeatureDetail2.default);

// Gets all the media fields for a feature

function getMedia(feature, fieldAnalysis) {
  var media = [];
  if (!feature) return media;
  (0, _keys2.default)(fieldAnalysis.properties).forEach(function (fieldname) {
    if (fieldAnalysis.properties[fieldname].type !== _constants.FIELD_TYPE_IMAGE) return;
    if (!feature.properties[fieldname]) return;
    media.push({
      fieldname: fieldname,
      value: feature.properties[fieldname]
    });
  });
  return media;
}
//# sourceMappingURL=ConnectFeatureDetail.js.map