'use strict';

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _reactIntl = require('react-intl');

var _cspaceLayout = require('cspace-layout');

var _withConfig = require('../../enhancers/withConfig');

var _withConfig2 = _interopRequireDefault(_withConfig);

var _withRecordType = require('../../enhancers/withRecordType');

var _withRecordType2 = _interopRequireDefault(_withRecordType);

var _prefs = require('../../actions/prefs');

var _reducers = require('../../reducers');

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

const getHeader = (key, messages) => {
  const message = messages.panel[key];

  if (!message) {
    return null;
  }

  return _react2.default.createElement(
    'h3',
    null,
    _react2.default.createElement(_reactIntl.FormattedMessage, message)
  );
};

const mapStateToProps = (state, ownProps) => {
  const config = ownProps.config,
        msgkey = ownProps.msgkey,
        name = ownProps.name,
        recordType = ownProps.recordType;
  let header = ownProps.header;


  let collapsed = (0, _reducers.isPanelCollapsed)(state, recordType, name);

  if (typeof collapsed === 'undefined') {
    collapsed = ownProps.collapsed;
  }

  if (!header) {
    const messages = config.recordTypes[recordType].messages;


    header = getHeader(msgkey || name, messages);
  }

  return {
    collapsed,
    header
  };
};

const mapDispatchToProps = (dispatch, ownProps) => {
  const recordType = ownProps.recordType;


  return {
    onToggleCollapsed: (name, collapsed) => {
      dispatch((0, _prefs.collapsePanel)(recordType, name, collapsed));
    }
  };
};

const ConnectedPanel = exports.ConnectedPanel = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps)(_cspaceLayout.Panel);

const EnhancedConnectedPanel = (0, _withRecordType2.default)((0, _withConfig2.default)(ConnectedPanel));

EnhancedConnectedPanel.propTypes = _cspaceLayout.Panel.propTypes;

exports.default = EnhancedConnectedPanel;