'use strict';

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

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

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

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

var _get = require('lodash/get');

var _get2 = _interopRequireDefault(_get);

var _pickBy = require('lodash/pickBy');

var _pickBy2 = _interopRequireDefault(_pickBy);

var _cspaceInput = require('cspace-input');

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

var _SearchButtonBar = require('./SearchButtonBar');

var _SearchButtonBar2 = _interopRequireDefault(_SearchButtonBar);

var _AdvancedSearchBuilder = require('./AdvancedSearchBuilder');

var _AdvancedSearchBuilder2 = _interopRequireDefault(_AdvancedSearchBuilder);

var _permissionHelpers = require('../../helpers/permissionHelpers');

var _PanelContainer = require('../../containers/layout/PanelContainer');

var _SearchForm = require('../../../styles/cspace-ui/SearchForm.css');

var _SearchForm2 = _interopRequireDefault(_SearchForm);

var _SearchFormRecordType = require('../../../styles/cspace-ui/SearchFormRecordType.css');

var _SearchFormRecordType2 = _interopRequireDefault(_SearchFormRecordType);

var _SearchFormVocab = require('../../../styles/cspace-ui/SearchFormVocab.css');

var _SearchFormVocab2 = _interopRequireDefault(_SearchFormVocab);

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

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var Label = _cspaceInput.components.Label,
    LineInput = _cspaceInput.components.LineInput,
    RecordTypeInput = _cspaceInput.components.RecordTypeInput,
    VocabularyInput = _cspaceInput.components.VocabularyInput;


var messages = (0, _reactIntl.defineMessages)({
  recordType: {
    'id': 'searchForm.recordType',
    'defaultMessage': 'Find'
  },
  vocabulary: {
    'id': 'searchForm.vocabulary',
    'defaultMessage': 'in vocabulary'
  },
  keyword: {
    'id': 'searchForm.keyword',
    'defaultMessage': 'Keywords'
  },
  fullTextSearch: {
    'id': 'searchForm.fullTextSearch',
    'defaultMessage': 'Full Text Search'
  }
});

var propTypes = {
  config: _propTypes2.default.object,
  intl: _reactIntl.intlShape,
  keywordValue: _propTypes2.default.string,
  recordTypeValue: _propTypes2.default.string,
  vocabularyValue: _propTypes2.default.string,
  advancedSearchCondition: _propTypes2.default.object,
  recordTypeInputReadOnly: _propTypes2.default.bool,
  recordTypeInputRootType: _propTypes2.default.string,
  recordTypeInputServiceTypes: _propTypes2.default.arrayOf(_propTypes2.default.string),
  showButtons: _propTypes2.default.bool,
  perms: _propTypes2.default.instanceOf(_immutable2.default.Map),
  onAdvancedSearchConditionCommit: _propTypes2.default.func,
  onKeywordCommit: _propTypes2.default.func,
  onRecordTypeCommit: _propTypes2.default.func,
  onVocabularyCommit: _propTypes2.default.func,
  onSearch: _propTypes2.default.func
};

