'use strict';

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

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 }; }

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


const 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'
  }
});

const 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
};

class SearchForm extends _react.Component {
  constructor() {
    super();

    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);
  }

  getRecordTypes() {
    var _props = this.props;
    const config = _props.config,
          perms = _props.perms;
    const recordTypes = config.recordTypes;


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

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

      return true;
    });
  }

  formatRecordTypeLabel(name, config) {
    const intl = this.props.intl;


    return intl.formatMessage(config.messages.record.collectionName) || name;
  }

  formatVocabularyLabel(name, config) {
    const intl = this.props.intl;


    return intl.formatMessage(config.messages.name) || name;
  }

  handleFormSubmit(event) {
    event.preventDefault();

    const onSearch = this.props.onSearch;


    if (onSearch) {
      onSearch();
    }
  }

  handleKeywordInputCommit(path, value) {
    const onKeywordCommit = this.props.onKeywordCommit;


    if (onKeywordCommit) {
      onKeywordCommit(value);
    }
  }

  handleRecordTypeDropdownCommit(path, value) {
    const onRecordTypeCommit = this.props.onRecordTypeCommit;


    if (onRecordTypeCommit) {
      onRecordTypeCommit(value);
    }
  }

  handleVocabularyDropdownCommit(path, value) {
    const onVocabularyCommit = this.props.onVocabularyCommit;


    if (onVocabularyCommit) {
      onVocabularyCommit(value);
    }
  }

  renderVocabularyInput() {
    var _props2 = this.props;
    const config = _props2.config,
          intl = _props2.intl,
          recordTypeValue = _props2.recordTypeValue,
          vocabularyValue = _props2.vocabularyValue;
    const 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
      })
    );
  }

  render() {
    var _props3 = this.props;
    const 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;


    const 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.

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

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

    const 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
    );
  }
}

exports.default = SearchForm;
SearchForm.propTypes = propTypes;