var SearchForm = function (_Component) {
  _inherits(SearchForm, _Component);

  function SearchForm() {
    _classCallCheck(this, SearchForm);

    var _this = _possibleConstructorReturn(this, (SearchForm.__proto__ || Object.getPrototypeOf(SearchForm)).call(this));

    _this.formatRecordTypeLabel = _this.formatRecordTypeLabel.bind(_this);
    _this.formatVocabularyLabel = _this.formatVocabularyLabel.bind(_this);
    _this.handleFormSubmit = _this.handleFormSubmit.bind(_this);
    _this.handleKeywordInputCommit = _this.handleKeywordInputCommit.bind(_this);
    _this.handleRecordTypeDropdownCommit = _this.handleRecordTypeDropdownCommit.bind(_this);
    _this.handleVocabularyDropdownCommit = _this.handleVocabularyDropdownCommit.bind(_this);
    return _this;
  }

  _createClass(SearchForm, [{
    key: 'getRecordTypes',
    value: function getRecordTypes() {
      var _props = this.props,
          config = _props.config,
          perms = _props.perms;
      var recordTypes = config.recordTypes;


      return (0, _pickBy2.default)(recordTypes, function (recordTypeConfig, name) {
        var serviceType = (0, _get2.default)(recordTypeConfig, ['serviceConfig', 'serviceType']);

        if (serviceType === 'object' || serviceType === 'procedure' || serviceType === 'authority') {
          return (0, _permissionHelpers.canList)(name, perms);
        }

        return true;
      });
    }
  }, {
    key: 'formatRecordTypeLabel',
    value: function formatRecordTypeLabel(name, config) {
      var intl = this.props.intl;


      return intl.formatMessage(config.messages.record.collectionName) || name;
    }
  }, {
    key: 'formatVocabularyLabel',
    value: function formatVocabularyLabel(name, config) {
      var intl = this.props.intl;


      return intl.formatMessage(config.messages.name) || name;
    }
  }, {
    key: 'handleFormSubmit',
    value: function handleFormSubmit(event) {
      event.preventDefault();

      var onSearch = this.props.onSearch;


      if (onSearch) {
        onSearch();
      }
    }
  }, {
    key: 'handleKeywordInputCommit',
    value: function handleKeywordInputCommit(path, value) {
      var onKeywordCommit = this.props.onKeywordCommit;


      if (onKeywordCommit) {
        onKeywordCommit(value);
      }
    }
  }, {
    key: 'handleRecordTypeDropdownCommit',
    value: function handleRecordTypeDropdownCommit(path, value) {
      var onRecordTypeCommit = this.props.onRecordTypeCommit;


      if (onRecordTypeCommit) {
        onRecordTypeCommit(value);
      }
    }
  }, {
    key: 'handleVocabularyDropdownCommit',
    value: function handleVocabularyDropdownCommit(path, value) {
      var onVocabularyCommit = this.props.onVocabularyCommit;


      if (onVocabularyCommit) {
        onVocabularyCommit(value);
      }
    }
  }, {
    key: 'renderVocabularyInput',
    value: function renderVocabularyInput() {
      var _props2 = this.props,
          config = _props2.config,
          intl = _props2.intl,
          recordTypeValue = _props2.recordTypeValue,
          vocabularyValue = _props2.vocabularyValue;
      var recordTypes = config.recordTypes;


      if (!recordTypeValue || !recordTypes[recordTypeValue] || !recordTypes[recordTypeValue].vocabularies) {
        return null;
      }

      return _react2.default.createElement(
        'div',
        { className: _SearchFormVocab2.default.common },
        _react2.default.createElement(
          Label,
          null,
          intl.formatMessage(messages.vocabulary)
        ),
        _react2.default.createElement(VocabularyInput, {
          recordTypes: config.recordTypes,
          recordType: recordTypeValue,
          value: vocabularyValue,
          formatVocabularyLabel: this.formatVocabularyLabel,
          onCommit: this.handleVocabularyDropdownCommit
        })
      );
    }
  }, {
    key: 'render',
    value: function render() {
      var _props3 = this.props,
          advancedSearchCondition = _props3.advancedSearchCondition,
          config = _props3.config,
          intl = _props3.intl,
          keywordValue = _props3.keywordValue,
          recordTypeValue = _props3.recordTypeValue,
          recordTypeInputReadOnly = _props3.recordTypeInputReadOnly,
          recordTypeInputRootType = _props3.recordTypeInputRootType,
          recordTypeInputServiceTypes = _props3.recordTypeInputServiceTypes,
          showButtons = _props3.showButtons,
          onAdvancedSearchConditionCommit = _props3.onAdvancedSearchConditionCommit;


      var fullTextPanelHeader = _react2.default.createElement(
        'h3',
        null,
        _react2.default.createElement(_reactIntl.FormattedMessage, messages.fullTextSearch)
      );

      // If showButtons is false, render the button bar anyway, but with height 0, so that the submit
      // button will exist on the page, invisibly. This allows pressing enter on fields to submit the
      // form.

      var headerStyle = showButtons ? null : { height: '0', overflow: 'hidden', margin: '0' };

      var header = _react2.default.createElement(
        'header',
        { style: headerStyle },
        _react2.default.createElement(_SearchButtonBar2.default, null)
      );

      var footer = showButtons ? _react2.default.createElement(
        'footer',
        null,
        _react2.default.createElement(_SearchButtonBar2.default, null)
      ) : null;

      return _react2.default.createElement(
        'form',
        { autoComplete: 'off', className: _SearchForm2.default.common, onSubmit: this.handleFormSubmit },
        header,
        _react2.default.createElement(
          _cspaceLayout.Panel,
          null,
          _react2.default.createElement(
            'div',
            { className: _SearchFormRecordType2.default.common },
            _react2.default.createElement(RecordTypeInput, {
              label: intl.formatMessage(messages.recordType),
              recordTypes: this.getRecordTypes(),
              rootType: recordTypeInputRootType,
              serviceTypes: recordTypeInputServiceTypes,
              value: recordTypeValue,
              formatRecordTypeLabel: this.formatRecordTypeLabel,
              onCommit: this.handleRecordTypeDropdownCommit,
              readOnly: recordTypeInputReadOnly
            }),
            this.renderVocabularyInput()
          ),
          _react2.default.createElement(
            _PanelContainer.ConnectedPanel,
            {
              collapsible: true,
              header: fullTextPanelHeader,
              name: 'fullTextSearch',
              recordType: recordTypeValue
            },
            _react2.default.createElement(LineInput, {
              label: intl.formatMessage(messages.keyword),
              value: keywordValue,
              onCommit: this.handleKeywordInputCommit
            })
          ),
          _react2.default.createElement(_AdvancedSearchBuilder2.default, {
            condition: advancedSearchCondition,
            config: config,
            recordType: recordTypeValue,
            onConditionCommit: onAdvancedSearchConditionCommit
          })
        ),
        footer
      );
    }
  }]);

  return SearchForm;
}(_react.Component);

exports.default = SearchForm;


SearchForm.propTypes = propTypes;